简体   繁体   中英

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.

Here's one way:

  1. Create a new_img that is (base_width, base_height + background.height) in size
  2. Paste the original img into new_img at (0, 0)
  3. Paste the background into new_img at (0, base_height)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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