Moved all pathlib logic out of function

This commit is contained in:
Andrew Murray 2021-12-18 21:16:50 +11:00
parent cab7d8a8ab
commit d455abffee

View File

@ -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")