Skip to content

Commit

Permalink
Fix .get_page_image to prefer paths over streams
Browse files Browse the repository at this point in the history
See discussion in #124
for details. h/t @ubmarco
  • Loading branch information
jsvine committed Apr 28, 2020
1 parent 9c4329d commit ab957de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. Currently g

The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [0.5.20] — 2020-04-28
### Fixed
- TKTK

## [0.5.19] — 2020-04-16
### Changed
- Add `utils.decimalize` performance improvement (830d117) [h/t @ubmarco]
Expand Down
17 changes: 14 additions & 3 deletions pdfplumber/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ def get_page_image(stream, page_no, resolution):
"""
For kwargs, see http://docs.wand-py.org/en/latest/wand/image.html#wand.image.Image
"""
stream.seek(0)
with wand.image.Image(file=stream, resolution=resolution) as pages:
img = wand.image.Image(image=pages.sequence[page_no])

# If we are working with a file object saved to disk
if hasattr(stream, "name"):
spec = dict(filename = "{0}[{1}]".format(stream.name, page_no))
postprocess = lambda img: img

# If we instead are working with a BytesIO stream
else:
stream.seek(0)
spec = dict(file = stream)
postprocess = lambda img: wand.image.Image(image=img.sequence[page_no])

with wand.image.Image(resolution=resolution, **spec) as img_init:
img = postprocess(img_init)
if img.alpha_channel:
img.background_color = wand.image.Color('white')
img.alpha_channel = 'background'
Expand Down

0 comments on commit ab957de

Please sign in to comment.