github/Show me the code (5)

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

from PIL import Image
import os

IPHONE_5 = (1136, 640)
PIC_DIR = '/home/hqi/source_code/python/show_me_the_code'

def change_resolution(pic_path, resolution):
    img = Image.open(pic_path)
    x, y = img.size
    factor_x = float(x) / resolution[0]
    factor_y = float(y) / resolution[1]

    new_x = x if factor_x < 1 else resolution[0]
    new_y = y if factor_y < 1 else resolution[1]

    img.resize((new_x, new_y)).save('change_'+pic_path)

def filter_pic(_dir):
    files = os.listdir(_dir)
    for file in files:
        if file.endswith('.jpg'):
            change_resolution(file, IPHONE_5)


if __name__ == '__main__':
    #change_resolution('pet.jpg', IPHONE_5)
    filter_pic(PIC_DIR)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容