#!/usr/bin/python
from PIL import Image
import os, sys
path = "C:/Users/nonono/Desktop/hypergan/data/trainingData/"
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((256,256), Image.ANTIALIAS)
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
resize()
我从另一个答案here得到了这段代码,并将其拟合为我自己的参数,但是当我在命令行中运行它时,我得到了一个错误:
Traceback (most recent call last):
File "resize.py", line 16, in <module>
resize()
File "resize.py", line 11, in resize
im = Image.open(path+item)
File "C:\Users\nonono\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2822, in open
raise IOError("cannot identify image file %r" % (filename if filename else fp))
OSError: cannot identify image file 'C:/Users/nonono/Desktop/hypergan/data/trainingData/bx71zze7egy18wel231 resized.jpg'
文件夹trainingData
中充满了大量图像,无论宽高比如何,我都希望将其调整为256x256的大小,之后我将不再需要这些图像,因此,如果有一种替代图像的方法,只是复制,调整大小和重命名,那会很棒(除非我读错了,我在python上有大约2周的经验)
任何帮助将不胜感激