Skip to content

Commit

Permalink
Compress figures in report (#28)
Browse files Browse the repository at this point in the history
Figures are converted from PNG to JPG and compressed to 95% quality, resulting in OOM reduction in final PDF size (~20 MB to ~2 MB). The JPGs are cleaned up and removed after report generation, so only the high quality individual PNGs remain for your viewing pleasure.
  • Loading branch information
bpurinton authored Jul 10, 2024
1 parent a3b8a2c commit 777845d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
22 changes: 21 additions & 1 deletion asp_plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ def save_figure(fig, save_dir=None, fig_fn=None, dpi=150):


def compile_report(plots_directory, processing_parameters_dict, report_pdf_path):
from PIL import Image

files = [f for f in os.listdir(plots_directory) if f.endswith(".png")]
files.sort()

# Convert .png files to .jpg with 95% quality
compressed_files = []
for file in files:
png_path = os.path.join(plots_directory, file)
jpg_file = file.replace(".png", ".jpg")
jpg_path = os.path.join(plots_directory, jpg_file)

with Image.open(png_path) as img:
img = img.convert("RGB")
img.save(jpg_path, "JPEG", quality=95)

compressed_files.append(jpg_file)

processing_date = (
f"Processed on: {processing_parameters_dict['processing_timestamp']:}"
)
Expand All @@ -50,11 +65,16 @@ def compile_report(plots_directory, processing_parameters_dict, report_pdf_path)
f"## Processing Parameters\n\n{ba_string:}\n\n{stereo_string}\n\n{point2dem_string}\n\n"
)
)
plots = "".join([f"![]({file})\n\n" for file in files])
plots = "".join([f"![]({file})\n\n" for file in compressed_files])
pdf.add_section(Section(f"## Plots\n\n{plots:}", root=plots_directory))

pdf.save(report_pdf_path)

# cleanup temporary JPEG files
for file in compressed_files:
jpg_path = os.path.join(plots_directory, file)
os.remove(jpg_path)


def get_xml_tag(xml, tag, all=False):
import xml.etree.ElementTree as ET
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "asp_plot"
version = "0.2.0"
version = "0.2.1"
authors = [
{ name="Ben Purinton", email="[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="asp_plot",
version="0.2.0",
version="0.2.1",
description="Python library and client for plotting Ames Stereo Pipeline outputs",
author="Ben Purinton",
author_email="[email protected]",
Expand Down

0 comments on commit 777845d

Please sign in to comment.