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

UnicodeDecodeError when using Image(data="some.jpeg") #3081

Closed
spookylukey opened this issue Mar 26, 2013 · 2 comments · Fixed by #3082
Closed

UnicodeDecodeError when using Image(data="some.jpeg") #3081

spookylukey opened this issue Mar 26, 2013 · 2 comments · Fixed by #3082
Milestone

Comments

@spookylukey
Copy link

For all JPEGs I have tried, I get the following error:

from IPython.display import Image
Image(data=file("myimage.jpeg").read())

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-89-c318d6052b14> in <module>()
----> 1 Image(data=file("/home/luke/images/marvin1.jpg").read())

/usr/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in __call__(self, result)
    240             self.update_user_ns(result)
    241             self.log_output(format_dict)
--> 242             self.finish_displayhook()
    243 
    244     def flush(self):

/usr/lib/python2.7/dist-packages/IPython/zmq/displayhook.pyc in finish_displayhook(self)
     59         sys.stdout.flush()
     60         sys.stderr.flush()
---> 61         self.session.send(self.pub_socket, self.msg, ident=self.topic)
     62         self.msg = None
     63 

/usr/lib/python2.7/dist-packages/IPython/zmq/session.pyc in send(self, stream, msg_or_type, content, parent, ident, buffers, subheader, track, header)
    557 
    558         buffers = [] if buffers is None else buffers
--> 559         to_send = self.serialize(msg, ident)
    560         flag = 0
    561         if buffers:

/usr/lib/python2.7/dist-packages/IPython/zmq/session.pyc in serialize(self, msg, ident)
    461             content = self.none
    462         elif isinstance(content, dict):
--> 463             content = self.pack(content)
    464         elif isinstance(content, bytes):
    465             # content is already packed, as in a relayed message

/usr/lib/python2.7/dist-packages/IPython/zmq/session.pyc in <lambda>(obj)
     76 
     77 # ISO8601-ify datetime objects
---> 78 json_packer = lambda obj: jsonapi.dumps(obj, default=date_default)
     79 json_unpacker = lambda s: extract_dates(jsonapi.loads(s))
     80 

/usr/lib/python2.7/dist-packages/zmq/utils/jsonapi.pyc in dumps(o, **kwargs)
     70         kwargs['separators'] = (',', ':')
     71 
---> 72     return _squash_unicode(jsonmod.dumps(o, **kwargs))
     73 
     74 def loads(s, **kwargs):

/usr/lib/python2.7/dist-packages/simplejson/__init__.pyc in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, use_decimal, namedtuple_as_object, tuple_as_array, bigint_as_string, sort_keys, item_sort_key, **kw)
    332         sort_keys=sort_keys,
    333         item_sort_key=item_sort_key,
--> 334         **kw).encode(obj)
    335 
    336 

/usr/lib/python2.7/dist-packages/simplejson/encoder.pyc in encode(self, o)
    235         # exceptions aren't as detailed.  The list call should be roughly
    236         # equivalent to the PySequence_Fast that ''.join() would do.
--> 237         chunks = self.iterencode(o, _one_shot=True)
    238         if not isinstance(chunks, (list, tuple)):
    239             chunks = list(chunks)

/usr/lib/python2.7/dist-packages/simplejson/encoder.pyc in iterencode(self, o, _one_shot)
    309                 Decimal=Decimal)
    310         try:
--> 311             return _iterencode(o, 0)
    312         finally:
    313             key_memo.clear()

UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

If I do:

Image(filename="myimage.jpeg")

it works fine. Using 'data=' also works fine for PNGs

@minrk
Copy link
Member

minrk commented Mar 26, 2013

If you don't specify the format='jpeg', it's assuming that you have given it PNG data. It shouldn't raise a UnicodeError, but it still probably shouldn't result in a valid image.

@spookylukey
Copy link
Author

Thanks, that fixed my issue at least

@minrk minrk closed this as completed in bc29c71 Mar 27, 2013
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
instead of only b64-encoding for valid image data,
b64-encode unless it is specifically detected as b64-encoded valid image data.
This prevents garbage data from skipping the b64 step,
and causing unicode errors.

Closes ipython#3081
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

Successfully merging a pull request may close this issue.

2 participants