美文网首页
PythonShowMeTheCode(0005): 改变图像尺

PythonShowMeTheCode(0005): 改变图像尺

作者: tyrone_li | 来源:发表于2016-08-22 20:44 被阅读0次

    1. 题目

    第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。

    2. 实现

    # -*- coding: utf-8 -*-
    import os
    import os.path
    from PIL import Image
    
    
    def resize_files(path, size):
        if path is None:
            print("plz input the path")
            return
        if not os.path.isdir(path):
            print("plz input the right path")
            return
    
        files = [os.path.join(path, x) for x in os.listdir(path) if os.path.splitext(x)[1] == ".jpg"]
        for file in files:
            img = Image.open(file)
            img.thumbnail(size)
            img.save(os.path.splitext(file)[0]+"1.jpg", "jpeg")
    
    def drop_generate_image(path):
        if path is None:
            print("plz input the path")
            return
        files = [os.path.join(path, x) for x in os.listdir(path) if "1.jpg" in os.path.split(x)[1]]
        for file in files:
            print(file)
            os.remove(file)
    
    
    if __name__ == "__main__":
        resize_files("C:/Users/Public/Pictures/Sample Pictures/", (1136, 640))
        drop_generate_image("C:/Users/Public/Pictures/Sample Pictures/")
    

    相关文章

      网友评论

          本文标题:PythonShowMeTheCode(0005): 改变图像尺

          本文链接:https://www.haomeiwen.com/subject/ulovsttx.html