-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImageCompressor.py
370 lines (323 loc) · 14.2 KB
/
ImageCompressor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import os
import platform
import shutil
import subprocess
import tempfile
import time
import warnings
import uuid
from io import BytesIO
from pathlib import Path
import click
import mozjpeg_lossless_optimization
from PIL import Image
class QualityInteger(click.ParamType):
name = "QualityInteger"
@staticmethod
def _parse_int(value):
try:
return int(value)
except ValueError:
return None
def convert(self, value, param, ctx):
"""
Convert a string to an integer or an integer range.
:param value: The value to convert.
:type value: str
:param param: The parameter.
:type param: XXX
:param ctx: The context.
:type ctx: XXX
:return: The converted integer or integer range.
:rtype: int or tuple[int, int]
"""
if value.isdigit():
return self._parse_int(value)
parts = value.split('-')
if len(parts) != 2:
raise click.BadParameter(f'"{value}": The parameter does not conform to the format like 80-90 or 90')
min_v, max_v = map(self._parse_int, parts)
if min_v is None or max_v is None or min_v > max_v or min_v <= 0 or max_v <= 0:
raise click.BadParameter(f'"{value}": The parameter does not conform to the format like 80-90 or 90')
return min_v, max_v
def generate_output_path(fp, output=None):
"""
Generate the output path.
:param fp: The file path.
:type fp: Path
:param output: The output path.
:type output: Path or None
:return: The output path.
:rtype: Path
"""
uuid_str = get_uuid("".join(["AGPicCompress", str(time.time()), fp.name]))
new_fp = Path(fp.parent, f"{fp.stem}_{uuid_str}_compressed{fp.suffix}")
if output:
if output.is_dir():
# If it is a directory, check if it exists, create it if it doesn't
output.mkdir(parents=True, exist_ok=True)
new_fp = output / new_fp.name
elif output.exists():
# If it is a file, check if it exists, throw an exception if it does
raise FileExistsError(f'"{output}": already exists')
else:
new_fp = output
return new_fp
def optimize_output_path(fp, output=None, force=False):
"""
Optimize the output path.
:param fp: File path.
:type fp: Path
:param output: Output path.
:type output: Path
:param force: Whether to force overwrite.
:type force: bool
:return: Output path.
:rtype: Path
"""
if force and output:
if output.is_dir():
output.mkdir(parents=True, exist_ok=True)
new_fp = output / fp.name
else:
new_fp = output
elif not force and output:
new_fp = generate_output_path(fp, output)
else:
new_fp = generate_output_path(fp)
return new_fp
def find_pngquant_cmd():
"""
Find and return the executable file path of pngquant.
:return: The executable file path of pngquant, or None if not found.
:rtype: str or None
"""
pngquant_cmd = shutil.which('pngquant')
if pngquant_cmd:
return pngquant_cmd
exe_extension = '.exe' if os.name == 'nt' else ''
search_paths = [Path(__file__).resolve().parent, Path(__file__).resolve().parent / 'ext']
for search_path in search_paths:
pngquant_exe_path = search_path / f'pngquant{exe_extension}'
if pngquant_exe_path.exists():
return str(pngquant_exe_path)
return None
def get_uuid(name):
"""
Get the UUID string of the specified string.
:param name: The name string for UUID generation.
:type name: str
:return: The UUID string generated based on the specified name.
:rtype: str
"""
return str(uuid.uuid3(uuid.NAMESPACE_DNS, name))
class ImageCompressor:
def __init__(self):
pass
@staticmethod
def compress_image(fp, force=False, quality=None, output=None, webp=False):
"""
Compression function.
:param fp: File name or directory name.
:type fp: Path
:param force: Whether to overwrite if a file with the same name exists.
:type force: bool
:param quality: Compression quality. 80-90, or 90.
:type quality: int or tuple[int, int]
:param output: Output path or output directory
:type output: Path
:param webp: Whether to convert to WebP format.
:type webp: bool
"""
# Check if the file exists
if not fp.exists():
raise FileNotFoundError(f'"{fp}": Path or directory does not exist')
if output:
if not output.is_dir():
if output.suffix == '':
output.mkdir(parents=True, exist_ok=True)
elif output.suffix.lower() not in ['.png', '.jpg', '.jpeg', '.webp']:
raise ValueError(f'"{output.name}": Unsupported output file format')
elif output.suffix.lower() == '.webp':
webp = True
output = output.with_name(f"{output.stem}_2webp{fp.suffix}")
if output.suffix.lower() != fp.suffix.lower():
raise ValueError('Inconsistent output file format with input file format')
if fp.is_dir():
for file in fp.iterdir():
if file.is_file() and file.suffix.lower() in ['.png', '.jpg', '.jpeg']:
ImageCompressor.compress_image(file, force, quality, output, webp)
return
ext = fp.suffix.lower()
if ext == '.png':
ImageCompressor._compress_png(fp, force, quality, output, webp)
elif ext in ['.jpg', '.jpeg']:
ImageCompressor._compress_jpg(fp, force, quality, output, webp)
else:
raise ValueError(f'"{fp.name}": Unsupported output file format')
@staticmethod
def _convert_to_webp(fp):
"""
Convert an image to WebP format.
:param fp: Image file path.
:type fp: Path
:return: WebP image file path.
:rtype: Path or None
"""
# Check if the file exists
if Path(fp).exists():
img = Image.open(fp)
webp_fp = Path(fp).with_suffix('.webp')
img.save(webp_fp, 'webp')
# Delete the original image file
os.remove(fp)
return webp_fp
return None
@staticmethod
def _compress_png(fp, force=False, quality=None, output=None, webp=False):
"""
Compress PNG images and specify compression quality.
:param fp: Path of the image file.
:type fp: Path
:param force: Whether to overwrite if a file with the same name exists. Defaults to False.
:type force: bool
:param quality: Compression quality. Defaults to None.
:type quality: int or tuple[int, int]
:param output: Output path.
:type output: Path
:param webp: Whether to convert to WebP format.
:type webp: bool
"""
new_fp = optimize_output_path(fp, output, force)
quality_command = f'--quality {quality}' if isinstance(quality, int) else f'--quality {quality[0]}-{quality[1]}' if isinstance(quality, tuple) else ''
pngquant_cmd = find_pngquant_cmd()
if not pngquant_cmd:
raise FileNotFoundError('pngquant not found. Please ensure pngquant is installed or added to the environment variable')
command = f'{pngquant_cmd} {fp} --skip-if-larger -f -o {new_fp} {quality_command}'
subprocess.run(command, shell=True, check=True)
if not new_fp.exists():
warnings.warn(f'"{fp}": The compressed image file was not generated successfully. It may no longer be compressible or no longer exist', Warning)
return
if webp:
ImageCompressor._convert_to_webp(new_fp)
@staticmethod
def _compress_jpg(fp, force=False, quality=None, output=None, webp=False):
"""
Compress JPG images and specify compression quality.
:param fp: Image file path.
:type fp: Path
:param force: Whether to overwrite if a file with the same name already exists, default is False.
:type force: bool
:param quality: Compression quality, default is None.
:type quality: int or None
:param output: Output path.
:type output: Path
:param webp: Whether to convert to WebP format.
:type webp: bool
"""
if quality is not None and not isinstance(quality, int):
raise ValueError(f'"{quality}": Unsupported type for quality parameter')
new_fp = optimize_output_path(fp, output, force)
with Image.open(fp) as img:
img = img.convert("RGB")
with BytesIO() as buffer:
img.save(buffer, format="JPEG", quality=quality if quality else 75)
input_jpeg_bytes = buffer.getvalue()
optimized_jpeg_bytes = mozjpeg_lossless_optimization.optimize(input_jpeg_bytes)
with open(new_fp, "wb") as output_jpeg_file:
output_jpeg_file.write(optimized_jpeg_bytes)
if not new_fp.exists():
warnings.warn(f'"{fp}": The compressed image file was not generated successfully. It may no longer be compressible or no longer exist', Warning)
return
if webp:
ImageCompressor._convert_to_webp(new_fp)
@staticmethod
def compress_image_from_bytes(image_bytes, quality=80, output_format='JPEG', webp=False):
"""
Compresses image data and returns the compressed image data.
:param image_bytes: The byte representation of the image data.
:type image_bytes: bytes
:param quality: The compression quality, ranging from 1 to 100.
:type quality: int
:param output_format: The output format of the image, default is 'JPEG'.
:type output_format: str
:param webp: Whether to convert to WebP format.
:type webp: bool
:return: The byte representation of the compressed image data.
:rtype: bytes
"""
# Load the image data into a PIL image object in memory
with BytesIO(image_bytes) as img_buffer:
img = Image.open(img_buffer).convert('RGB')
output_buffer = BytesIO()
if output_format.upper() == 'JPEG':
img.save(output_buffer, format=output_format, quality=quality, optimize=True)
compressed_img_bytes = output_buffer.getvalue()
compressed_img_bytes = mozjpeg_lossless_optimization.optimize(compressed_img_bytes)
if webp:
output_buffer = BytesIO()
img = Image.open(BytesIO(compressed_img_bytes))
img.save(output_buffer, format='webp')
compressed_img_bytes = output_buffer.getvalue()
elif output_format.upper() == 'PNG':
with tempfile.TemporaryDirectory() as temp_dir:
temp_png_file_path = Path(temp_dir) / f'temp_{get_uuid(f"AGPicCompress{time.time()}")}.png'
with open(temp_png_file_path, 'wb') as temp_png_file:
temp_png_file.write(image_bytes)
new_fp = optimize_output_path(temp_png_file_path, Path(temp_dir), False)
pngquant_cmd = find_pngquant_cmd()
if not pngquant_cmd:
raise FileNotFoundError('pngquant not found. Please ensure pngquant is installed or added to the environment variable')
quality_command = f'--quality {quality}' if isinstance(quality, int) else f'--quality {quality[0]}-{quality[1]}' if isinstance(quality, tuple) else ''
command = f'{pngquant_cmd} {temp_png_file_path} --skip-if-larger -f -o {new_fp} {quality_command}'
subprocess.run(command, shell=True, check=True)
if new_fp.exists():
with open(new_fp, 'rb') as compressed_img_file:
compressed_img_bytes = compressed_img_file.read()
else:
warnings.warn('The compressed image file was not generated successfully. It may no longer be compressible or no longer exist', Warning)
return None
if webp:
output_buffer = BytesIO()
img = Image.open(BytesIO(compressed_img_bytes))
img.save(output_buffer, format='webp')
compressed_img_bytes = output_buffer.getvalue()
else:
raise ValueError(f'"{output_format}": Unsupported output file format')
return compressed_img_bytes
@staticmethod
@click.command()
@click.argument('fp')
@click.option(
"--force", "-f", "--violent",
is_flag=True,
help="Whether to overwrite if a file with the same name exists, defaults to False."
)
@click.option('--quality', "-q", default="80", type=QualityInteger(),
help="Compression quality. 80-90, or 90, default is 80.")
@click.option('--output', '-o', help='Output path or output directory.')
@click.option('--webp', is_flag=True, help='Convert images to WebP format, default is False.')
def cli_compress(fp, force=False, quality=None, output=None, webp=False):
"""
Compress images via command line.
:param fp: Image file path or directory path.
:type fp: str
:param force: Whether to overwrite if a file with the same name exists, defaults to False.
:type force: bool
:param quality: Compression quality. 80-90, or 90, default is 80.
:type quality: int or tuple[int, int]
:param output: Output path or output directory.
:type output: str
:param webp: Convert images to WebP format, default is False.
:type webp: bool
"""
if not fp:
raise ValueError(f'"{fp}": The file path or directory cannot be empty')
fp_path = Path(fp)
if output:
output_path = Path(output) if len(output) > 0 else None
ImageCompressor.compress_image(fp_path, force, quality, output_path, webp)
return
if __name__ == "__main__":
ImageCompressor.cli_compress()
# ImageCompressor.compress_image(Path('./images/'), force=False, quality=80, output=Path('./images/'))