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

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

Luz12个月前 (10-16)DayDayUp1673
#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!==========================")

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

相关文章

linux 常用tcpdump 抓包命令

默认抓包(抓取网络接口1上的所有数据包)tcpdump指定网卡(eth1:网卡名)tcpdump -i eth1指定主机(name:主机名,1.1.1.1:主机ip,抓取进入/离开指定主机的包;src:发送,dst:接收...

Activemq端口放行(activemq报错Transport Connection to: tcp://XXX failed: java.io.EOFExcepti)

Activemq端口放行(activemq报错Transport Connection to: tcp://XXX failed: java.io.EOFExcepti)

发现有的activemq部署后局域网内其他机子连接日志报错为2021-08-06 16:21:49,499 | WARN  | Transport Connection&...

斐波那契数列 前2000个数

偶尔有脑洞题需要用到,算了一些方便查询计算脚本:fib_recur1=[0]*500 fib_recur1[-1]=0 fib_recur1[0]=1 fib_recur1[1]=1 def fib_recur(n):...

redis未授权访问漏洞复现

redis未授权访问漏洞复现

环境搭建靶机:wget http://download.redis.io/releases/redis-2.8.17.tar.gz  //服务下载 tar xzfredis-2.8.17.tar.g...

Microsoft Compatibility Telemetry进程关闭

Microsoft Compatibility Telemetry进程关闭

没什么用的进程,主要负责向微软传送系用户数据并占用大量系统资源。关闭方法打开任务计划程序在  任务计划程序库--Microsoft--Windows--Application Experience中禁用即可...

b站批量取消关注

console代码:$(".be-pager-next:contains('下一页')").click();$(".be-dropdown-item:contains('取消关注'...

发表评论

访客

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