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/")