Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 oxipng 优化 iconBitmap,减少文件大小 #540

Merged
merged 6 commits into from
Jan 18, 2025

Conversation

lns103
Copy link
Contributor

@lns103 lns103 commented Jan 18, 2025

使用了 oxipng 对于PNG图片数据进行优化,使用参数为-o max --zopfli --alpha --strip all
json文件总大小由1709KB减小到1014KB(缩小40%)
其中 Blued(com.soft.blued)、快影(com.kwai.videoeditor) 和 小米互传(com.miui.mishare.connectivity) 为webp格式,都已手动优化。

lns103 and others added 5 commits January 18, 2025 19:14
Blued(com.soft.blued) and 快影(com.kwai.videoeditor) failed
小米互传(com.miui.mishare.connectivity) failed
之前为webp格式
替换之前的webp格式图标
@lns103 lns103 force-pushed the lns103-patch-oxipng branch from dea7b13 to 7dde3a9 Compare January 18, 2025 11:17
@lns103
Copy link
Contributor Author

lns103 commented Jan 18, 2025

手动优化的几个app图标
Screenshot_2025-01-18-19-37-52-19_1584f1d8cc4c3efa14862223b41fd22e
Screenshot_2025-01-18-19-38-23-41_1584f1d8cc4c3efa14862223b41fd22e
Screenshot_2025-01-18-19-38-53-71_1584f1d8cc4c3efa14862223b41fd22e

@lns103 lns103 marked this pull request as ready for review January 18, 2025 11:41
@lns103
Copy link
Contributor Author

lns103 commented Jan 18, 2025

这是我的处理脚本,如果担心有问题可以自己跑一遍

import base64
import json
import subprocess

# 输入文件和输出文件路径
input_file = ''
output_file = ''

# 读取 JSON 文件
with open(input_file, 'r', encoding='utf-8') as f:
    data = json.load(f)

# 处理每个 iconBitmap
for item in data:
    if 'iconBitmap' in item:
        # 解码 Base64 数据
        icon_data = base64.b64decode(item['iconBitmap'])

        try:
            # 调用 oxipng 通过 stdin 和 stdout 优化 PNG
            result = subprocess.run(
                ['oxipng', '-o', 'max', '--zopfli', '--alpha', '--strip', 'all', '-'],
                input=icon_data,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                check=True
            )
            optimized_data = result.stdout
            # 重新编码为 Base64
            item['iconBitmap'] = base64.b64encode(optimized_data).decode('utf-8')
        except subprocess.CalledProcessError as e:
            print(f"Error optimizing icon for {item.get('appName', 'unknown')}:\n{e.stderr.decode()}")
            continue

# 将结果写入新 JSON 文件
with open(output_file, 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

print(f"优化后的 JSON 文件已保存到: {output_file}")

@fankes fankes merged commit ac8696f into fankes:main Jan 18, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants