简体   繁体   English

使用python和imagemagick附加图像

[英]Appending images using python and imagemagick

Most people recommend "wand" when it comes to imagemagick for python but how can I append images using it in python ? 多数人在使用python的imagemagick时建议使用“ wand”,但是如何在python中使用它附加图像呢? I want to add lables to bottom of images using imagemagick in python : http://www.imagemagick.org/Usage/annotating/ but wand api seems to be very basic and doesn't have lots of imgemagick commands including labels and appending. 我想在python中使用imagemagick在图像底部添加标签: http : //www.imagemagick.org/Usage/annotating/,但wand api似乎是非常基本的,并且没有很多imgemagick命令,包括标签和附加。

Is there any other way to use imagemagick in python ? 还有其他方法可以在python中使用imagemagick吗? my images are png type and are BytesIO streams inside the python code not files so I can not pass them to imagemagick using command line and can't save them on any temporary file either. 我的图像是png类型,是python代码中的BytesIO流而不是文件,因此我无法使用命令行将其传递给imagemagick,也无法将其保存在任何临时文件中。

I'm not really sure what your asking for, but I'm guessing you want to write a label "below" an image. 我不太确定您要什么,但我猜您想在图像下方写一个标签。 Here's an example with the library. 这是库的示例。

from wand.image import Image
from wand.compat import nested
from wand.color import Color
from wand.font import Font

with nested(Image(filename='logo:'),
            Image(filename='null:')) as (source, text):
    text.font = Font('Impact', 64)
    text.read(filename='label:Hello world!')
    largest_width = max(source.width, text.width)
    offset = (largest_width - min(source.width, text.width)) / 2
    with Image(width=largest_width,
               height=source.height + text.height,
               background=Color('WHITE')) as dst:
        dst.composite(source, 0, 0)
        dst.composite(text, int(offset), source.height)
        dst.save(filename="output.png")

你好,世界

Overview 总览

with nested(Image(filename='logo:'),
            Image(filename='null:')) as (source, text):

Create two images. 创建两个图像。 You would be responsible for replacing logo: image with your ByteIO buffer. 您将负责用ByteIO缓冲区替换logo:图像。 The null: image is a placeholder for allocating a wand instance. null: image是用于分配魔杖实例的占位符。

    text.font = Font('Impact', 64)
    text.read(filename='label:Hello world!')

This defines the typeface & text to draw. 这定义了要绘制的字体和文字。 The label: protocol can be replaced with caption: for additional behavior(s?). label:协议可以替换为caption:用于其他行为。

    with Image(width=largest_width,
               height=source.height + text.height,
               background=Color('WHITE')) as dst:

Create a third "blank" image that is large enough to include both images. 创建第三个“空白”图像,其大小足以包含两个图像。

        dst.composite(source, 0, 0)
        dst.composite(text, int(offset), source.height)

Copy the image data from the source & text to the new image. 将图像数据从sourcetext复制到新图像。

Imagemagick is great but has steep learning curve. Imagemagick很棒,但是学习曲线很陡。 You need to have 3rd party ImageMagick installed (Would have to be 32 or 64 bit depending on your version of python). 您需要安装第三方ImageMagick(根据您的python版本,必须为32位或64位)。 Installation alone is a pain considering you need to add it to path and the executable is necessary to run your script. 考虑到您需要将其添加到路径,并且可执行文件是运行脚本所必需的,因此仅进行安装是一件痛苦的事情。

Consider something more portable and contained like Pillow which has number of features for achieving your goal. 考虑一些更轻便的东西,例如Pillow,它具有许多可以实现您的目标的功能。

pip install pillow

In pillow you can read images from BytesIO. 在枕头上,您可以从BytesIO中读取图像。 And also draw or print on them. 并在其上绘制或打印。 There is number of options available. 有许多可用的选项。

from PIL import Image, ImageDraw
import io


# Reading bytes from file but you can skip that and simply open your bytes like below.

with open('picture.jpg', 'rb') as f:
    im = Image.open(io.BytesIO(f.read()))
    draw = ImageDraw.Draw(im)
    draw.text((10,10), "Hello World", fill=(255))
    im.show() # do whatever you like with the image

Check the docs for more examples. 查看文档以获取更多示例。 https://pillow.readthedocs.io/en/latest/ https://pillow.readthedocs.io/en/latest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM