[ol]
[/ol]
示意.gif (69.68 KB, 下载次数: 0)
下载附件
2023-4-13 10:56 上传
下载地址
https://wwqt.lanzoum.com/ikx1N0sv7p9g
密码:52pj
用python写的,没啥技术含量
# -*- coding: utf-8 -*-
# @Time : 2023/4/12 17:37
# @AuThor : Ryan
# @file : tiny_main.py
import sys
import os
import tinify
import msvcrt
import tempfile
from cert import CERT_DATA
# 解决打包后缺少CA证书问题
temp_cert = tempfile.NamedTemporaryFile(delete=False, encoding="utf-8", mode="w")
temp_cert.write(CERT_DATA)
temp_cert.flush()
os.environ['REQUESTS_CA_BUNDLE'] = temp_cert.name
KEY_FILENAME = "key.txt"
DOCUMENTS_PATH = os.path.join(os.path.expanduser("~"), "Documents")
filename = sys.argv[1]
def showerror():
while True:
if msvcrt.getch():
return
def main():
key_path = os.path.join(DOCUMENTS_PATH, KEY_FILENAME)
if os.path.exists(key_path):
with open(key_path, "r") as f:
tinify.key = f.read().strip()
else:
key = input("请输入您的tinify_KEY:")
with open(key_path, "w") as f:
f.write(key)
tinify.key = key
print("正在压缩图片...")
try:
source = tinify.from_file(filename)
optimized_filename = os.path.splitext(filename)[0] + "_压缩图片" + os.path.splitext(filename)[1]
source.to_file(optimized_filename)
except tinify.errors.AccountError:
print("API错误或上限,请检查tinyfy key!\n我的电脑-文档-key,进行修改\n按任意键关闭程序...")
showerror()
except tinify.errors.ClientError:
print("文件格式不正确!!!\n按任意键关闭程序...")
showerror()
except :
print("网络连接失败,请稍后重试!\n按任意键关闭程序...")
showerror()
if __name__ == '__main__':
main()