New PIL user : How to merge images

V

vincent_delft

Does any one can give me sample of PIL code that merge 2 pictures.

To "normalize" pictures size, I would like to superpose pictures on a "fixed
size" background.

Thanks




I've tried :

import Image
bg=Image.open('back.jpg') # size 800,600
img=Image.open('test.jpg') # size 400,400

But the both following command return an error
img.paste(bg)
bg.paste(img)
 
R

Roberto Lopez-Gulliver

try this,

import Image

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# create/read your images
bg = Image.new('RGB', (800, 600), RED)
img = Image.new('RGB', (400, 400), GREEN)
## paste it
W, H = bg.size
w, h = img.size
xo, yo = (W-w)/2, (H-h)/2
bg.paste(img, (xo, yo, xo+w, yo+h))
## voila!
bg.show()

you may also consider using ImageMagick (www.imagemagick.org) for this special case.

hope this helps.

--r
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top