Skip to content

Commit 59c3c5a

Browse files
RobPasMueSMoraisAnsyspre-commit-ci[bot]
authored
feat: resize all images to the same aspect ratio and format (#701)
* feat: resize all images to the same aspect ratio and format * fix: rename script * fix: remove unused image * fix: vale issues * Update doc/source/_static/thumbnails/README.md Co-authored-by: Sébastien Morais <[email protected]> * fix: reverting images * feat: delete from _static folder * feat: run on Sphinx build process * feat: adapting conf.py to properly format it * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Sébastien Morais <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6f5349b commit 59c3c5a

File tree

2 files changed

+108
-8
lines changed

2 files changed

+108
-8
lines changed

doc/source/conf.py

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from datetime import datetime
44
import os
5+
import subprocess
56

67
from ansys_sphinx_theme import ansys_favicon, convert_version_to_pymeilisearch, get_version_match
8+
import sphinx
79
from sphinx.builders.latex import LaTeXBuilder
810

911
from pyansys import __version__ as pyansys_version
@@ -104,6 +106,12 @@
104106
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.2420.81" # noqa: E501
105107

106108

109+
###########################################################################
110+
111+
# -------------------------------------------------------------------------
112+
# Defining Sphinx build hooks
113+
# -------------------------------------------------------------------------
114+
107115
###########################################################################
108116
# Generate the package_versions directory
109117
###########################################################################
@@ -348,14 +356,105 @@ def get_release_branches_in_metapackage():
348356
return ["main"] + release_branches, ["dev"] + versions
349357

350358

351-
# -------------------------------------------------------------------------
352-
# Execute the previous functions to generate the package_versions directory
353-
# -------------------------------------------------------------------------
359+
###########################################################################
360+
# Adapting thumbnails to the documentation
361+
###########################################################################
362+
# This section adapts the thumbnails to the documentation. The thumbnails
363+
# are resized to 640x480 pixels and centered on a white background.
364+
#
365+
# The script resizes all images in the _static/thumbnails directory to 640x480
366+
# pixels and saves the resized images in the same directory. After the
367+
# documentation build, the script reverts the changes to the thumbnails.
368+
369+
370+
def resize_with_background(input_image_path, output_image_path, target_size):
371+
"""Resize an image while maintaining aspect ratio and centering it on a white background.
372+
373+
Parameters
374+
----------
375+
input_image_path : Path
376+
The path to the input image.
377+
output_image_path : Path
378+
The path to save the output image.
379+
target_size : tuple[int, int]
380+
The target size of the output image as a tuple (width, height) in pixels.
381+
"""
382+
from PIL import Image
383+
384+
# Open the input image
385+
img = Image.open(input_image_path).convert("RGBA") # Ensure the image has an alpha channel
386+
387+
# Resize the image while maintaining aspect ratio
388+
img.thumbnail(target_size, Image.LANCZOS)
389+
390+
# Create a white background of the target size
391+
background = Image.new(
392+
"RGBA", target_size, (255, 255, 255, 255)
393+
) # White background with no transparency
394+
395+
# Calculate the position to center the resized image on the background
396+
img_w, img_h = img.size
397+
bg_w, bg_h = background.size
398+
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
399+
400+
# Paste the resized image onto the white background
401+
background.paste(img, offset, mask=img) # Use the image's transparency as a mask
402+
403+
# Convert the image to RGB to remove the alpha channel (no transparency)
404+
background = background.convert("RGB")
405+
406+
# Save the result to the output path
407+
background.save(output_image_path)
354408

355-
branches, versions = get_release_branches_in_metapackage()
356-
generate_rst_files(
357-
versions,
358-
{version: build_versions_table(branch) for version, branch in zip(versions, branches)},
359-
)
360409

361410
###########################################################################
411+
412+
# --------------------------------------------------------------------------
413+
# Sphinx build hooks
414+
# --------------------------------------------------------------------------
415+
416+
417+
def resize_thumbnails(app: sphinx.application.Sphinx):
418+
"""Resize all images in the current directory to 640x480 pixels."""
419+
# Process all images
420+
from pathlib import Path
421+
422+
thumbnail_dir = Path(__file__).parent.absolute() / "_static" / "thumbnails"
423+
424+
for image in thumbnail_dir.glob("*.png"):
425+
target_size = (640, 480)
426+
resize_with_background(image, image, target_size)
427+
428+
429+
def revert_thumbnails(app: sphinx.application.Sphinx, exception):
430+
"""Resize all images in the current directory to 640x480 pixels."""
431+
from pathlib import Path
432+
433+
thumbnail_dir = Path(__file__).parent.absolute() / "_static" / "thumbnails"
434+
435+
subprocess.run(["git", "checkout", "--", thumbnail_dir])
436+
437+
438+
def package_versions_table(app: sphinx.application.Sphinx):
439+
"""Generate the package_versions directory."""
440+
branches, versions = get_release_branches_in_metapackage()
441+
generate_rst_files(
442+
versions,
443+
{version: build_versions_table(branch) for version, branch in zip(versions, branches)},
444+
)
445+
446+
447+
def setup(app: sphinx.application.Sphinx):
448+
"""Run different hook functions during the documentation build.
449+
450+
Parameters
451+
----------
452+
app : sphinx.application.Sphinx
453+
Sphinx instance containing all the configuration for the documentation build.
454+
"""
455+
# At the beginning of the build process - update the version in cheatsheet
456+
app.connect("builder-inited", resize_thumbnails)
457+
app.connect("builder-inited", package_versions_table)
458+
459+
# Reverting the thumbnails - no local changes
460+
app.connect("build-finished", revert_thumbnails)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ doc = [
9696
"Sphinx==8.0.2",
9797
"ansys-sphinx-theme==1.0.11",
9898
"Jinja2 ==3.1.4",
99+
"Pillow==10.4.0",
99100
"PyGithub==2.4.0",
100101
"sphinx-copybutton==0.5.2",
101102
"sphinx-design==0.6.1",

0 commit comments

Comments
 (0)