0

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

从一组给定的缩略图生成照片马赛克

如何使用 PIL/Pillow 将图像合并到画布中?

PIL 反复填充背景图像

这是代码:

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”对象没有属性“缩略图”

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

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

4

0 回答 0