Python PIL Paste

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

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

Each image is 128x in size 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 make this work?

+6
source share
2 answers

You are not loading the image correctly. The built-in open function simply opens a new file descriptor. To load an image using 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, do the following:

 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 does not load the actual image data until it is needed.

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

+1
source

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

I did this on my server using ubuntu:

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

Source: https://habr.com/ru/post/956377/


All Articles