I wanted to match all the sizes of the images I had.
First, transform the image at hand so that it is smaller than the specified size without changing the aspect ratio. Since it deforms while maintaining the aspect ratio, there will be a deviation from the specified size vertically or horizontally. I decided to paint the gap black.
It was done using Python and PIL.
(Addition) In the comments, @shiracamus added. Please also check!
image.py
from PIL import Image
#Specify vertical and horizontal sizes
width =360
height = 240
#Transform the height and width of the image so that it is smaller than the specified size
img = Image.open("hoge.jpg ")
img.thumbnail((width,height),Image.ANTIALIAS)
#Create a background image to fill in black
bg = Image.new("RGBA",[width,height],(0,0,0,255))
#Place the original image in the center of the background image
bg.paste(img,(int((width-img.size[0])/2),int((height-img.size[1])/2)))
I referred to the following site. Prevent plagiarism! Watermark images using Python Paste image in Python
Thank you very much.
Recommended Posts