[Image-SIG] rotate image with transparent background?

Alec Bennett wrybread at gmail.com
Thu Jan 18 06:19:21 CET 2007


Douglas helped me off list, wanted to post the complete code in case
anyone else comes looking.  So here's the code to rotate an image and
paste it onto another image without creating a black bounding box:


from PIL import Image

collage = Image.open("cheese.jpg")

# open the pic and give it an alpha channel so it's transparent
im1 = Image.open("pic1.jpg").convert('RGBA')

# rotate it and expand it's canvas so the corners don't get cut off:
im2 = im1.rotate(32, expand = 1)

# note the second appearance of im2, that's necessary to paste without a bg
collage.paste(im2, (30, 30), im2 )
collage.save("new.jpg")

collage.show()


More information about the Image-SIG mailing list