from PIL import Image, ImageOps
import sys
list = ['jpg', 'jpeg', 'png']
before = sys.argv[1]
after = sys.argv[2]
if len(sys.argv) == 3:
if before[-3:] in list and after[-3:] in list and after[-3:] == before[-3:]:
try:
i_file = Image.open(before)
shirt = Image.open('shirt.png')
#o_file = Image.open(after)
except FileNotFoundError:
print(f"Input does not exist")
sys.exit(1)
size = (600, 600)
#Resizing the before image here
ImageOps.fit(i_file, size).save(before)
i_file = Image.open(before)
#Trying to paste the shirt over i_file(before2.jpg image)
Image.Image.paste(i_file, shirt, (0, 0))
i_file.save(after)
else:
print('Input and output have different extensions')
sys.exit(1)
$ python shirt.py before2.jpg after2.jpg
在上面的代码中,我想将衬衫.png(常量图像)粘贴到 before.jpg(从命令行获取)上,然后将其保存到 after.jpg(也从命令行获取)
我面临的问题是这样的,我的after.jpg出来是这样的 之后.jpg
我已经尝试过,但我认为问题出在我执行粘贴方法的方式上。
请LZ帮帮我