Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python和imagemagick附加图像_Python_Imagemagick_Wand_Bytesio - Fatal编程技术网

使用python和imagemagick附加图像

使用python和imagemagick附加图像,python,imagemagick,wand,bytesio,Python,Imagemagick,Wand,Bytesio,对于python的imagemagick,大多数人都建议使用“wand”,但如何在python中使用它附加图像呢?我想在python中使用imagemagick将标签添加到图像的底部:但wand api似乎非常基础,没有很多imgemagick命令,包括标签和附加 在python中使用imagemagick还有其他方法吗?我的图像是png类型的,是python代码中的字节流,不是文件,因此我无法使用命令行将它们传递给imagemagick,也无法将它们保存在任何临时文件中。imagemagick

对于python的imagemagick,大多数人都建议使用“wand”,但如何在python中使用它附加图像呢?我想在python中使用imagemagick将标签添加到图像的底部:但wand api似乎非常基础,没有很多imgemagick命令,包括标签和附加


在python中使用imagemagick还有其他方法吗?我的图像是png类型的,是python代码中的字节流,不是文件,因此我无法使用命令行将它们传递给imagemagick,也无法将它们保存在任何临时文件中。

imagemagick很棒,但学习曲线陡峭。您需要安装第三方ImageMagick(必须是32位或64位,具体取决于您的python版本)。考虑到您需要将其添加到path,并且运行脚本需要可执行文件,单独安装是一件痛苦的事情

考虑一些更便携、更内敛的东西,比如枕头,它有很多功能来实现你的目标

pip安装枕

在枕头中,你们可以阅读BytesIO的图像。也可以在上面画画或打印。有很多选择

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

查看文档以了解更多示例

Imagemagick很棒,但学习曲线陡峭。您需要安装第三方ImageMagick(必须是32位或64位,具体取决于您的python版本)。考虑到您需要将其添加到path,并且运行脚本需要可执行文件,单独安装是一件痛苦的事情

考虑一些更便携、更内敛的东西,比如枕头,它有很多功能来实现你的目标

pip安装枕

在枕头中,你们可以阅读BytesIO的图像。也可以在上面画画或打印。有很多选择

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

查看文档以了解更多示例

我不太确定你的要求是什么,但我猜你想在图像的“下方”写一个标签。下面是一个库的示例

从wand.image导入图像
从wand.compat导入嵌套
从wand.color导入颜色
从wand.font导入字体
带有嵌套(图像(filename='logo:'),
图像(filename='null:')为(源,文本):
text.font=font('Impact',64)
text.read(filename='label:Hello world!')
最大宽度=最大(source.width,text.width)
偏移量=(最大宽度-最小值(source.width,text.width))/2
带图像(宽度=最大宽度),
高度=source.height+text.height,
背景=颜色(“白色”)作为dst:
dst.composite(源,0,0)
dst.composite(文本、整数(偏移量)、源高度)
保存(filename=“output.png”)

概述

带有嵌套(图像(filename='logo:')的
,
图像(filename='null:')为(源,文本):
创建两个图像。您将负责用ByteIO缓冲区替换
徽标:
图像。
null:
图像是分配魔杖实例的占位符

text.font=font('Impact',64)
text.read(filename='label:Hello world!')
这定义了要绘制的字体和文本。
标签:
协议可以替换为
标题:
,用于其他行为

带有图像(宽度=最大宽度),
高度=source.height+text.height,
背景=颜色(“白色”)作为dst:
创建第三个“空白”图像,其大小足以包含两个图像

dst.composite(源代码,0,0)
dst.composite(文本、整数(偏移量)、源高度)

将图像数据从
源文件
文本
复制到新图像。

我不确定您的要求,但我猜您想在图像的“下方”写一个标签。下面是一个库的示例

从wand.image导入图像
从wand.compat导入嵌套
从wand.color导入颜色
从wand.font导入字体
带有嵌套(图像(filename='logo:'),
图像(filename='null:')为(源,文本):
text.font=font('Impact',64)
text.read(filename='label:Hello world!')
最大宽度=最大(source.width,text.width)
偏移量=(最大宽度-最小值(source.width,text.width))/2
带图像(宽度=最大宽度),
高度=source.height+text.height,
背景=颜色(“白色”)作为dst:
dst.composite(源,0,0)
dst.composite(文本、整数(偏移量)、源高度)
保存(filename=“output.png”)

概述

带有嵌套(图像(filename='logo:')的
,
图像(filename='null:')为(源,文本):
创建两个图像。您将负责用ByteIO缓冲区替换
徽标:
图像。
null:
图像是分配魔杖实例的占位符

text.font=font('Impact',64)
text.read(filename='label:Hello world!')
这定义了要绘制的字体和文本。
标签:
协议可以替换为
标题:
,用于其他行为

带有图像(宽度=最大宽度),
高度=source.height+text.height,
背景=颜色(“白色”)作为dst:
创建第三个“空白”图像,其大小足以包含两个图像

dst.composite(源代码,0,0)
dst.composite(文本、整数(偏移量)、源高度)

将图像数据从
文本
复制到新图像。

参见@fmw42的魔杖绘制文本是的,我看到了,但我正在寻找附加标签,如我发送的链接,否则,如果我想创建一个空图像,则速度会慢得多。将我的图像复制到它的顶部为底部创建一个较小的图像,并在其上写入文本,然后复制到底部。我不知道Wand,但从我看到的情况来看,您应该能够使用
with image
方法将文本(或其他原语)直接绘制到图像上,指定