From af924a1f96ae25eca765e7e272d6ac3ac8b0177f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Wed, 1 Dec 2021 17:21:21 +0100 Subject: [PATCH 01/10] added pathlib tutorial --- docs/handbook/tutorial.rst | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index cdac0ae2dd8..abb1605ac3f 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -406,6 +406,38 @@ Using the ImageSequence Iterator class # ...do something to frame... +Batch processing with Pathlib +----------------------------- + +This example uses PIL togehter with pathlib, in order to reduce the quality of all png images in a folder + +:: + + from PIL import Image + from pathlib import Path + + + def compressImg(filepath, verbose=False): + file = filepath.stem + img = Image.open(filepath) + if img.mode != 'RGB': + img = img.convert('RGB') + img.save(file + ".jpg", + "JPEG", + optimize=True, + quality=80) + return + + + base_directory = Path.cwd() + + for path in base_directory.iterdir(): + if path.suffix == ".png": + print(path) + compressImg(path) + + + PostScript printing ------------------- From 52ac725ae0110cfcd74a2d090a85cdbda22e21b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Wed, 1 Dec 2021 23:06:52 +0100 Subject: [PATCH 02/10] Apply suggestions from code review Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- docs/handbook/tutorial.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index abb1605ac3f..b5d7407da5f 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -409,7 +409,7 @@ Using the ImageSequence Iterator class Batch processing with Pathlib ----------------------------- -This example uses PIL togehter with pathlib, in order to reduce the quality of all png images in a folder +This example uses PIL together with pathlib, in order to reduce the quality of all png images in a folder :: @@ -419,13 +419,13 @@ This example uses PIL togehter with pathlib, in order to reduce the quality of a def compressImg(filepath, verbose=False): file = filepath.stem - img = Image.open(filepath) - if img.mode != 'RGB': - img = img.convert('RGB') - img.save(file + ".jpg", - "JPEG", - optimize=True, - quality=80) + with Image.open(filepath) as img: + if img.mode != 'RGB': + img = img.convert('RGB') + img.save(file + ".jpg", + "JPEG", + optimize=True, + quality=80) return From 21351c8982cb24a2d2cf40994054e7680f283dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Wed, 1 Dec 2021 23:23:17 +0100 Subject: [PATCH 03/10] Update docs/handbook/tutorial.rst Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- docs/handbook/tutorial.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index b5d7407da5f..26625eacc2b 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -417,7 +417,7 @@ This example uses PIL together with pathlib, in order to reduce the quality of a from pathlib import Path - def compressImg(filepath, verbose=False): + def compressImg(filepath): file = filepath.stem with Image.open(filepath) as img: if img.mode != 'RGB': From cab7d8a8ab77f3dce3a536902ea7307e7e115b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Thu, 2 Dec 2021 16:20:13 +0100 Subject: [PATCH 04/10] Apply suggestions from code review Co-authored-by: Hugo van Kemenade --- docs/handbook/tutorial.rst | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index 26625eacc2b..e4848fa62e3 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -406,26 +406,23 @@ Using the ImageSequence Iterator class # ...do something to frame... -Batch processing with Pathlib +Batch processing with pathlib ----------------------------- -This example uses PIL together with pathlib, in order to reduce the quality of all png images in a folder +This example uses Pillow together with pathlib, in order to reduce the quality of all PNG images in a folder: :: - from PIL import Image from pathlib import Path + from PIL import Image - def compressImg(filepath): + def compress_image(filepath): file = filepath.stem with Image.open(filepath) as img: - if img.mode != 'RGB': - img = img.convert('RGB') - img.save(file + ".jpg", - "JPEG", - optimize=True, - quality=80) + if img.mode != "RGB": + img = img.convert("RGB") + img.save(file + ".jpg", "JPEG", optimize=True, quality=80) return @@ -434,7 +431,7 @@ This example uses PIL together with pathlib, in order to reduce the quality of a for path in base_directory.iterdir(): if path.suffix == ".png": print(path) - compressImg(path) + compress_image(path) From d455abffeebd566f40ae859e89c590c3d260309e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 18 Dec 2021 21:16:50 +1100 Subject: [PATCH 05/10] Moved all pathlib logic out of function --- docs/handbook/tutorial.rst | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index e4848fa62e3..dbbacceee07 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -417,13 +417,11 @@ This example uses Pillow together with pathlib, in order to reduce the quality o from PIL import Image - def compress_image(filepath): - file = filepath.stem - with Image.open(filepath) as img: + def compress_image(source_path, dest_path): + with Image.open(source_path) as img: if img.mode != "RGB": img = img.convert("RGB") - img.save(file + ".jpg", "JPEG", optimize=True, quality=80) - return + img.save(dest_path, "JPEG", optimize=True, quality=80) base_directory = Path.cwd() @@ -431,7 +429,7 @@ This example uses Pillow together with pathlib, in order to reduce the quality o for path in base_directory.iterdir(): if path.suffix == ".png": print(path) - compress_image(path) + compress_image(path, filepath.stem + ".jpg") From 946571d4a3af5b605ee375df9cb2e3912148d1c7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 18 Dec 2021 21:23:07 +1100 Subject: [PATCH 06/10] Moved batch processing example under "More on reading images" --- docs/handbook/tutorial.rst | 54 +++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index dbbacceee07..17c98ed9448 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -406,33 +406,6 @@ Using the ImageSequence Iterator class # ...do something to frame... -Batch processing with pathlib ------------------------------ - -This example uses Pillow together with pathlib, in order to reduce the quality of all PNG images in a folder: - -:: - - from pathlib import Path - from PIL import Image - - - def compress_image(source_path, dest_path): - with Image.open(source_path) as img: - if img.mode != "RGB": - img = img.convert("RGB") - img.save(dest_path, "JPEG", optimize=True, quality=80) - - - base_directory = Path.cwd() - - for path in base_directory.iterdir(): - if path.suffix == ".png": - print(path) - compress_image(path, filepath.stem + ".jpg") - - - PostScript printing ------------------- @@ -520,6 +493,33 @@ Reading from a tar archive fp = TarIO.TarIO("Tests/images/hopper.tar", "hopper.jpg") im = Image.open(fp) + +Batch processing +^^^^^^^^^^^^^^^^ + +This example uses Pillow together with pathlib, in order to reduce the quality of all PNG images in a folder: + +:: + + from pathlib import Path + from PIL import Image + + + def compress_image(source_path, dest_path): + with Image.open(source_path) as img: + if img.mode != "RGB": + img = img.convert("RGB") + img.save(dest_path, "JPEG", optimize=True, quality=80) + + + base_directory = Path.cwd() + + for path in base_directory.iterdir(): + if path.suffix == ".png": + print(path) + compress_image(path, filepath.stem + ".jpg") + + Controlling the decoder ----------------------- From 6c8ac0e700740750da11368d9121338c7949a028 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 18 Dec 2021 21:59:09 +1100 Subject: [PATCH 07/10] Added "os" example --- docs/handbook/tutorial.rst | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index 17c98ed9448..a6b5e23d8e0 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -497,11 +497,12 @@ Reading from a tar archive Batch processing ^^^^^^^^^^^^^^^^ -This example uses Pillow together with pathlib, in order to reduce the quality of all PNG images in a folder: +Operations can be applied to multiple image files. For example, all PNG images +in the current directory can be saved as JPEGs at reduced quality. :: - from pathlib import Path + import os from PIL import Image @@ -512,12 +513,20 @@ This example uses Pillow together with pathlib, in order to reduce the quality o img.save(dest_path, "JPEG", optimize=True, quality=80) - base_directory = Path.cwd() + paths = [path for path in os.listdir(".") if path.endsWith(".png")] + for path in paths: + compress_image(path, path[:-4] + ".jpg") + +Since images can also be opened from a ``Path`` from the ``pathlib`` module, +the example could be modified to use ``pathlib`` instead of ``os``. + +:: + + from pathlib import Path - for path in base_directory.iterdir(): - if path.suffix == ".png": - print(path) - compress_image(path, filepath.stem + ".jpg") + paths = [path for path in Path.cwd().iterdir() if path.suffix == ".png"] + for path in paths: + compress_image(path, filepath.stem + ".jpg") Controlling the decoder From cd613e68503ff130a32b44e80d18332809c01640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Sun, 19 Dec 2021 13:26:30 +0100 Subject: [PATCH 08/10] Apply suggestions from code review Co-authored-by: Hugo van Kemenade --- docs/handbook/tutorial.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index a6b5e23d8e0..f71ad769837 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -513,7 +513,7 @@ in the current directory can be saved as JPEGs at reduced quality. img.save(dest_path, "JPEG", optimize=True, quality=80) - paths = [path for path in os.listdir(".") if path.endsWith(".png")] + paths = glob.glob(".png") for path in paths: compress_image(path, path[:-4] + ".jpg") @@ -524,7 +524,7 @@ the example could be modified to use ``pathlib`` instead of ``os``. from pathlib import Path - paths = [path for path in Path.cwd().iterdir() if path.suffix == ".png"] + paths = Path(".").glob("*.png") for path in paths: compress_image(path, filepath.stem + ".jpg") From 36caa53fedd11466066d3f55a826becba5220315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Sun, 19 Dec 2021 22:48:23 +0100 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- docs/handbook/tutorial.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index f71ad769837..f8ee0c413ab 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -502,7 +502,7 @@ in the current directory can be saved as JPEGs at reduced quality. :: - import os + import glob from PIL import Image @@ -513,12 +513,13 @@ in the current directory can be saved as JPEGs at reduced quality. img.save(dest_path, "JPEG", optimize=True, quality=80) - paths = glob.glob(".png") + paths = glob.glob("*.png") for path in paths: compress_image(path, path[:-4] + ".jpg") Since images can also be opened from a ``Path`` from the ``pathlib`` module, -the example could be modified to use ``pathlib`` instead of ``os``. +the example could be modified to use ``pathlib`` instead of the ``glob`` +module. :: From a12c18608efbf21395662dccf2e105c6722d5652 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Mon, 20 Dec 2021 09:39:09 +1100 Subject: [PATCH 10/10] Corrected variable name --- docs/handbook/tutorial.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index f8ee0c413ab..895408b79c6 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -527,7 +527,7 @@ module. paths = Path(".").glob("*.png") for path in paths: - compress_image(path, filepath.stem + ".jpg") + compress_image(path, path.stem + ".jpg") Controlling the decoder