pythonで画像のアスペクト比を維持したままサイズ変更して余白を埋める

やりたいこと

540x700の画像を16:9の1920x1080に変更したい(比率は変えず)

 

540x700と縦の700の方が大きいので700を基準に計算

 

1080 / 700 = 約1.5

700 x 1.5 = 1050

縦のサイズが1050になる

横のサイズも540 x 1.5 = 810で810となる

 

そしたら1920x1080まで余白を黒くする

 

微妙な30の隙間の埋め方がわからないので、誰か教えてくれると嬉しいです。

 

以下コード

# -*- coding: utf-8 -*-
from PIL import Image

class image_aspect():

def __init__(self, image_file, aspect_width, aspect_height):
self.img = Image.open(image_file)
self.aspect_width = aspect_width
self.aspect_height = aspect_height
self.result_image = None

def change_aspect_rate(self):

img_width = self.img.size[0]
img_height = self.img.size[1]

if img_width > img_height:
rate = self.aspect_width / img_width
else:
rate = self.aspect_height / img_height

rate = round(rate, 1)
self.img = self.img.resize((int(img_width * rate), int(img_height * rate)))
return self

def past_background(self):
self.result_image = Image.new("RGB", [self.aspect_width, self.aspect_height], (0, 0, 0, 255))
self.result_image.paste(self.img, (int((self.aspect_width - self.img.size[0]) / 2), int((self.aspect_height - self.img.size[1]) / 2)))
return self

def save_result(self, file_name):
self.result_image.save(file_name)

# example
if __name__ == "__main__":
image_aspect('./test_dir/test.jpg', 1920, 1080)\
.change_aspect_rate()\
.past_background()\
.save_result('result.jpg')