Python处理图片压缩

编程入门 行业动态 更新时间:2024-10-09 00:50:23

Python处理<a href=https://www.elefans.com/category/jswz/34/1770705.html style=图片压缩"/>

Python处理图片压缩

        继上次女朋友用五块钱加瓶可乐,换我一个抽奖程序后,她她她,又来了。

        本着吃瓜第一,干饭第二的优秀原则,我选择了拒绝。毕竟最近的瓜瓜太好吃了,一会是ZS和ZH的代孕风波,虽然她抽烟、喝酒、出轨、耍大牌,但是她是一个好女孩呀。一会又是天王ZJL和上海乘务长的激情故事。刺激,太刺激了。哪来的空再去做需求。我今天就是死,从这跳下去,也不做女朋友的廉价需求。

        来吧,看看需求。啥,你说啥,啥男儿尊严?男子汉能屈能伸,眼睛有点近视,听不清你在说啥。毕竟这年头骗个女朋友不容易。

        需求:要对很多图片进行压缩处理,量太大,用PS累死人。

        来吧,废话不多说,翠花,上酸菜。

        文件目录:

               文件夹in_pictures : 放图片的目录                文件夹out_pictures : 输出图片的目录               config.xml : 配置文件        

        config.xml       指定输入、输出目录,以及压缩到文件的大小

<?xml version="1.0"?>
<config><in_path>in_pictures</in_path><out_path>out_pictures</out_path><end_size>10240</end_size> <!--kb-->
</config>

       pic_compress.py    压缩代码

# -*- coding: utf-8 -*-from PIL import Image
import os
from xml.dom.minidom import parsein_path = ''
out_path = ''
end_size = 1024succ_count = 0
all_count = 0def get_inpictures() :all_pictures = []end_path = os.getcwd() + '\\' + in_pathg = os.walk(end_path)for _, _, filelist in g :for filename in filelist :if filename.endswith('jpg') or filename.endswith('png') or \filename.endswith('jpeg') :all_pictures.append(filename)global all_countall_count = len(all_pictures)return all_pictures# 读XML
def read_xml() :doc = parse('./config.xml')root = doc.documentElementglobal in_path, out_path, end_sizein_path = root.getElementsByTagName('in_path')[0].firstChild.dataout_path = root.getElementsByTagName('out_path')[0].firstChild.dataend_size = int(root.getElementsByTagName('end_size')[0].firstChild.data)def do_compress(in_pic) :global succ_countquality = 100step = 10in_pic_name = os.getcwd() + '\\' + in_path + '\\' + in_pic #输入图片out_pic_name = os.getcwd() + '\\' + out_path + '\\' + in_pic #输出图片#print(in_pic_name, out_pic_name)im = Image.open(in_pic_name)im.save(out_pic_name, quality=quality)o_size = os.path.getsize(out_pic_name) / 1024 # K为单位succ_flag = Truewhile o_size > end_size :quality -= stepif quality <= 0 :print('error !!!  file:', in_pic_name)succ_flag = Falsebreak#print('size  ', o_size, '  step  ', step, '  quality  ', quality)im = Image.open(in_pic_name)im.save(out_pic_name, quality=quality) # quality表示压缩比率o_size = os.path.getsize(out_pic_name) / 1024if succ_flag :succ_count += 1# 压缩
def compress(all_pictures) :for pic in all_pictures :do_compress(pic)if all_count :print('success !!!  all_pictures_num: %d, succ_num: %d' %(all_count, succ_count))# 清空目标目录
def clear_out_path() :path = os.getcwd() + '\\' + out_pathfor file in os.listdir(path) :file_data = path + '\\' + fileif os.path.isfile(file_data) :os.remove(file_data)def main() :read_xml()clear_out_path()all_pictures = get_inpictures()compress(all_pictures)
main()

       注意,并不是简单的压缩,如果你的输入文件大小远小于要压缩的目标大小,会进行部分扩大。

更多推荐

Python处理图片压缩

本文发布于:2024-02-06 13:41:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1749461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图片   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!