Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paste an image onto a larger image using Pillow?

I've got a fairly simple code file:

from PIL import Image
til = Image.new("RGB",(50,50))
im = Image.open("tile.png") #25x25
til.paste(im)
til.paste(im,(23,0))
til.paste(im,(0,23))
til.paste(im,(23,23))
til.save("testtiles.png")

However, when I attempt to run it, I get the following error:

Traceback (most recent call last):
    til.paste(im)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1340, in paste
    self.im.paste(im, box)
ValueError: images do not match

What is causing this error? They are both RGB images, the docs don't say anything about this error.

like image 658
user1796160 Avatar asked Feb 09 '15 10:02

user1796160


1 Answers

The problem is in the first pasting - according to the PIL documentation (http://effbot.org/imagingbook/image.htm), if no "box" argument is passed, images' sizes must match.

EDIT: I actually misunderstood the documentation, you are right, it's not there. But from what I tried here, it seems like passing no second argument, sizes must match. If you want to keep the second image's size and place it in the upper-left corner of the first image, just do:

...
til.paste(im,(0,0))
...
like image 85
rayt Avatar answered Sep 21 '22 12:09

rayt