Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 734
user2901745 Avatar asked Oct 21 '13 04:10

user2901745


3 Answers

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
like image 97
Jithin U. Ahmed Avatar answered Oct 23 '22 15:10

Jithin U. Ahmed


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.

like image 32
mpenkov Avatar answered Oct 23 '22 15:10

mpenkov


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)
like image 32
Dileep Maurya Avatar answered Oct 23 '22 13:10

Dileep Maurya