From d4f952e7c8a22407f8d3532ed3e006e3dbad3dd2 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sat, 4 Jul 2020 19:31:37 -0700 Subject: [PATCH] Delete temporary copied opengraph image after conversion --- hyperglass/util.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hyperglass/util.py b/hyperglass/util.py index 721f01b..ba10906 100644 --- a/hyperglass/util.py +++ b/hyperglass/util.py @@ -439,6 +439,7 @@ def generate_opengraph( # Copy the original image to the target path copied = shutil.copy2(image_path, target_path) + log.debug("Copied {} to {}", str(image_path), str(target_path)) with Image.open(copied) as src: @@ -446,11 +447,13 @@ def generate_opengraph( if src.size[0] != max_width or src.size[1] != max_height: # Resize image while maintaining aspect ratio + log.debug("Opengraph image is not 1200x630, resizing...") src.thumbnail((max_width, max_height)) # Only impose a background image if the original image has # alpha/transparency channels if src.mode in ("RGBA", "LA"): + log.debug("Opengraph image has transparency, converting...") background = Image.new("RGB", (max_width, max_height), background_color) background.paste(src, box=center_point(background, src)) dst = background @@ -460,9 +463,14 @@ def generate_opengraph( # Save new image to derived target path dst.save(dst_path) + # Delete the copied image + Path(copied).unlink() + if not dst_path.exists(): raise RuntimeError(f"Unable to save resized image to {str(dst_path)}") + log.debug("Opengraph image ready at {}", str(dst_path)) + return True