简体   繁体   中英

How to Paste a Border Image (Constant) over Multiple Thumbnail Images?

Here is what i'm trying to achieve:

  1. Resize Multiple Product images into 500 x 500 pixels

  2. Paste all of these resized images individually onto a 800 x 800 image and output as individual images

For now, I have managed to complete Step 1, but have no idea how to proceed to step 2. Here is my code for Step 1:

from PIL import Image
import os, sys

path = "C:\\Users\\User\\Desktop\\Test\\"
dirs = os.listdir( path )
final_size = 500;

def resize_aspect_fit():
    for item in dirs:
         if item == '.png':
             continue
         if os.path.isfile(path+item):
             im = Image.open(path+item)
             f, e = os.path.splitext(path+item)
             size = im.size
             ratio = float(final_size) / max(size)
             new_image_size = tuple([int(x*ratio) for x in size])
             im = im.resize(new_image_size, Image.ANTIALIAS)
             new_im = Image.new("RGBA", (final_size, final_size), (255,255,255,000))

             new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
             new_im.save(f + 'resized.png', 'PNG', quality=100)
resize_aspect_fit()

Thanks!

Edit:

Here's an image illustration for better explanation of what i am trying to achieve. I have 2 smiley faces (500 x 500) which i need to paste over the default 800 x 800 image multiple times (centered) to produce 2 separate images of 800 x 800.

Example

You're nearly there, you can use .paste() with an offset to paste into the middle over your image. a little bit like:

Border_im = Image.open(pathToBorder)
product_im = Image.open(pathToProduct)
x_offset=150;
y_offset=150;
Border_im.paste(product_im, (x_offset,y_offset))

Then save out border_im to a new file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM