将所有tiff图像合并为一个图像

2024-04-19 18:29:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有15个瓷砖或tiff文件的文件夹,我想把它作为一个单一的文件与所有的图像作为一个tiff图像。所有瓷砖应缝合为单个tiff图像。我该怎么做

到目前为止我试过什么

import imageio
import os

path = "path/to/dir"
image_path_list = os.listdir(path)

with imageio.get_writer("new_image.tif") as new_image:
    for image_path in image_path_list:
        image = imageio.imread(path+image_path)
        new_image.append_data(image)

这将另存为tiff文件中的单独图像。我想将所有图像缝合在一起并按如下方式保存:

Desired Output

1,2,3…,15代表瓷砖。需要作为单个图像进行缝合


Tags: 文件topath图像imageimport文件夹new
3条回答

从你的评论看来,你准备考虑一个非Python的解决方案,所以我在终端中使用<强> IGimeMaGeI/Stand >蒙太奇15幅图像如下:

magick montage -tile 3x -geometry +0+0 09*tif result.tif

enter image description here

要演示如何将5幅图像而不是3幅图像进行布局,请添加不同的背景,并以不同方式影响水平和垂直间距,以下是一个变体:

magick montage -background magenta -tile 5x -geometry +5+15 09*tif result.tif

enter image description here


仅供参考,我制作了15个随机着色的块,如下所示:

for x in {a..o} ; do magick xc: +noise random -scale 80x50\! 09$x.tif ; done 

使用numpy: 此脚本接受图像生成器(以便更快地处理大型图像)。它不会事先检查它们的尺寸。如果图像高度与行高度不匹配,或者行的宽度不同,则该操作将失败

    #!/usr/bin/env python3
import numpy as np
from imageio import imread, imwrite
from pathlib import Path


def tile_images(images, cols):
    """Tile images of same size to grid with given number of columns.
    
    Args:
        images (collection of ndarrays)
        cols (int): number of colums 
    
    Returns:
        ndarray: stitched image
    """
    images = iter(images)
    first = True
    rows = []
    i = 0
    while True:
        
        try:
            im = next(images)
            print(f"add image, shape: {im.shape}, type: {im.dtype}")
        except StopIteration:
            if first:
                break
            else:
                im = np.zeros_like(im)  # black background
                
        if first:
            row = im  # start next row
            first = False  
        else:    
            row = np.concatenate((row, im), axis=1)  # append to row
            
        i += 1
        if not i % cols:
            print(f"row done, shape: {row.shape}")
            rows.append(row) # finished row
            first = True
            
    tiled = np.concatenate(rows)   # stitch rows    
    return tiled        

def main():
    images = (imread(f) for f in Path().glob("*.*") if f.suffix in (".jpg", ".png") if f.name != "new.png") 
    new = tile_images(images, cols=3)
    imwrite("new.png", new)


def test():
    im1 = np.arange(65536).reshape(256,256)
    im2 = np.arange(65536/2).reshape(128,256)
    
    images = [im1,im1,im1,im2,im2,im2]
    
    # works
    new = tile_images(images, 3)
    imwrite("new.png", new)
    
    # failes
    new = tile_images(images, 2)
    imwrite("new2.png", new)
    
    
if __name__ == "__main__":
    main()
    # test()

给定一个目录,其中包含15个大小相同的图像

使用PIL(枕头),我最终得到:

from PIL import Image


import os

path_to_file ='tiff-files'


images = []



for i in os.listdir(path_to_file):
    with Image.open(path_to_file+'/'+i) as im:
        images.append(im.copy())

    
new_image = Image.new(images[0].mode, (images[0].size[0]*3,images[0].size[1]*5))



new_image.paste(images[0])
new_image.paste(images[1],(images[0].size[0]*1,0))
new_image.paste(images[2],(images[0].size[0]*2,0))
new_image.paste(images[3],(0,images[0].size[1]*1))
new_image.paste(images[4],(images[0].size[0]*1,images[0].size[1]*1))
new_image.paste(images[5],(images[0].size[0]*2,images[0].size[1]*1))
new_image.paste(images[6],(0,images[0].size[1]*2))
new_image.paste(images[7],(images[0].size[0]*1,images[0].size[1]*2))
new_image.paste(images[8],(images[0].size[0]*2,images[0].size[1]*2))
new_image.paste(images[9],(0,images[0].size[1]*3))
new_image.paste(images[10],(images[0].size[0]*1,images[0].size[1]*3))
new_image.paste(images[11],(images[0].size[0]*2,images[0].size[1]*3))
new_image.paste(images[12],(0,images[0].size[1]*4))
new_image.paste(images[13],(images[0].size[0]*1,images[0].size[1]*4))
new_image.paste(images[14],(images[0].size[0]*2,images[0].size[1]*4))

new_image.show()

让我知道它是否有效

马克·塞切尔在这里建议了一个新版本,希望它更好

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).show()

更多地按照https://stackoverflow.com/a/68468658/2836621的思路思考

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'



    

image = []
for i in os.listdir(path_to_file):
    with Image.open(path_to_file+'/'+i) as im:
        image.append(im.copy()) 
        
     


w, h = image[0].size



new_image = np.zeros((4 * h, 3 * w)).astype('uint8')


col = 0
row = -1
for i, img in enumerate(image):
    if not i % 3 :
        row += 1
        col = 0
    img = np.array(img)
    new_image[row * h: (row + 1) * h, col * w: (col + 1) * w] = img
    col += 1




image_pillow = Image.fromarray(new_image, mode = 'L')

image_pillow.save('prova.tif', mode = 'L')


image_pillow.show()

使用.tif图像灰度8位进行测试

修改为RGB et similia添加3个通道:

new_image = np.zeros((3 * h, 3 * w,3)).astype('uint8')

new_image[row * h: (row + 1) * h,col * w: (col + 1) * w,:] = img

最后一个示例是8位灰度图像的函数:

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'

    
def stich_img(path_to_file, x , y):

    image = []
    for i in os.listdir(path_to_file):
            image.append(path_to_file+'/'+i)
    
    print(image)
         
    if len(image) >= x*y:
        pass
    
    else:
        # raise ValueError('not enough images in path_to_file !!!!!!!!!!!')
        raise ValueError('EXCEPTION not enough images in path_to_file !!!!!!!!!!!', x*y ,'images  needed : ', len(image),'images present !!!')
    
    
    image = image[:x*y] #-----> riduce lista immagini al numero richiesto
    
    
    with Image.open(image[0]) as img0:
        w, h = img0.size
   
    
    
    
    # new_image = np.zeros((4 * h, 3 * w)).astype('uint8')
    new_image = np.zeros((y * h, x * w)).astype('uint8')
    
    
     
    col = 0
    row = -1
    for i, imgs in enumerate(image):
        with Image.open(imgs) as img:
            if not i % x :
                row += 1
                col = 0
            img = np.array(img)
            new_image[row * h: (row + 1) * h, col * w: (col + 1) * w] = img
            col += 1
            
    

    
    image_pillow = Image.fromarray(new_image, mode = 'L')
    
    return image_pillow

img_stiched = stich_img(path_to_file, 3,5)   

# img_stiched.save('prova.tif', mode = 'L')


img_stiched.show()

相关问题 更多 >