起因是自己的一个生图项目把图片放到了 CLoudfare R2 上,不过上传之前没有做好压缩优化,导致有很多存量的图片很大,就做了这一个小工具将 cloudfare r2 上的图片批量压缩并上传覆盖,最后有一个压缩的 report 。
地址: https://github.com/zhangchenchen/reduce_cloudfare_image
效果大致如下:
Files to be processed:
+--------------------------------------------------------------+------+----------+----------+
| File Path | Format| Size(KB) | Size(MB) |
+==============================================================+======+==========+==========+
| themes/steampunk-game-community/steampunk-game-community.png | PNG | 664.5 KB | 0.65 MB |
+--------------------------------------------------------------+------+----------+----------+
| Total | 1 file| 664.5 KB | 0.65 MB |
+--------------------------------------------------------------+------+----------+----------+
Processing 1 file, total size 0.65 MB. Continue? (y/n): y
Processing images: 100%|████████████████████████████████████| 1/1 [00:06<00:00, 6.36s/it]
Image Compression Report:
+--------------------------------------------------------------+----------+----------+----------+-------+--------+
| File | Original | Compressed| Space | Ratio | Time |
| | Size | Size | Saved | | |
+==============================================================+==========+==========+==========+=======+========+
| themes/steampunk-game-community/steampunk-game-community.png | 664.5 KB | 529.1 KB | 135.3 KB | 20.4% | 2.7s |
+--------------------------------------------------------------+----------+----------+----------+-------+--------+
| Total | 664.5 KB | 529.1 KB | 135.3 KB | 20.4% | - |
+--------------------------------------------------------------+----------+----------+----------+-------+--------+
| Total(MB) | 0.65 MB | 0.52 MB | 0.13 MB | 20.4% | - |
+--------------------------------------------------------------+----------+----------+----------+-------+--------+
Processed 1 file
Total space saved: 0.13 MB
Average compression ratio: 20.4%