user2901745
user2901745

Reputation: 413

Python PIL Paste

I want to paste a bunch of images together with PIL. For some reason, when I run the line blank.paste(img,(i*128,j*128)) I get the following error: ValueError: cannot determine region size; use 4-item box

I tried messing with it and using a tuple with 4 elements like it said (ex. (128,128,128,128)) but it gives me this error: SystemError: new style getargs format but argument is not a tuple

Each image is 128x and has a naming style of "x_y.png" where x and y are from 0 to 39. My code is below.

from PIL import Image

loc = 'top right/'
blank = Image.new("RGB", (6000,6000), "white")

for x in range(40):
    for y in reversed(range(40)):
        file = str(x)+'_'+str(y)+'.png'
        img = open(loc+file)
        blank.paste(img,(x*128,y*128))

blank.save('top right.png')

How can I get this to work?

Upvotes: 9

Views: 19750

Answers (3)

Dileep Maurya
Dileep Maurya

Reputation: 21

For me the methods above didn't work.

After checking image.py I found that image.paste(color) needs one more argument like image.paste(color, mask=original). It worked well for me by changing it to this:

image.paste(color, box=(0, 0) + original.size)

Upvotes: 2

Jithin U. Ahmed
Jithin U. Ahmed

Reputation: 1811

This worked for me, I'm using Odoo v9 and I have pillow 4.0.

I did it steps in my server with ubuntu:

# pip uninstall pillow
# pip install Pillow==3.4.2
# /etc/init.d/odoo restart

Upvotes: 6

mpenkov
mpenkov

Reputation: 21912

You're not loading the image correctly. The built-in function open just opens a new file descriptor. To load an image with PIL, use Image.open instead:

from PIL import Image
im = Image.open("bride.jpg") # open the file and "load" the image in one statement

If you have a reason to use the built-in open, then do something like this:

fin = open("bride.jpg") # open the file
img = Image.open(fin) # "load" the image from the opened file

With PIL, "loading" an image means reading the image header. PIL is lazy, so it doesn't load the actual image data until it needs to.

Also, consider using os.path.join instead of string concatenation.

Upvotes: 5

Related Questions