How do you composite an image onto another image with PIL in Python?

后端 未结 5 1546
忘了有多久
忘了有多久 2020-12-07 14:19

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:

5条回答
  •  旧巷少年郎
    2020-12-07 15:01

    This is to do something similar

    Where I started was by generating that 'white background' in photoshop and exporting it as a PNG file. Thats where I got im1 (Image 1). Then used the paste function cause it's way easier.

    from PIL import Image
    
    im1 = Image.open('image/path/1.png')
    im2 = Image.open('image/path/2.png')
    area = (40, 1345, 551, 1625)  
    im1.paste(im2, area)
                       l>(511+40) l>(280+1345)
             |    l> From 0 (move, 1345px down) 
              -> From 0 (top left, move 40 pixels right)
    

    Okay so where did these #'s come from? (40, 1345, 551, 1625) im2.size (511, 280) Because I added 40 right and 1345 down (40, 1345, 511, 280) I must add them to the original image size which = (40, 1345, 551, 1625)

    im1.show() 
    

    to show your new image

提交回复
热议问题