批量修改图像的大小 Python PIL

复制代码
#-*-coding:utf-8-*-
import os
import os.path
from PIL import Image
import time
def ResizeImage(filein, fileout, width, height, type):
    img = Image.open(filein)
    out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
    dir_path = str('/'.join(fileout.split('/')[:-1]))
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    out.save(fileout, type)


if __name__ == "__main__":
    filein = r'4__fenlei\test.png'

    for i in "23456789ABVDEFGHKMNPRSTUVWXYZ":
        target_path = "4__fenlei/" + i + "/"
        for title in os.listdir(target_path):
            print(title)
            filein = target_path + title
            print(filein)

            fileout = filein.replace('4__fenlei','fenlei')
            width = 30
            height = 40
            type = 'png'
            ResizeImage(filein, fileout, width, height, type)
           
复制代码

 

posted @   淋哥  阅读(933)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示