How do I fix this error: ValueError: cannot determine region size; use 4-item box?

Viewed 41

Im writing a script that takes 10k images from a folder and collages them into rows of 100. Each image is 32x32 pixels. This is the script I wrote:

import glob
from PIL import Image, ImageDraw

#creates an Image based on given pixel size
collage = Image.new("RGBA", (3200,3200), color=(255,255,255,255))
images = glob.glob("*.png")
for image in images:
    img = Image.open(image)
    x = img.convert("RGBA")
    print(x)
#takes each image from the given directory above and merges it into a collage
for i in range(0,3200,32):
    for j in range(0,3200,32):
        img = images.pop()
        collage.paste(img, (i,j))

#displays output of collage
collage.show()

#saves Image to given directory
collage.save(r"C:\Users\17379\Desktop\Nouns DAO\Noun assets\noun"+image)

But when I run it, I get this error:

ValueError: cannot determine region size; use 4-item box

What am I doing wrong?

1 Answers

images is a list of file names. You do load them in your first loop, but you never save the images anywhere -- you throw them away. Thus, in your second loop, you are passing a file name to collage.paste, not an image object.

Maybe:

images = glob.glob("*.png")
images = [Image.open(image).convert("RGBA") for image in images]
for i in range(0,3200,32):
    for j in range(0,3200,32):
        img = images.pop()
        collage.paste(img, (i,j))

Also remember that .pop() pops from the end. If you want the images in order, you need .pop(0).

Related