开发者

How to pass an unicode char argument to ImageMagick?

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.


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.


  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'])
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜