扩展图像 (PIL/Pillow)
Extending an image (PIL/Pillow)
我可以使用以下代码在基本图像上绘制黑色条带(或矩形):
base_width, base_height = img.size
background = Image.new('RGBA', (base_width, base_height/3),(0,0,0,146))
offset = (0,base_height/2)
img.paste(background,offset,mask=background)
结果:
但我如何扩展图像的高度,使所述黑色条带出现在图像底部边框下方,在图像本身之外?
如果我在上面的代码中移动 offset
,黑色条带不能移动到基本图像的边界之外,所以这不是一个可行的解决方案。
这是一种方法:
- 创建大小为
(base_width, base_height + background.height)
的 new_img
- 将原来的
img
粘贴到 new_img
的 (0, 0)
- 将
background
粘贴到 new_img
的 (0, base_height)
我可以使用以下代码在基本图像上绘制黑色条带(或矩形):
base_width, base_height = img.size
background = Image.new('RGBA', (base_width, base_height/3),(0,0,0,146))
offset = (0,base_height/2)
img.paste(background,offset,mask=background)
结果:
但我如何扩展图像的高度,使所述黑色条带出现在图像底部边框下方,在图像本身之外?
如果我在上面的代码中移动 offset
,黑色条带不能移动到基本图像的边界之外,所以这不是一个可行的解决方案。
这是一种方法:
- 创建大小为
(base_width, base_height + background.height)
的new_img
- 将原来的
img
粘贴到new_img
的(0, 0)
- 将
background
粘贴到new_img
的(0, base_height)