#!/usr/bin/env python # -*- coding: utf-8 -*- import os from PIL import Image import re def image_size_off(rootDir): for lists in os.listdir(rootDir): #須要什麼格式的圖片本身手動改改就行了 if lists[lists.rfind('.'):].lower() == '.jpg': path = os.path.join(rootDir, lists) im = Image.open(path) box = clipimage(im.size) region = im.crop(box) size = (50, 50) region.thumbnail(size, Image.ANTIALIAS) #這裏保存thumbnail之後的結果 region.save( os.path.join("/home/mingjunyang/uploadfiles/thub/", lists)) box = () #取寬和高的值小的那一個來生成裁剪圖片用的box #而且儘量的裁剪出圖片的中間部分,通常人攝影都會把主題放在靠中間的,個別藝術家有特殊的藝術需求我顧不上 def clipimage(size): width = int(size[0]) height = int(size[1]) box = () if (width > height): dx = width - height box = (dx / 2, 0, height + dx / 2, height) else: dx = height - width box = (0, dx / 2, width, width + dx / 2) return box def main(): '''這裏輸入的參數是圖片文件的位置''' image_size_off("/home/mingjunyang/uploadfiles/") if __name__ == '__main__': main()
需求是這樣的:個人相冊裏面有不少的圖片,大小不一的.相冊的導航欄須要放50*50的縮略圖,並且實際需求裏面縮略圖的文件名還有特殊要求,因而本身來生成縮略圖啦!
python