I need to take an image and place it onto a new, generated white background in order for it to be converted into a downloadable desktop wallpaper. So the process would go:
In PIL, I see the ImageDraw
object, but nothing indicates it can draw existing image data onto another image. Suggestions or links anyone can recommend?
Firstly we opened the primary image and saved its image object into variable img1. Then we opened the image that would be used as an overlay and saved its image object into variable img2. Then we called the paste method to overlay/paste the passed image on img1.
You can use the PIL library to mask the images. Add in the alpha parameter to img2, As you can't just paste this image over img1. Otherwise, you won't see what is underneath, you need to add an alpha value.
This can be accomplished with an Image instance's paste
method:
from PIL import Image img = Image.open('/path/to/file', 'r') img_w, img_h = img.size background = Image.new('RGBA', (1440, 900), (255, 255, 255, 255)) bg_w, bg_h = background.size offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2) background.paste(img, offset) background.save('out.png')
This and many other PIL tricks can be picked up at Nadia Alramli's PIL Tutorial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With