zimscraperlib.image.optimization
An image optimization module to optimize the following image formats:
- JPEG (using optimize-images)
- PNG (using optimize-images)
- GIF (using gifsicle with lossy optimization)
- WebP (using Pillow)
Some important notes: - This makes use of the --lossy option from gifsicle which is present only in versions above 1.92. If the package manager has a lower version, you can build gifsicle from source and install or do not use the lossiness option.
-
Presets for the optimizer are available in zimscraperlib.image.presets.
-
If no options for an image optimization is passed, the optimizer can still run on default settings which give a bit less size than the original images but maintain a high quality.
Classes:
-
OptimizeGifOptions–Dataclass holding GIF optimization options
-
OptimizeJpgOptions–Dataclass holding JPG optimization options
-
OptimizeOptions–Dataclass holding optimization options for all supported formats
-
OptimizePngOptions–Dataclass holding PNG optimization options
-
OptimizeWebpOptions–Dataclass holding WebP optimization options
Functions:
-
ensure_matches–Raise ValueError if src is not of image type
fmt -
optimize_gif–method to convert (if needed) and optimize GIFs using gifsicle >= 1.92
-
optimize_image–Optimize image, automatically selecting correct optimizer
-
optimize_jpeg–method to convert (if needed) and optimize JPEG files
-
optimize_png–method to convert (if needed) and optimize PNG files
-
optimize_webp–method to convert (if needed) and optimize WebP using Pillow options
OptimizeGifOptions
dataclass
OptimizeGifOptions(
optimize_level: int | None = 1,
lossiness: int | None = None,
max_colors: int | None = None,
interlace: bool | None = True,
no_extensions: bool | None = True,
)
Dataclass holding GIF optimization options
Parameters:
-
optimize_level(int | None, default:1) –Optimization level; higher values give better compression (integer between 1 and 3); values: 1 | 2 | 3
-
lossiness(int | None, default:None) –Level of lossy optimization to use; higher values give better compression (integer) values: 20 | 45 | 80 | XX
-
interlace(bool | None, default:True) –Whether to interlace the frames (boolean) values: True | False
-
no_extensions(bool | None, default:True) –Whether to remove all extension options from GIF (boolean) values: True | False
-
max_colors(int | None, default:None) –Maximum number of colors in resultant GIF; (integer between 2 and 256) values: 2 | 86 | 128 | 256 | XX
refer to the link for more details - https://www.lcdf.org/gifsicle/man.html
Attributes:
-
interlace(bool | None) – -
lossiness(int | None) – -
max_colors(int | None) – -
no_extensions(bool | None) – -
optimize_level(int | None) –
OptimizeJpgOptions
dataclass
OptimizeJpgOptions(
quality: int | None = 85,
fast_mode: bool | None = True,
keep_exif: bool | None = True,
)
Dataclass holding JPG optimization options
Parameters:
-
quality(int | None, default:85) –JPEG quality (integer between 1 and 100) values: 50 | 55 | 35 | 100 | XX
-
keep_exif(bool | None, default:True) –Whether to keep EXIF data in JPEG (boolean) values: True | False
-
fast_mode(bool | None, default:True) –Use the supplied quality value. If turned off, optimizer will get dynamic quality value to ensure better compression values: True | False
Attributes:
OptimizeOptions
dataclass
OptimizeOptions(
gif: OptimizeGifOptions,
webp: OptimizeWebpOptions,
jpg: OptimizeJpgOptions,
png: OptimizePngOptions,
)
Dataclass holding optimization options for all supported formats
Methods:
-
of–Helper to override only few options from default value
Attributes:
-
gif(OptimizeGifOptions) – -
jpg(OptimizeJpgOptions) – -
png(OptimizePngOptions) – -
webp(OptimizeWebpOptions) –
of
classmethod
of(
gif: OptimizeGifOptions | None = None,
webp: OptimizeWebpOptions | None = None,
jpg: OptimizeJpgOptions | None = None,
png: OptimizePngOptions | None = None,
)
Helper to override only few options from default value
Source code in src/zimscraperlib/image/optimization.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
OptimizePngOptions
dataclass
OptimizePngOptions(
max_colors: int = 256,
background_color: tuple[int, int, int] = (
255,
255,
255,
),
reduce_colors: bool | None = False,
fast_mode: bool | None = True,
remove_transparency: bool | None = False,
)
Dataclass holding PNG optimization options
Parameters:
-
reduce_colors(bool | None, default:False) –Whether to reduce colors using adaptive color pallette (boolean) values: True | False
-
max_colors(int, default:256) –Maximum number of colors if reduce_colors is True (integer between 1 and 256) values: 35 | 64 | 256 | 128 | XX
-
fast_mode(bool | None, default:True) –Whether to use faster but weaker compression (boolean) values: True | False
-
remove_transparency(bool | None, default:False) –Whether to remove transparency (boolean) values: True | False
-
background_color(tuple[int, int, int], default:(255, 255, 255)) –Background color if remove_transparency is True (tuple containing RGB values) values: (255, 255, 255) | (221, 121, 108) | (XX, YY, ZZ)
Attributes:
-
background_color(tuple[int, int, int]) – -
fast_mode(bool | None) – -
max_colors(int) – -
reduce_colors(bool | None) – -
remove_transparency(bool | None) –
background_color
class-attribute
instance-attribute
OptimizeWebpOptions
dataclass
OptimizeWebpOptions(
quality: int | None = 60,
method: int | None = 6,
lossless: bool | None = False,
)
Dataclass holding WebP optimization options
Parameters:
-
lossless(bool | None, default:False) –Whether to use lossless compression (boolean); values: True | False
-
quality(int | None, default:60) –WebP quality for lossy, effort put into compression for lossless (integer between 0 to 100); values: 30 | 45 | 100 | XX
-
method(int | None, default:6) –Quality/speed trade-off; higher values give better compression (integer between 1 and 6); values: 1 | 2 | 3 | 4 | 5 | 6
refer to the link for more details https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp
Attributes:
ensure_matches
Raise ValueError if src is not of image type fmt
Source code in src/zimscraperlib/image/optimization.py
45 46 47 48 49 50 51 52 | |
optimize_gif
optimize_gif(
src: Path | BytesIO,
dst: Path | BytesIO,
options: OptimizeGifOptions | None = None,
) -> Path | BytesIO
method to convert (if needed) and optimize GIFs using gifsicle >= 1.92
Source code in src/zimscraperlib/image/optimization.py
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 371 372 373 374 | |
optimize_image
optimize_image(
src: Path | BytesIO | bytes,
dst: Path | BytesIO,
options: OptimizeOptions | None = None,
*,
dst_format: str | None = None,
delete_src: bool = False,
convert: bool | str = False,
) -> Path | BytesIO
Optimize image, automatically selecting correct optimizer
Parameters:
-
dst_format(str | None, default:None) –format of the destination image, required when dst is io.BytesIO.
-
delete_src(bool, default:False) –whether to remove src file upon success (boolean) values: True | False
-
convert(bool | str, default:False) –will be deprecated in scraperlib 6, use dst_format instead. values: False: don't convert True: convert to format implied by dst suffix str: convert to the specified format
Source code in src/zimscraperlib/image/optimization.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
optimize_jpeg
optimize_jpeg(
src: Path | BytesIO,
dst: Path | BytesIO | None = None,
options: OptimizeJpgOptions | None = None,
) -> Path | BytesIO
method to convert (if needed) and optimize JPEG files
Source code in src/zimscraperlib/image/optimization.py
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 | |
optimize_png
optimize_png(
src: Path | BytesIO,
dst: Path | BytesIO | None = None,
options: OptimizePngOptions | None = None,
) -> Path | BytesIO
method to convert (if needed) and optimize PNG files
Source code in src/zimscraperlib/image/optimization.py
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 | |
optimize_webp
optimize_webp(
src: Path | BytesIO,
dst: Path | BytesIO | None = None,
options: OptimizeWebpOptions | None = None,
) -> Path | BytesIO
method to convert (if needed) and optimize WebP using Pillow options
Source code in src/zimscraperlib/image/optimization.py
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 | |