tags:

views:

312

answers:

2

Suppose the char of "▣" is in somefont.ttf's glyph table.

char = unichr(9635)

subprocess.call(['convert', '-font', 'somefont.ttf', '-size', '50x50', '-label:%s' % char, 'output.png'])

subprocess.call(['convert', '-font', 'somefont.ttf', '-size', '50x50', ('-label:%s' % char).encode('utf-8'), 'output.png'])

Both create an blank image with no char of "▣" on it. Is above code correct? Or the problem is on ImageMagick side which doesn't capture label in certain ranges?

The reason for using ImageMagick to draw text is it's more flexible than PIL to fix and align text to certain image size.

EDIT:

According to yuku's suggestion, I tried the following methods:

root@host:~@convert -font somefont.ttf -size 50x65 label:▣ output.png
root@host:~@convert -font somefont.ttf -size 50x65 label:'▣' output.png

Both outputs a question mark but not the correct character.

A: 
  1. Try to get it working by hand using ASCII labels in your console.
    $ convert -font somefont.ttf -size 50x50 -label:A output.png
    convert: unrecognized option `-label:A' @ convert.c/ConvertImageCommand/1753.
    1 ;( 
    $ convert -font somefont.ttf -size 50x50 -label A output.png
    convert: missing an image filename `output.png' @ convert.c/ConvertImageComm\
    and/2775.
    1 ;( 
  1. Use subprocess.check_call instead of os.system.

    import subprocess
    
    
    if __name__=="__main__":
        cmd = 'convert -font somefont.ttf -size 50x50'.split()
        #XXX command arguments are invalid 
        subprocess.check_call(cmd + ['-label', unichr(9635), 'output.png'])
    
J.F. Sebastian
'-label' and unichr(9635) should be in one element in cmd list otherwise you will get "convert: missing an image filename `output.png'" error.
jack
@jack: read comment after `XXX` mark.
J.F. Sebastian
@J.F. your first command should be right. why you got a "unrecognized option" error?
jack
@jack: the first command is incorrect because there is `'-'` before `'label'`.
J.F. Sebastian
+1  A: 

According to this link, you need to pass the text encoded in UTF8. It will be able to draw the correct character outside ASCII range.

yuku
char=unichr(9635) already encoded the character in unicode. what should I do?
jack
also tried subprocess.call(['convert', '-font', 'somefont.ttf', '-size', '50x50', ('-label:%s' % char).encode('utf-8'), 'output.png']) but no success.
jack
unicode is not the same as utf8, `unicode()` and `unichr()` return unicode objects while `unichr().encode('utf8')` creates a `str` object which is what you want. Also watch out for `LC_*` environment variables which also may play a role.
Otto Allmendinger
@Otto, yes, i tried encode('utf8') method. system locale are all en_US.UTF-8.
jack
@yuku, I tried your method (convert -font somefont.ttf -size 50x65 label:▣ output.png). It outputs a question mark but not expected like what your link suggests.
jack
Maybe your terminal encoding is not set correctly. Just a speculation.
yuku