Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image fails to open file after seek-read-seek #213

Closed
mmiermans opened this issue Feb 8, 2015 · 3 comments
Closed

Image fails to open file after seek-read-seek #213

mmiermans opened this issue Feb 8, 2015 · 3 comments
Assignees

Comments

@mmiermans
Copy link

The combination of doing a seek and read (in this order) causes MagickReadImageFile to consistently return 0. The following code requires that there is an image wand.png. It has three test cases: the first two run successfully and the third one fails after doing a seek-read-seek.

from wand.image import Image

fn = 'wand.png'

# This works as expected
fp = open(fn)
try:
  fp.read(1)
  fp.seek(0)
  with Image(file=fp) as img:
    print '%s could be opened with read-seek' % img.format
finally:
  fp.close()

# This is a workaround that fixes the issue, but I have no idea why a flush 
# would change a file that has not been written to. 
fp = open(fn)
try:
  fp.seek(0)
  fp.read(1)
  fp.flush()
  fp.seek(0)
  with Image(file=fp) as img:
    print '%s could be opened with seek-read-flush-seek' % img.format
finally:
  fp.close()

# This fails
fp = open(fn)
try:
  fp.seek(0)
  fp.read(1)
  fp.seek(0)
  with Image(file=fp) as img:
    print '%s could be opened with seek-read-seek' % img.format
finally:
  fp.close()

Here is the output that I get:

PNG could be opened with read-seek
PNG could be opened with seek-read-flush-seek
Traceback (most recent call last):
  File "./test.py", line 34, in <module>
    with Image(file=fp) as img:
  File "/usr/local/lib/python2.7/dist-packages/wand/image.py", line 1979, in __init__
    self.read(file=file, resolution=resolution)
  File "/usr/local/lib/python2.7/dist-packages/wand/image.py", line 2048, in read
    self.raise_exception()
  File "/usr/local/lib/python2.7/dist-packages/wand/resource.py", line 222, in raise_exception
    raise e
wand.exceptions.MissingDelegateError: no decode delegate for this image format `/tmp/magick-ZEtTP5H0' @ error/constitute.c/ReadImage/544

I've traced the problem back to the call to MagickReadImageFile returning 0 in wand/image.py:2029. Different images and a new virtualenv made no change. I am on the latest stable version of Wand (0.3.9) and on:

>>> print(sys.version)
2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1]

At the moment I am just really curious what is going on internally. Thanks for doing an awesome job!


Edit: My guess is that the problem originates from magick_wand.c:6837.

@dahlia dahlia self-assigned this Feb 19, 2015
@dahlia
Copy link
Collaborator

dahlia commented Feb 19, 2015

I just noticed that it is not reproducible since Python 3.2. It seems probably because builtin open() became an alias of io.open() since Python 3.

@dahlia
Copy link
Collaborator

dahlia commented Feb 19, 2015

Also the workaround using flush() doesn’t work for me.

@emcconville
Copy link
Owner

emcconville commented Jan 17, 2019

This is an oddity from Python 2. Be sure to open the file in binary mode with open(fn, 'rb') or even open(fn, 'r+b').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants