Python/PIL调整文件夹中所有图像的大小 [英] Python/PIL Resize all images in a folder

查看:236
本文介绍了Python/PIL调整文件夹中所有图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我认为它将调整指定路径中图像的大小 但是当我运行它时,什么都没有起作用,但是python不会抛出任何错误,所以我不知道该怎么办.请指教.谢谢.

I have the following code that I thought would resize the images in the specified path But when I run it, nothing works and yet python doesn't throw any error so I don't know what to do. Please advise. Thanks.

from PIL import Image
import os, sys

path = ('C:\Users\Maxxie\color\complete')

def resize():
for item in os.listdir(path):
    if os.path.isfile(item):
        im = Image.open(item)
        f, e = os.path.splitext(item)
        imResize = im.resize((200,200), Image.ANTIALIAS)
        imResize.save(f + ' resized.jpg', 'JPEG', quality=90)

resize()

推荐答案

#!/usr/bin/python
from PIL import Image
import os, sys

path = "/root/Desktop/python/images/"
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((200,200), Image.ANTIALIAS)
            imResize.save(f + ' resized.jpg', 'JPEG', quality=90)

resize()

您的错误属于文件的完整路径.代替 item 的必须是 path + item

Your mistake is belong to full path of the files. Instead of item must be path+item

这篇关于Python/PIL调整文件夹中所有图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆