企业网站优化,简网app工场体验,办公类网站开发背景,抚州市临川区建设局网站小编最近有一篇png图片要批量压缩#xff0c;大小都在5MB之上#xff0c;在网上找了半天要么就是有广告#xff0c;要么就是有毒#xff0c;要么就是功能复杂#xff0c;整的我心烦意乱。
于是我自己用python写了一个纯净工具#xff0c;只能压缩png图片#xff0c;没任…小编最近有一篇png图片要批量压缩大小都在5MB之上在网上找了半天要么就是有广告要么就是有毒要么就是功能复杂整的我心烦意乱。
于是我自己用python写了一个纯净工具只能压缩png图片没任何广告。在windwos平台上使用。
指定的压缩质量40 压缩前图片大小 压缩后图片大小 可以看到图片从5MB压缩到了 500KB。指定的质量还是降低压缩尺寸效果还可以更大。
压缩前和压缩后图片对比
压缩前大小5.7MB 压缩后大小861KB 使用视频教程 png压缩 代码采用python编写打包成了exe 文件目录 直接运行这个 批量压缩png图片.exe 即可。
下载地址
https://gitee.com/lz-code/soft.git
部分代码展示
import tkinter as tk
from tkinter.filedialog import askdirectory
from tkinter.messagebox import *
import datetime
import _thread
from pathlib import Path
import osdef center_window(root, width, height):# 获取屏幕宽高screen_width root.winfo_screenwidth()screen_height root.winfo_screenheight()# 计算窗口左上角坐标x (screen_width - width) // 2y (screen_height - height) // 2# 设置窗口位置root.geometry(f{width}x{height}{x}{y})def count_files_with_extension(folder_path, extension1 , extension2):count 0path Path(folder_path)# 获取路径下的所有文件并打印文件名称for f in path.iterdir():if f.is_file():full_path os.path.join(folder_path, f)source_name full_pathif source_name.endswith(extension1) or source_name.endswith(extension2):count 1return countdef open_img_dir(edit):path_ askdirectory() # 使用askdirectory()方法返回文件夹的路径if path_ :pass# showerror(错误, 图片目录未选择)else:edit.insert(0,path_)def start_compress(entry_dir,entry_quality):img_dir entry_dir.get()img_quality entry_quality.get()if img_dir.strip() :showerror(错误, 图片目录未选择)returnif img_quality.strip() :showerror(错误, 压缩质量未设置)returntry:img_quality int(img_quality)except:showerror(错误, 压缩质量只能是整数)returnif img_quality 0 or img_quality 100:showerror(错误, 压缩质量在0-100之间)returndef run(img_dir,img_quality):try:## 输出路径out_dir img_dir/outappend_log(创建输入路径: out_dir)if not os.path.exists(out_dir):os.mkdir(out_dir)path Path(img_dir)count 0total count_files_with_extension(img_dir,png,PNG)btn_start.config(text压缩中 0/str(total))btn_start.config(statetk.DISABLED)# 获取路径下的所有文件并打印文件名称for f in path.iterdir():if f.is_file():full_path os.path.join(img_dir, f)source_name full_pathappend_log(作业压缩完成存储 out_dir)btn_start.config(statetk.NORMAL)btn_start.config(text开始压缩)showinfo(提示, 恭喜 图片压缩完成)except Exception as e:import logginglogging.exception(e)append_log(str(e))btn_start.config(statetk.NORMAL)btn_start.config(text开始压缩)_thread.start_new_thread(run , (img_dir,img_quality))def append_log(log):current_time datetime.datetime.now().strftime(%Y-%m-%d-%H:%M:%S)log [current_time]# log \ntext.insert(tk.END, log \n)text.see(tk.END)root tk.Tk()
root.title(批量PNG压缩 by 【轻量小工具工作室*QQ:3571289092】)
window_width 500
window_height 500
center_window(root, window_width, window_height)uiHeight 30
padding 20hint tk.Label(root, text ★★★ 无收费无广告无毒自主研发联系开发可定制 ★★★)
hint.place(xpadding, y0, width400, heightuiHeight)hint2 tk.Label(root, text压缩质量)
hint2.place(xpadding, yuiHeightpadding, width100, heightuiHeight)entry_qualitytk.Entry(root,bd2)
entry_quality.place(xpadding100, yuiHeightpadding, width100, heightuiHeight)
entry_quality.insert(0,40)hint2 tk.Label(root, text0[差]-100[好])
hint2.place(x2*padding2*100, yuiHeightpadding, width80, heightuiHeight)hint2 tk.Label(root, textPNG图片路径 )
hint2.place(xpadding, y2*uiHeight2*padding, width100, heightuiHeight)entry_dirtk.Entry(root,bd2)
entry_dir.place(xpadding100, y2*uiHeight2*padding, width300, heightuiHeight)btn tk.Button(root, text 选择..., commandlambda :open_img_dir(entry_dir))
btn.place(x2*padding100300, y2*uiHeight2*padding, width50, heightuiHeight)btn_start tk.Button(root, text 开始压缩, commandlambda :start_compress(entry_dir,entry_quality))
btn_start.place(xpadding100, y3*uiHeight3*padding, width150, heightuiHeight)## 滚动的日志text tk.Text(root , bgblack, fgwhite)
scrollbar tk.Scrollbar(root, commandtext.yview)
scrollbar.pack(sidetk.RIGHT, filltk.Y)
scrollbar.place(x500-padding, y4*uiHeight4*padding, width10, heightuiHeight)
text.place(xpadding, y4*uiHeight4*padding, width500-2*padding, height280)root.mainloop()