summaryrefslogtreecommitdiff
path: root/ATRI/utils/img.py
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-02-06 00:32:26 +0800
committerKyomotoi <[email protected]>2021-02-06 00:32:26 +0800
commitf5ceb8927f2e7f2a9e29d62c8e4cef876f917249 (patch)
tree40b9dcd6b7d3db486054e3aa9b5a04d25fa2284e /ATRI/utils/img.py
parenteb52fab79ada7efe6191e3a5f90179766feaded0 (diff)
downloadATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.tar.gz
ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.tar.bz2
ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.zip
🏗 💩 更改项目结构,修复啥b BUG
Diffstat (limited to 'ATRI/utils/img.py')
-rw-r--r--ATRI/utils/img.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/ATRI/utils/img.py b/ATRI/utils/img.py
new file mode 100644
index 0000000..68a44fc
--- /dev/null
+++ b/ATRI/utils/img.py
@@ -0,0 +1,23 @@
+import os
+from PIL import ImageFile, Image
+
+from ATRI.exceptions import WriteError
+
+
+def compress_image(out_file, kb=500, quality=85, k=0.9) -> str:
+ """将目标图片进行压缩"""
+ o_size = os.path.getsize(out_file) // 1024
+ if o_size <= kb:
+ return out_file
+
+ ImageFile.LOAD_TRUNCATED_IMAGES = True # type: ignore
+ while o_size > kb:
+ img = Image.open(out_file)
+ x, y = img.size
+ out = img.resize((int(x * k), int(y * k)), Image.ANTIALIAS)
+ try:
+ out.save(out_file, quality=quality)
+ except WriteError:
+ raise WriteError('Writing file failed!')
+ o_size = os.path.getsize(out_file) // 1024
+ return out_file