当前位置:首页 > DayDayUp > 正文内容

大华人脸照片处理(缩小到小于100KB)

Luz1个月前 (10-16)DayDayUp230
#coding:utf-8
from PIL import Image
import sys
import time
import os
def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    #print(file)
    return size / 1024
def exif_transpose(img):
    if not img:
        return img

    exif_orientation_tag = 274

    # Check for EXIF data (only present on some files)
    if hasattr(img, "_getexif") and isinstance(img._getexif(), dict) and exif_orientation_tag in img._getexif():
        exif_data = img._getexif()
        orientation = exif_data[exif_orientation_tag]

        # Handle EXIF Orientation
        if orientation == 1:
            # Normal image - nothing to do!
            pass
        elif orientation == 2:
            # Mirrored left to right
            img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 3:
            # Rotated 180 degrees
            img = img.rotate(180)
        elif orientation == 4:
            # Mirrored top to bottom
            img = img.rotate(180).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 5:
            # Mirrored along top-left diagonal
            img = img.rotate(-90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 6:
            # Rotated 90 degrees
            img = img.rotate(-90, expand=True)
        elif orientation == 7:
            # Mirrored along top-right diagonal
            img = img.rotate(90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 8:
            # Rotated 270 degrees
            img = img.rotate(90, expand=True)

    return img

def compress_image(infile, outfile='', mb=90, step=3, quality=50):
    o_size = get_size(infile)
    print(o_size)
    quality=int((mb/o_size)*100)
    print(quality)
    if o_size <= mb:
        return infile
    #outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im=exif_transpose(im)
        im.save(infile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(infile)
    im.save(infile)
def resize_image(infile, outfile='', x_s=50):
    im = Image.open(infile)
    im=exif_transpose(im)
    x, y = im.size
    print(x,y)
    y_s = int(y * x_s / x)
    out = im.resize((x_s, y_s), Image.ANTIALIAS)
    #outfile = get_outfile(infile, outfile)
    out.save(infile)
#if __name__ == '__main__':
print("=========================JPG-Changer=========================")
print("========================Luz 2021-9-17========================")
jpg_path=os.path.dirname(os.path.realpath(sys.argv[0]))
print("path:"+jpg_path)
print("please check path")

for i in range(5):
    print("run in "+str(5-i)+" seconds")
    time.sleep(1)
print("=========================start task==========================")
jpg_names=[name for name in os.listdir(jpg_path)]
geshis=['jpg']
for i in jpg_names:
    geshi=i.split('.')[-1]
    geshi=geshi.lower()
    for a in geshis:
        if(geshi==a):
            while(1):
                try:
                    resize_image(jpg_path+'\\'+i)
                    time.sleep(0.1)
                    compress_image(jpg_path+'\\'+i)
                    print(i)
                    break
                except:
                    print(i)
    #print(i)
print("========================= complete!==========================")

放置到照片所在文件夹内运行脚本

分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。