【问题标题】:Use PIL to create an array of images with borders bteween images使用 PIL 创建一个图像数组,其中包含图像之间的边界
【发布时间】:2021-05-02 22:18:14
【问题描述】:

来自this question我有以下代码:

def create_collage(cols, rows, width, height, listofimages):
    thumbnail_width = width//cols
    thumbnail_height = height//rows
    size = thumbnail_width, thumbnail_height
    new_im = Image.new('RGB', (width, height)) # 300 pt border
    ims = []
    for p in listofimages:
        im = Image.open(p)
        im.thumbnail(size)
        ims.append(im)
    i = 0
    x = 0
    y = 0
    for col in range(cols):
        for row in range(rows):
            new_im.paste(ims[i], (x, y))
            i += 1
            y += thumbnail_height
        x += thumbnail_width
        y = 0

    new_im.save("out.png", "PNG", quality=80, optimize=True, progressive=True)

但我需要图像之间的边框。然后我找到了this answer here,所以我将代码修改如下:

def create_collage(images_per_row, img_width, img_height, padding, frame_width, images):
    sf = (frame_width - (images_per_row - 1) * padding) / (images_per_row * img_width) # scaling factor
    scaled_img_width = ceil(img_width * sf)
    scaled_img_height = ceil(img_height * sf) + padding # THIS NEEDED CHANGING as no horizontal borders were showing
    number_of_rows = ceil(len(images) / 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(images):
        if num % images_per_row == 0:
            i = 0
        im = Image.open(im)
        im.thumbnail((scaled_img_width, scaled_img_height))
        y_cord = (j // images_per_row) * scaled_img_height
        new_im.paste(im, (i, y_cord))
        i = (i + scaled_img_width) + padding
        j += 1

    new_im.save("out.png", "PNG", quality = 80, optimize = True, progressive = True)

几乎工作了,但现在我在图像底部得到了一个边框,这不是我想要的。

如何在拼贴图像之间获得一致的水平和垂直边界,而不会使它们出现在拼贴之外?

【问题讨论】:

    标签: python python-3.x python-imaging-library


    【解决方案1】:

    在朋友的帮助下,这就是我现在所拥有的。我可以接受这个作为我问题的答案,尽管我希望其他人有更清洁的解决方案。这个答案是基于上面第二个代码sn-p:

    from PIL import Image
    import glob
    from math import ceil, floor
    
    listofimages = glob.glob("directory/*") # this is where you need to put your path to directory
    
    def create_collage(images_per_row, img_width, img_height, padding, images):
        frame_width = (width * images_per_row) + (padding * (images_per_row - 1))
        cell_width = ceil((frame_width + padding) / images_per_row)
        new_image_width = cell_width - padding
        scaling_factor = new_image_width / img_width
    
        scaled_img_width = ceil(img_width * scaling_factor)
        scaled_img_height = ceil(img_height * scaling_factor)
    
        cell_height = scaled_img_height + padding
    
        number_of_rows = ceil(len(images) / images_per_row)
        frame_height = cell_height * number_of_rows - padding
    
        new_im = Image.new('RGB', (frame_width, frame_height))
        
        x_cord = 0
        for num, im in enumerate(images):
            if num % images_per_row == 0:
                x_cord = 0
            im = Image.open(im)
            im.thumbnail((scaled_img_width, scaled_img_height))
            y_cord = (num // images_per_row) * cell_height
            new_im.paste(im, (x_cord, y_cord))
            x_cord += cell_width
    
        # using PNG because JPEG is lossy
        new_im.save("Collage.png", "PNG", quality = 80, optimize = True, progressive = True)
    
    width, height = Image.open(listofimages[0]).size # get size of images (assume all same size)
    create_collage(5, width, height, 20, listofimages)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2019-12-28
      • 2021-02-02
      • 1970-01-01
      相关资源
      最近更新 更多