使用 PIL/Pillow 在基础图像上粘贴叠加层(带文本)
Pasting an overlay (with text) on top of a base image, using PIL/Pillow
我有给定的图像。我想创建一个黑色条带作为该图像的叠加层,并在所述条带上写上文字。 Here's 我的意思的一个直观例子。
我正在使用 Python PIL
来完成这个(在 Django 项目中),这是我到目前为止所写的内容:
from PIL import Image, ImageFont, ImageDraw
img_width, img_height = img.size #getting the base image's size
if img.mode != 'RGB':
img = img.convert("RGB")
strip = Image.new('RGB', (img_width, 20)) #creating the black strip
draw = ImageDraw.Draw(strip)
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 16)
draw.text((img_width/2,10),"foo bar",(255,255,255),font=font) #drawing text on the black strip
offset = (img_width/2,img_height/2)
img.paste(strip,offset) #pasting black strip on the base image
# and from here on, I save the image, create thumbnails, etc.
这根本不起作用。如图所示,图像显示时没有任何文本或黑条,就像它原来的样子。
请注意,如果我直接尝试在图像上书写(没有黑条),它会完美。此外,图像处理本身也工作得很好(即在我不在图像上写任何东西的情况下)。
谁能帮我指出问题?位置(或偏移量)有问题吗?我pasting
错了吗? RGB
转换是罪魁祸首吗?或者它完全是另外一回事?一个说明性的例子会很棒。顺便说一句,性能也很重要;我正在尝试尽可能不花钱地做到这一点。
以防万一,下面是我稍后对图像文件所做的操作:
from django.core.files.uploadedfile import InMemoryUploadedFile
img.thumbnail((300, 300))
thumbnailString = StringIO.StringIO()
img.save(thumbnailString, 'JPEG', optimize=True)
newFile = InMemoryUploadedFile(thumbnailString, None, 'temp.jpg','image/jpeg', thumbnailString.len, None)
# and then the image file is saved in a database object, to be served later
问题出在 offset
。 docs Image.paste
表示:
If a 2-tuple is used instead, it’s treated as the upper left corner.
因此,对于 (img_width/2, img_height/2)
,您是将条带粘贴到大图像中间的左上角。这是将 "foo bar" 粘贴到您的示例图片上:
如果您将其更改为 offset = (0, img_height/2)
,它会将其从左侧粘贴到一半。这是粘贴到正确位置的 "foo bar":
条带可以稍微高一点(高度可以根据给定字体大小的文本计算),并且文本可以居中,但我希望这些问题已经在 Stack Overflow 的其他地方得到了回答或在 Pillow 文档中。
我有给定的图像。我想创建一个黑色条带作为该图像的叠加层,并在所述条带上写上文字。 Here's 我的意思的一个直观例子。
我正在使用 Python PIL
来完成这个(在 Django 项目中),这是我到目前为止所写的内容:
from PIL import Image, ImageFont, ImageDraw
img_width, img_height = img.size #getting the base image's size
if img.mode != 'RGB':
img = img.convert("RGB")
strip = Image.new('RGB', (img_width, 20)) #creating the black strip
draw = ImageDraw.Draw(strip)
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 16)
draw.text((img_width/2,10),"foo bar",(255,255,255),font=font) #drawing text on the black strip
offset = (img_width/2,img_height/2)
img.paste(strip,offset) #pasting black strip on the base image
# and from here on, I save the image, create thumbnails, etc.
这根本不起作用。如图所示,图像显示时没有任何文本或黑条,就像它原来的样子。
请注意,如果我直接尝试在图像上书写(没有黑条),它会完美。此外,图像处理本身也工作得很好(即在我不在图像上写任何东西的情况下)。
谁能帮我指出问题?位置(或偏移量)有问题吗?我pasting
错了吗? RGB
转换是罪魁祸首吗?或者它完全是另外一回事?一个说明性的例子会很棒。顺便说一句,性能也很重要;我正在尝试尽可能不花钱地做到这一点。
以防万一,下面是我稍后对图像文件所做的操作:
from django.core.files.uploadedfile import InMemoryUploadedFile
img.thumbnail((300, 300))
thumbnailString = StringIO.StringIO()
img.save(thumbnailString, 'JPEG', optimize=True)
newFile = InMemoryUploadedFile(thumbnailString, None, 'temp.jpg','image/jpeg', thumbnailString.len, None)
# and then the image file is saved in a database object, to be served later
问题出在 offset
。 docs Image.paste
表示:
If a 2-tuple is used instead, it’s treated as the upper left corner.
因此,对于 (img_width/2, img_height/2)
,您是将条带粘贴到大图像中间的左上角。这是将 "foo bar" 粘贴到您的示例图片上:
如果您将其更改为 offset = (0, img_height/2)
,它会将其从左侧粘贴到一半。这是粘贴到正确位置的 "foo bar":
条带可以稍微高一点(高度可以根据给定字体大小的文本计算),并且文本可以居中,但我希望这些问题已经在 Stack Overflow 的其他地方得到了回答或在 Pillow 文档中。