PIL question

Peter Otten __peter__ at web.de
Tue Apr 6 10:05:04 CEST 2010


Tim Eichholz wrote:

> I'm trying to cut a BMP with 80 adjacent frames down to 40 using the
> Image.copy and .paste functions but I'm getting error "ValueError:
> images do not match" on the paste line.

> newimage.paste(cols[f], (f*framew, 0, (f*framew)+192, 192))

The 4-tuple doesn't match the size of the image you are pasting:

>>> from PIL import Image
>>> image = Image.open("tmp1.png")
>>> image.size
(134, 400)
>>> image.paste(image, (0, 0, 134, 400)) # correct size --> works
>>> image.paste(image, (0, 0, 134, 300)) # wrong size --> raises exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1077, in paste
    self.im.paste(im, box)
ValueError: images do not match

You can add

for index, image in enumerate(cols):
   print "image %d --> %s" % (index, image.size)
   # image.save("tmp%d.png" % index)

to your code to learn the actual size of your intermediate images.
After you have fixed the size problem and see that the resulting image 
doesn't meet your expectations (likely, don't despair) you can uncomment the 
image.save(...) statement and have a look at the intermediate images.

A hint about counting loops: instead of

x = 0
while x < n:
   ...
   x = x + 1

it is idiomatic python to write

for x in range(n):
    ...

and

x = 0
while x < total_width:
    # ...
    x += single_width

is typically expressed as

for x in range(0, total_width, single_width):
   ...

Have a look at the tutorial for more basic tips.

Peter



More information about the Python-list mailing list