views:

244

answers:

2

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:

1) Generate new, all white image with 1440x900 dimensions
2) Place existing image on top, centered
3) Save as single image

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?

A: 

Image.blend()? [link]

Or, better yet, Image.paste(), same link.

Felix
"Creates a new image by interpolating between the given images, using a constant alpha. Both images must have the same size and mode." From the documentation, it appears they can't be different sizes.
Sebastian
I noted `Image.paste()`, too, which was ultimately the solution.
Felix
+2  A: 

This can be accomplished with an Image instance's paste method:

import Image
img=Image.open('/pathto/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

unutbu
You, my friend, are a lifesaver. Works perfectly.
Sebastian