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

Wand should not rely on default encoding on Python 2.x #163

Closed
jgehrcke opened this issue Dec 2, 2013 · 4 comments
Closed

Wand should not rely on default encoding on Python 2.x #163

jgehrcke opened this issue Dec 2, 2013 · 4 comments
Labels

Comments

@jgehrcke
Copy link

jgehrcke commented Dec 2, 2013

Example code:

# -*- coding: utf-8 -*-
from wand.drawing import Drawing
from wand.image import Image
import sys


def annotate(path, isovalue):
    with Drawing() as draw:
        with Image(filename=sys.argv[1]) as image:
            draw.font_size = 20
            draw.gravity = "south_west"
            draw.text(0, 0, u'Φ')
            draw(image)
            image.save(filename="out.png")

With Wand 0.3.5, Python 2.7 on Linux (Ubuntu 12.04) this results in

  File "/projects/bioinfp_apps/Python-2.7.3/lib/python2.7/site-packages/wand/drawing.py", line 431, in text
    body = binary(body)
  File "/projects/bioinfp_apps/Python-2.7.3/lib/python2.7/site-packages/wand/compat.py", line 44, in binary
    return string.encode()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a6' in position 0: ordinal not in range(128)

The reason is that in compat.py (https://github.com/dahlia/wand/blob/master/wand/compat.py#L44) we are currently doing a simple return string.encode(). If called without arguments, encode uses the current default string encoding which is ASCII in case of Python 2. Obviously, ASCII cannot encode my greek character there and the UnicodeEncodeError is thrown.

We cannot and dont want to change the fact that ASCII is Python 2's default encoding. Relevant discussion about this topic can be found here:

So the solution would be to make a well-grounded decision about which encoding to use. I tried UTF-8 and it worked for me (I changed line 44 of compat.py accordingly). I am not entirely sure why this works, for this I do not understand enough of which data format the imagemagick binaries do expect.

@dahlia
Copy link
Collaborator

dahlia commented Dec 2, 2013

ImageMagick actually seems to provide a property to set string encoding (DrawGetTextEncoding() and DrawSetTextEncoding()) while Wand does not expose them. I will work on this soon. Thanks for your reporting!

@jgehrcke
Copy link
Author

jgehrcke commented Dec 2, 2013

Just my thoughts, you may of course do something else :-): then Wand maybe should not directly expose DrawSetTextEncoding, but rather use it internally and set it to UTF-8 (IM's docks say The only character encoding which may be specified at this time is "UTF-8"). Wand could require people to use unicode objects in Python code, which would then (internally, again) become encoded as UTF-8 before passing to ImageMagick.

@dahlia
Copy link
Collaborator

dahlia commented Dec 2, 2013

I made a small change ☝️ for this.

dahlia added a commit that referenced this issue Dec 2, 2013
@jgehrcke
Copy link
Author

jgehrcke commented Dec 2, 2013

Great, thanks.

@dahlia dahlia closed this as completed Dec 2, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants