跳至主要內容
压缩图片
import os
from PIL import Image


# 获取图片文件大小
def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024


# 拼接输出文件地址
def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile


# 压缩图片方法(配置即可用_1007)
def compress_image(infile, outfile='', mb=600, step=10, quality=80):
    # 判断该张图片是否压缩
    if os.path.exists(outfile):
        return
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标,KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址,压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        return infile
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)


# 扩展压缩
a = r"C:\Users\DM\Desktop\图片过长\新建文件夹\1"
b = r"C:\Users\DM\Desktop\图片过长\新建文件夹\2"
for i in os.listdir(a):
    in_file = os.path.join(a, i)
    out_file = os.path.join(b, i)
    print(in_file, out_file)
    compress_image(in_file, out_file)

爱喝水的木子...小于 1 分钟codepython压缩图片
图片大小压缩
from PIL import Image
import os
from multiprocessing.pool import ThreadPool

# 获取图片文件大小
def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024


# 拼接输出文件地址
def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile


# 压缩文件到指定大小
def compress_image(infile, outfile='', mb=500, step=10, quality=80):
    # 判断该张图片是否压缩
    if os.path.exists(outfile):
        return
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标,KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址,压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        shutil.copyfile(infile,outfile)
        return f"cp:{infile}>{outfile}"
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)


# 默认600k,但是可能不会到600K,有的压缩不到
path_a = r"文件路径"
path_b = r"文件路径"
# 使用线程池提高速度
s = ThreadPool(12)
for i in os.listdir(path_a):
    infile = os.path.join(path_a, i)
    outfile = os.path.join(path_b, i)
    # if os.path.exists(outfile):
    #     continue
    print(infile)
    s.apply_async(compress_image, args=(infile, outfile))
    # compress_image(infile,outfile)
s.close()
s.join()


爱喝水的木子...小于 1 分钟codepython图片压缩
图片旋转摆正根据图片的exif信息
from PIL import Image
import os


# 图片旋转
def image_route(in_file, out_file):
    img = Image.open(in_file)
    dict_exif = img.getexif()
    try:
        res = dict_exif[274]
        if res == 1:
            img = img.rotate(0, expand=True)
        elif res == 8:
            print("向右", in_file)
            img = img.rotate(90, expand=True)
        elif res == 6:
            print("向左", in_file)
            img = img.rotate(-90, expand=True)
        elif res == 3:
            print("翻转", in_file)
            img = img.rotate(180, expand=True)
        else:
            print(res, "不正常", in_file)
            img = img.rotate(0, expand=True)
        img.save(out_file)
    except:
        print("error:", in_file)
        img = img.rotate(0, expand=True)
        img.save(os.path.join(_save, i))


if __name__ == '__main__':
    # 要调整的路径
    path = r"文件路径"
    # 调整之后保存的路径
    _save = r"文件路径"
    for i in os.listdir(path):
        in_file = os.path.join(path, i)
        out_file = os.path.join(_save, i)
        image_route(in_file, out_file)


爱喝水的木子...小于 1 分钟pythonpython图片旋转exif