如何在网格中镶嵌 1000 个栅格?按文件名顺序?

问题描述

我将我的图像(光栅图像)拆分为 1000 个图块以进行图像分割预测,并且我想将预测的光栅按顺序镶嵌到 1664*2432 的网格中。 我使用了这些帖子:

Generate a Photographic mosaic from a given set of thumbnails

How do you merge images into a canvas using PIL/Pillow?

PIL fill background image repeatedly

这是代码

import PIL,os,glob
from PIL import Image
from math import ceil,floor

PATH = '/content/drive/MyDrive/classification/CNN_segmentation/big_Image/prediction'

frame_width = 1664
images_per_row = 26
padding = 2

os.chdir(PATH)

images = glob.glob("*.tiff")
images = images[:988]       
img_width,img_height = Image.open(images[0]).size
sf = (frame_width-(images_per_row-1)*padding)/(images_per_row*img_width)       #scaling factor
scaled_img_width = ceil(img_width*sf)                   #s
scaled_img_height = ceil(img_height*sf)

number_of_rows = 38
frame_height = 2432 

new_im = Image.new('RGB',(frame_width,frame_height))

i,j=0,0
for num,im in enumerate(images):
    if num%images_per_row==0:
        i=0
    im = Image.open(im)
    im.thumbnail((64,64))
    #Iterate through a 4 by 4 grid with 100 spacing,to place my image
    y_cord = (j//images_per_row)*scaled_img_height
    new_im.paste(im,(i,y_cord))
    i=(i+64)+0
    j+=1

new_im.show()
new_im.save("out.jpg","JPEG",quality=80,optimize=True,progressive=True)
new_im

结果是一张 1664*2432 的图像,但所有像素都是黑色的。 我尝试使用“im=Rasterio.open(im)”而不是“im = Image.open(im)”,但我遇到了这个错误

AttributeError: 'DatasetReader' 对象没有属性 'thumbnail'

如果有人能帮助我,我将不胜感激。

注意:我用P代替RGB解决了这个问题,但还是不知道如何按文件名顺序拼接图像。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)