最快的ubuntu图像库,用于注释

时间:2011-05-19 14:42:32

标签: python image-processing imagemagick

我目前正在处理图片,并希望了解有关注释的最快图像处理库。我目前正在使用此命令:

convert orig.tif -background Red -density 300 -font /usr/share/fonts/truetype/msttcorefonts/Arial.ttf -pointsize 12 -gravity south -splice 0x150 -gravity southwest -annotate +50+50 "left corner" -gravity southeast -annotate +50+50 'right corner' +repage endorsed.tif  

我在python中的subprocess.call方法中调用它。如果我在命令行中执行此操作,则总共需要大约1秒的时间:

real    0m0.162s
user    0m0.940s
sys         0m0.030s

我的节目平均为1.96秒:

[**copied_from** avg] 0:00:00.071887
[**mysql** avg] 0:00:00.000265
[**cropped** avg] 0:00:00.433935
[**rm** avg] 0:00:00.007758
[**copied_to** avg] 0:00:00.147880
[**endorsed** avg] 0:00:01.963496
[**mssql** avg] 0:00:00.000010

我不确定为什么从命令行调用它或通过python的subprocess.call(cmd,shell = True)之间存在差异。无论哪种方式,这两者都太慢了。

我基本上是裁剪和注释图像。看起来像imagemagick的转换,裁剪速度足够快,但注释太慢了。有谁知道更快的图像库?我很习惯使用python / ruby​​,但是在这一点上,任何更快的解决方案都会有所帮助。

谢谢大家。

1 个答案:

答案 0 :(得分:2)

与实际shell(例如bash)相比,从subprocess.call调用事物总是会产生相当大的开销。

您是否尝试过使用imagemagick's python bindings而不是通过subprocess调用它? (编辑:取消那些...那些绑定已经过时了。我不知道有什么更好的,但是......我很长时间没有通过python使用imagemagick ......)

您也可以使用PIL执行此操作,但它的灵活性稍差,可能不会明显加快。

无论它值多少,这都等同于使用PIL的imagemagick命令。

import Image
import ImageDraw
import ImageFont

rect_height = 150

im_orig = Image.open('orig.tif')
width, height = im_orig.size
im = Image.new('RGB', (width, height+rect_height))
im.paste(im_orig, (0,0))

width, height = im.size
dpi = 300
im.info['dpi'] = (dpi, dpi)
font_size = 12
font = ImageFont.truetype('/usr/share/fonts/truetype/arial.ttf', 
                         int(dpi * font_size / 72.0))

draw = ImageDraw.Draw(im)
draw.rectangle([(0,height-rect_height), (width, height)], fill='red')

text = 'left corner'
text_width, text_height = font.getsize(text)
draw.text((50, height - 50 - text_height), 
    text, fill='black', font=font)

text = 'right corner'
text_width, text_height = font.getsize(text)
draw.text((width - 50 - text_width, height - 50 - text_height), 
        text, fill='black', font=font)

im.save('output.tif')

正如您所看到的,它不太方便,但确实有效。此外,这似乎比我的机器上的imagemagick更快。对于较大的图像,上述脚本需要约0.43秒,而您的等效imagemagick命令(直接从shell运行)需要约1.1秒。当然,imagemagick可以比PIL更多地进行 lot ,但是对于python中的简单图像处理,PIL工作得很好。

(编辑:速度差异是因为PIL将输入tif(LZW压缩)保存为未压缩的tif,而imagemagick将其保存为LZW压缩tiff,类似于输入。)

相关问题