【问题标题】:ValueError: cannot determine region size; use 4-item boxValueError:无法确定区域大小;使用 4 件箱
【发布时间】:2021-03-16 03:03:52
【问题描述】:

我试图从一个包含 9 个 RGB 图像的图像列表中创建一个拼贴画。我收到 ValueError:无法确定区域大小;使用 4 件箱。 下面是功能。我做错了什么?

def create_collage(width, height, imageList):
    cols = 3
    rows = 3
    thumbnail_width = width//cols
    thumbnail_height = height//rows
    size = thumbnail_width, thumbnail_height
    new_im = Image.new('RGB', (width, height))
    ims = []
    for image in imageList:
        th = image.thumbnail(size)
        ims.append(th)
    i = 0
    x = 0
    y = 0
    for col in range(cols):
        for row in range(rows):
            print(i, x, y)
            new_im.paste(ims[i], (x, y))
            i += 1
            y += thumbnail_height
        x += thumbnail_width
        y = 0

    new_im.save("Collage.png")

【问题讨论】:

    标签: image image-processing python-imaging-library


    【解决方案1】:

    我最终使用了下面的代码,它工作得很好。

    #Ceating 3 x 3 拼贴:

    def getSize(imageList):
        for image in imageList:
            img_width, img_height = image.size
        return img_width, img_height
    
    
    def createCollage(imageList, frame_width, images_per_row):
    
        img_width, img_height = getSize(imageList)
    
        #scaling factor
        sf = (frame_width-(images_per_row-1))/(images_per_row*img_width)
    
        scaled_img_width = ceil(img_width*sf)
        scaled_img_height = ceil(img_height*sf)
    
        number_of_rows = ceil(len(imageList)/images_per_row)
        frame_height = ceil(sf*img_height*number_of_rows) 
    
        new_im = Image.new('RGB', (frame_width, frame_height))
    
        i,j=0,0
        for num, im in enumerate(imageList):
            if num%images_per_row==0:
                i=0
            
            #resizing opened image
            im.thumbnail((scaled_img_width,scaled_img_height))
            #Iterate through a 3 x 3 grid
            y_cord = (j//images_per_row)*scaled_img_height
            new_im.paste(im, (i,y_cord))
            # print(i, y_cord)
            i=(i+scaled_img_width)
            j+=1
    
        # # new_im.show()
        new_im.save("collage.png", "PNG")
    

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2018-02-14
      相关资源
      最近更新 更多