Alex
Alex

Reputation: 21

Python PIL concatenate images

I'm working on a project where I need to concatenate a lot of images (80282). Each image is 256 x 256 pixels, and some of the files are empty (no image), so I need to create a blank image to replace the file. I have the data in this format: data-D0H0-X52773-Y14041

X and Y correspond to the coordinates that I need to concatenate in order. The order is from the top left X52773-Y14314 to the bottom right X52964-Y14041. It is 294 iterations on X and 274 on Y. Here is the code I have written which is not working correctly, I could use any help if you have an idea, currently, my images are not well aligned on Y. For example, the image X10-Y10 is not under the image X10-Y11 as it should. I think I have some problem using correctly the try: and except: Thanks for you help !

from PIL import Image

width = 75264
height = 70144
new_im = Image.new('RGBA', (75264, 70144))

x_offset = 0
y_offset = 0

coordinate = {}
coordinate['x']=52672 
coordinate['y']=14314

#top image line should be from:    X52,672-Y14,314 to X52,965-Y14,314
#bottom image line should be from: X52,672-Y14,041 to X52,965-Y14,041

for irow in range(0, 274): 
    for icol in range(0, 294): 
        try:
            if (x_offset == width):
                coordinate['y'] = coordinate['y'] - 1
                coordinate['x'] = 52672
            img = Image.open("data-D0H0-X"+str(coordinate['x'])+"-Y"+str(coordinate['y'])+".png")

        except:
            coordinate['x'] = coordinate['x'] + 1
            blank = Image.new('RGBA', (256,256))
            new_im.paste(blank, (x_offset, y_offset))
            x_offset += 256
            if (x_offset == width):
                x_offset = 0
                y_offset += 256
            break

        new_im.paste(img, (x_offset, y_offset))
        x_offset += 256
        if (x_offset == width):
            x_offset = 0
            y_offset += 256
        coordinate['x'] = coordinate['x'] + 1


new_im.show()
new_im.save('full_image.png')

EDIT: Here is the new code I've modified according to your answer. However, I'm still getting an error: struct.error: 'I' format requires 0 <= number <= 4294967295

Not sure if my coordinate calcul is right now.

CODE:

from PIL import Image
import glob
import imghdr

width = 75264
height = 70144

new_im = Image.new('RGBA', (width, height))

for filename in glob.glob('data-D0H0-X*.png'):
    tmp_arr = filename.split('-')
    x_coord = int(tmp_arr[2][1:6])
    y_coord = int(tmp_arr[3][1:6])

    info = imghdr.what(filename)
    if (info == "png"):
        new_img = Image.open(filename)
    else:
        new_img = Image.new('RGBA', (256,256))


    x_coord = (x_coord-52672)*256
    y_coord = (14314-y_coord)*256
    print x_coord, y_coord
    new_im.paste(new_img, (x_coord, y_coord))

new_im.show()
new_im.save('full_image.png')

Upvotes: 2

Views: 2877

Answers (1)

Noam Kremen
Noam Kremen

Reputation: 418

Your coordinate arithmetic seems a bit off. Since your images are 256x256 you should never have to inc/dec x and y by 1 as you do in your code. The code below hasn't been tested but should provide a general outline.

from PIL import Image
import glob

width = 75264
height = 70144
new_im = Image.new('RGBA', (width, height))

for filename in glob.glob('data-D0H0-X*.png'):
    tmp_arr = filename.split('-')
    x_coord = int(tmp_arr[2][1:])
    y_coord = int(tmp_arr[3][1:])
    small_img = Image.open(filename)
    new_im.paste(small_img, (x_coord, y_coord))

new_im.show()
new_im.save('full_image.png')

Upvotes: 3

Related Questions