Coverage for pyguymer3/image/optimise_image.py: 77%
22 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def optimise_image(
5 fname,
6 /,
7 *,
8 chunksize = 1048576,
9 debug = __debug__,
10 exiftoolPath = None,
11 gifsiclePath = None,
12 jpegtranPath = None,
13 optipngPath = None,
14 pool = None,
15 strip = False,
16 timeout = 60.0,
17):
18 """
19 Please read the documentation for the four functions: "exiftool",
20 "gifsicle", "jpegtran" and "optipng". It is not safe to keep on running the
21 programs "gifsicle" and "jpegtran" on images, but it is safe to keep on
22 running all of my wrapper functions on images instead.
24 chunksize : int, optional
25 the size of the chunks of any files which are read in (in bytes)
26 """
28 # Import standard modules ...
29 import multiprocessing
30 import multiprocessing.pool
31 import os
33 # Import sub-functions ...
34 from .exiftool import exiftool
35 from .gifsicle import gifsicle
36 from .jpegtran import jpegtran
37 from .optipng import optipng
39 # Check that the image exists ...
40 if not os.path.exists(fname):
41 raise Exception(f"\"{fname}\" does not exist") from None
43 # Extract file extension ...
44 ext = os.path.splitext(fname)[1]
46 # Optimise image depending the file extension ...
47 match ext.lower():
48 case ".gif":
49 gifsicle(
50 fname,
51 chunksize = chunksize,
52 debug = debug,
53 gifsiclePath = gifsiclePath,
54 timeout = timeout,
55 )
56 case ".jpg" | ".jpeg":
57 jpegtran(
58 fname,
59 chunksize = chunksize,
60 debug = debug,
61 jpegtranPath = jpegtranPath,
62 timeout = timeout,
63 )
64 case ".png":
65 optipng(
66 fname,
67 optipngPath = optipngPath,
68 pool = pool,
69 timeout = timeout,
70 )
71 case _:
72 # Crash ...
73 raise ValueError(f"\"ext.lower()\" is an unexpected value ({repr(ext.lower())})") from None
75 # Strip metadata synchronously ...
76 if strip and not isinstance(pool, multiprocessing.pool.Pool):
77 exiftool(
78 fname,
79 exiftoolPath = exiftoolPath,
80 timeout = timeout,
81 )