图片拼接后如何去除黑色space?

How to remove the black space after stitching the images together?

我有以下格式的图块图像:

每个数字代表一个方块。我使用了以下脚本(在 Whosebug 的帮助下)并将图像拼接在一起。以下是我用来拼接图片的脚本:

from PIL import Image
import os

path_to_file ='tiff-files'



def stich_tile(path_to_file, xx , yy):
    images = []
    for i in os.listdir(path_to_file):
            images.append(i)

    
    if len(images) >= xx*yy:
        pass
    
    else:
        raise ValueError('not enough images in path_to_file !!!!!!!!!!!')
        
    
    sq_x = xx
    sq_y = yy
    img_x = (Image.open(path_to_file+'/'+images[0]).size[0])
    img_y = (Image.open(path_to_file+'/'+images[0]).size[1])
    img_mode = (Image.open(path_to_file+'/'+images[0]).mode)
    
    new_image = Image.new(img_mode, (img_x*sq_x, img_y*sq_y))
    
    x = 0
    y = 0
    cnt = 0
    for i in images:
        with Image.open(path_to_file+'/'+i) as img:
            new_image.paste(img, (x,y))
            cnt += 1
            x += img_x 
            if cnt == sq_x:
                x = 0
                y += img_y
                cnt = 0
            else:
                pass
                
  
    return new_image

stich_tile(path_to_file, 3, 5).save('filename.tiff')

输出保存的图像如下所示:

我想删除创建的黑色图像。我该怎么做?

此处修改的脚本删除了拼接图像底部和右侧的黑色边框...只要问题出在起始图像内:


import numpy as np
from PIL import Image
import os

# path_to_file ='tiff-files'

# path_to_file ='tiff-files2'

# path_to_file ='tiff-files3'

# path_to_file ='tiff-files5'

# path_to_file ='tiff-files5'

path_to_file ='tiff-files6'


def stich_tile(path_to_file, xx , yy):
    images = []
    for i in os.listdir(path_to_file):
            images.append(i)
    
    images.sort() # sort images alphabetically
    # images.sort(key = lambda x: int(x.strip('.tiff').split('-')[1]))  ## ---> per path_to_file ='tiff-files3'
    
    images = images[:xx*yy] #-----> riduce lista immagini al numero richiesto
    
    print(images)

    print('lenght list of images', len(images), 'x and y requested', xx*yy)
    
    if len(images) >= xx*yy:
        pass
    
    else:
        # raise ValueError('not enough images in path_to_file !!!!!!!!!!!')
        raise ValueError('EXCEPTION not enough images in path_to_file !!!!!!!!!!!', xx*yy,'images  needed : ',   len(images),'images present !!!')
    
    sq_x = xx
    sq_y = yy
    img_x = (Image.open(path_to_file+'/'+images[0]).size[0])
    img_y = (Image.open(path_to_file+'/'+images[0]).size[1])
    img_mode = (Image.open(path_to_file+'/'+images[0]).mode)
    print('images[0] size : ',  img_x,  img_y,   img_x*sq_x,  img_y*sq_y)
    
    new_image = Image.new(img_mode, (img_x*sq_x, img_y*sq_y))
    print('new_image : size :', new_image.size)
    
    x = 0
    y = 0
    cnt = 0
    cnt_cycle = 0
    for i in images:
        with Image.open(path_to_file+'/'+i) as img:
            new_image.paste(img, (x,y))
            cnt += 1
            
            cnt_cycle += 1
            x += img_x 
            if cnt == sq_x:
                x = 0
                y += img_y
                cnt = 0
            else:
                pass
                
    print('count of for i in images cycles', cnt_cycle)
    
    new_image = np.array(new_image)
    print(new_image.shape, np.min(new_image), np.max(new_image))
    for ar_y in range(new_image.shape[0]-1,0,-1):
        res = np.all(new_image[ar_y,:] == (0,0,0))  
        if res:
            new_image = new_image[0:(ar_y),:]
            # print('black', ar_y)
            
        else:
            print('break at :', ar_y ,' row')
            break
    print(new_image.shape, np.min(new_image), np.max(new_image))
    
    print(new_image.shape, np.min(new_image), np.max(new_image))
    for ar_x in range(new_image.shape[1]-1,0,-1):
        res = np.all(new_image[:,ar_x] == (0,0,0))  
        if res:
            new_image = new_image[:,0:(ar_x)]
            # print('black', ar_x)
        else:
            print('break at :', ar_x ,' column')
            break
    print(new_image.shape, np.min(new_image), np.max(new_image))
    
    new_image = Image.fromarray(new_image)
    
    return new_image
 


try :
    pippo = stich_tile(path_to_file, 3,3)
    pippo.show()
    # pippo.save('RGB_black_tiff_3X.tiff')
    

except ValueError as err:
    print('stopped', err.args)

可以使用相同的方法从 top/left 中删除黑色边框。

可能是 pillow 库有一个内置的 option/function/whatever 调用它来做同样的事情....

有点晚了,用带黑色边框的 3X3 RGB tiff 图像测试了代码。如果它有效,请告诉我