首页 > 解决方案 > 将图像拼接在一起后如何去除黑色空间?

问题描述

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

在此处输入图像描述

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

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')

输出保存的图像如下所示: 在此处输入图像描述

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

标签: pythonpython-imaging-libraryimage-stitching

解决方案


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


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)

可以使用相同的方法从顶部/左侧删除黑色边框。

可能是枕头库有一个内置的选项/功能/无论它调用什么来做同样的事情......

有点晚了,测试了带有黑色边框的 3X3 RGB tiff 图像的代码.. 让我知道它是否有效


推荐阅读