简体   繁体   中英

Why is my tiled image shifted with pasting into Pillow?

I am writing a program where I chop up an image into many sub-tiles, process the tiles, then stitch them together. I am stuck at the stitching part. When I run my code, after the first row the tiles each shift one space over. I am working with 1000x1000 tiles and the image size can be variable. I also get this ugly horizontal padding that I can't figure out how to get rid of. Here is a google drive link to the images: https://drive.google.com/drive/folders/1HqRl29YlWUrsYoZP88TAztJe9uwgP5PS?usp=sharing

Clarification based on the comments

I take the original black and white image and crop it into 1000px x 1000px black and white tiles. These tiles are then re-colored to replace the white with a color corresponding to a density heatmap. The recolored tiles are then saved into that folder. The picture I included is one of the colored in tiles that I am trying to piece back together. When pieced together it should be the same shape but multi colored version of the black and white image

from PIL import Image
import os


stitched_image = Image.new('RGB', (large_image.width, large_image.height))
image_list = os.listdir('recolored_tiles')
current_tile = 0

for i in range(0, large_image.height, 1000):
    for j in range(0, large_image.width, 1000):
        p = Image.open(f'recolored_tiles/{image_list[current_tile]}')
        stitched_image.paste(p, (j, i), 0)
        current_tile += 1

stitched_image.save('test.png')

I am attaching the original image that I process in tiles and the current state of the output image: 原始预处理图像 处理输出

An example of the tiles found in the folder recolored_tiles: 在此处输入图片说明

First off, the code below will create the correct image:

from PIL import Image
import os

stitched_image = Image.new('RGB', (original_image_width, original_image_height))
image_list = os.listdir('recolored_tiles')
current_tile = 0

for y in range(0, original_image_height - 1, 894):
    for x in range(0, original_image_width - 1, 1008):
        tile_image = Image.open(f'recolored_tiles/{image_list[current_tile]}')
        print("x: {0}     y: {1}".format(x, y))
        stitched_image.paste(tile_image, (x, y), 0)
        current_tile += 1

stitched_image.save('test.png')

Explanation

First off, you should notice, that your tiles aren't 1000x1000. They are all 1008x984 because 18145x16074 can't be divided up into 19 1000x1000 tiles each.

Therefore you will have to put the correct tile width and height in your for loops:

for y in range(0, 16074, INSERT CURRECT RECOLORED_TILE HEIGHT HERE):
    for x in range(0, 18145, INSERT CURRECT RECOLORED_TILE WIDTH HERE):

Secondly, how python range works, it doesn't run on the last digit. Representation:

for i in range(0,5):
    print(i)

The output for that would be:

0
1
2
3
4

Therefore the width and height of the original image will have to be minused by 1, because it thinks you have 19 tiles, but there isn't.

Hope this works and what a cool project you're working on :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM