简体   繁体   English

扩展图像(PIL /枕头)

[英]Extending an image (PIL/Pillow)

I can use the following code to draw a black strip (or rectangle) atop a base image: 我可以使用以下代码在基础图像上方绘制黑条(或矩形):

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)

Result: 结果:

在此处输入图片说明

But how do I extend the height of the image such that the said black strip appears attached below the image's bottom border, outside the image itself ? 但是,如何扩展图像的高度,使所述黑色条看起来附着在图像底部边框的下方, 图像本身之外

If I move the offset in my code above, the black strip can't move beyond the borders of the base image, so that's not a viable solution. 如果我在上面的代码中移动了offset ,则黑条不能移动到基本图像的边界之外,因此这不是可行的解决方案。

Here's one way: 这是一种方法:

  1. Create a new_img that is (base_width, base_height + background.height) in size 创建一个new_img(base_width, base_height + background.height)
  2. Paste the original img into new_img at (0, 0) 将原始img粘贴到(0, 0) new_img
  3. Paste the background into new_img at (0, base_height) background粘贴到(0, base_height) new_img

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

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