zoukankan      html  css  js  c++  java
  • 利用pillow库(PIL)批量修改图片尺寸

    楔子

    平时使用Snipaste截图总是不注意图片的尺寸,导致保存了一堆大大小小尺寸不一的图片
    在写文档的时候插入图片显得很杂乱,因此抽空写了个小工具帮我批量格式化文件夹中的图片尺寸成一样的大小

    代码

    import os
    from tkinter import *
    
    
    class Application(object):
        def __init__(self):
            self.root = Tk()
            self.root.title('格式化图片尺寸')
            self.path = StringVar()
            self.size = StringVar()
    
            Label(self.root, text="目标路径:").grid(row=0, column=0)
            Entry(self.root, textvariable=self.path).grid(row=0, column=1)
            Button(self.root, text="路径选择", command=self.selectPath).grid(row=0, column=2)
    
            Label(self.root, text="目标大小:").grid(row=1, column=0)
            Entry(self.root, textvariable=self.size).grid(row=1, column=1)
            Button(self.root, text="确定选择", command=self.formatSize).grid(row=1, column=2)
    
            Button(self.root, text="退出程序", command=self.root.destroy).grid(row=2, column=2)
    
        def run(self):
            self.root.mainloop()
    
        def selectPath(self):
            from tkinter.filedialog import askdirectory
            path_ = askdirectory()
            self.path.set(path_)
    
        def formatSize(self):
            from PIL import Image
            from glob import glob
            import tkinter.messagebox as messagebox
            sourcesPathList = glob(os.path.join(self.path.get(), '*.png'))
            sourcesPathList.extend(glob(os.path.join(self.path.get(), '*.jpg')))
            sourcesPathList.extend(glob(os.path.join(self.path.get(), '*.jpeg')))
            sourcesPathList.extend(glob(os.path.join(self.path.get(), '*.bmp')))
    
            if not os.path.exists('./result'):
                os.mkdir('./result')
            try:
                if not sourcesPathList:
                    messagebox.showinfo('警告', '所选文件夹内容为空')
                    assert not '0'
                for p in sourcesPathList:
                    tmp = Image.open(p)
                    try:
                        res = tmp.resize([int(i) for i in self.size.get().split(',')], Image.ANTIALIAS)
                    except ValueError:
                        res = tmp.resize([int(i) for i in self.size.get().split(',')], Image.ANTIALIAS)
                    res.save(os.path.join('./result', p.split('\')[-1]), quality=95)
                messagebox.showinfo('提示', '修改成功')
            except (FileNotFoundError, AssertionError):
                messagebox.showinfo('警告', '修改失败')
    
    
    if __name__ == '__main__':
        app = Application()
        app.run()
    

    使用说明

    1、命令行 python formatPic.py

    2、
    3、点击路径选择(只需要选择图片文件所在的文件夹,推荐把要修改尺寸的图片都放到一个文件夹下)
    4、输入目标大小(100,200或者100,200,可以兼容中英文标点)
    5、点击确定选择
    6、修改成功或失败皆会有弹窗提示
    7、使用完毕点击退出程序即可关闭
    8、修改好尺寸的图片可以到脚本同路径下的 result 文件夹下查看

    【完】

  • 相关阅读:
    python学习笔记——拾
    python学习笔记——玖
    Python 实现栈与队列
    Vijos1774 机器翻译 [模拟]
    Vijos1788 第K大 [模拟]
    Python 序列求和
    HDU 2102 A计划 DFS与BFS两种写法 [搜索]
    Python 多组输入
    Python 文件读写
    HDU 2068 RPG错排 [错排公式]
  • 原文地址:https://www.cnblogs.com/dmcs95/p/13200344.html
Copyright © 2011-2022 走看看