From d455abffeebd566f40ae859e89c590c3d260309e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 18 Dec 2021 21:16:50 +1100 Subject: [PATCH 1/4] 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 e4848fa62..dbbacceee 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 2/4] 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 dbbacceee..17c98ed94 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 3/4] 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 17c98ed94..a6b5e23d8 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") - for path in base_directory.iterdir(): - if path.suffix == ".png": - print(path) - compress_image(path, filepath.stem + ".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 + + 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 4/4] 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 a6b5e23d8..f71ad7698 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")