首先需要导入Pillow包
#####图像路径
basicpath = r'/Users/PycharmProjects/代码及图像/inputs' # 这个地址换成您自己的文件地址
styleImg = Image.open(basicpath + r'/style/starry_night.jpg') # 风格图
contentImg = Image.open(basicpath + r'/content/Dipping-Sun.jpg') # 内容图
#对图像进行尺寸统一的裁切
def ImageResize(styleImg,contentImg):
if(contentImg.width<=styleImg.width):
width = contentImg.width
else:
width = styleImg.width
if(contentImg.height<=styleImg.height):
height = contentImg.height
else:
height = styleImg.height
outStyle = styleImg.resize((width, height), Image.ANTIALIAS)
outContent = contentImg.resize((width, height), Image.ANTIALIAS)
outContent.save('/Users/PycharmProjects/代码及图像/inputs/CutContent/outContent.jpg')
outStyle.save('/Users/PycharmProjects/代码及图像/inputs/CutStyle/outStyle.jpg')
return 1
- 先用Image.open(‘文件绝对路径+后缀名’) 赋值给一个变量styleImg,它就代表了打开了这个图像。
- 然后使用Image.resize()函数,作用是裁切图像长宽,参数是 (width,height),Image.ANTIALIAS
- 返回值是一个图像,在PIL中,使用save函数可以保存到指定路径(前提是路径的文件夹必须存在)(也可以使用python的os根据路径自动创建原本不存在的文件夹)