[英]How can I save lots of images after resizing them all into another directory in Python(PIL)?
这是我的代码:
# -*- coding: utf-8 -*-
#!/usr/bin/python
import PIL
from PIL import Image
import os,sys
path = "/home/ozer/Desktop/Yedek/Workspace/"
dirs = os.listdir (path)
def resize():
for item in dirs:
if os.path.isfile(path1+item):
img = Image.open(path1+item)
f,e = os.path.splitext(path1+item)
basewidth = 100
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize))
img.save("/home/ozer/Desktop/Scripts/Last/"+"*.jpg","JPEG")
resize()
如果我让此脚本将经过调整大小的图像保存在名为“ path”的文件夹中,它将调整所有图像的大小并保存在此处,但会造成混乱,我的意思是将未调整大小和调整大小的图像全部保存在一个目录中。 当我尝试编写这样的解决方案时,它只会在最后一行显示的目录中保存一张图片。 你能帮我吗?
尝试
img.save("/home/ozer/Desktop/Scripts/Last/"+item+".jpg","JPEG")
或者,等效地
img.save("/home/ozer/Desktop/Scripts/Last/{}.jpg".format(item),"JPEG")
从Python 3.6开始,编写起来更加容易
img.save(f"/home/ozer/Desktop/Scripts/Last/{item}.jpg","JPEG")
目前,您正在尝试为每个输入文件创建一个名为*.jpg
的输出文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.