Skip to content

Commit

Permalink
Merge pull request #1055 from xylar/switch-thumbnails-to-jpg
Browse files Browse the repository at this point in the history
Switch to writing thumbnails as jpgs
  • Loading branch information
xylar authored Jan 22, 2025
2 parents 2340566 + c4ce510 commit 7b0b1af
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
26 changes: 19 additions & 7 deletions mpas_analysis/shared/html/image_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/main/LICENSE

import datetime
import os
import sys
import socket
import subprocess
import datetime
import sys
from pathlib import Path

from lxml import etree
from PIL import Image

Expand Down Expand Up @@ -196,12 +198,21 @@ def _generate_thumbnails(imageFileName, directory):
fixedWidth = 480
fixedHeight = 360

image = Image.open('{}/{}'.format(directory, imageFileName))
thumbnailDir = '{}/thumbnails'.format(directory)
# more vertical than horizontal
aspectRatioThreshold = 0.75

directory = Path(directory)

image = Image.open(directory / imageFileName)
image = image.convert('RGB')
thumbnailDir = directory / 'thumbnails'
thumbnailFilename = Path(imageFileName).with_suffix('.jpg')
fixedFilename = f'fixed_{str(thumbnailFilename)}'

imageSize = image.size
aspectRatio = imageSize[0] / float(imageSize[1])

if imageSize[0] < imageSize[1]:
if aspectRatio < aspectRatioThreshold:
orientation = 'vert'
thumbnailHeight = 320
else:
Expand All @@ -212,7 +223,8 @@ def _generate_thumbnails(imageFileName, directory):
factor = image.size[1] / float(thumbnailHeight)
thumbnailSize = [int(dim / factor + 0.5) for dim in image.size]
thumbnail = image.resize(thumbnailSize, Image.LANCZOS)
thumbnail.save('{}/{}'.format(thumbnailDir, imageFileName))

thumbnail.save(thumbnailDir / thumbnailFilename)

# second, make a thumbnail with a fixed size
widthFactor = image.size[0] / float(fixedWidth)
Expand All @@ -233,7 +245,7 @@ def _generate_thumbnails(imageFileName, directory):
# crop out the left side of the thubnail
thumbnail = thumbnail.crop([0, 0, fixedWidth, fixedHeight])

thumbnail.save('{}/fixed_{}'.format(thumbnailDir, imageFileName))
thumbnail.save(thumbnailDir / fixedFilename)

return imageSize, thumbnailSize, orientation

30 changes: 21 additions & 9 deletions mpas_analysis/shared/html/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/main/LICENSE

import pkg_resources
from os import makedirs
from lxml import etree
from collections import OrderedDict
import subprocess
import os
import subprocess
import sys
from collections import OrderedDict
from os import makedirs
from pathlib import Path

import pkg_resources
from lxml import etree

import mpas_analysis.version
from mpas_analysis.shared.io.utility import build_config_full_path, copyfile
Expand Down Expand Up @@ -222,7 +224,7 @@ def generate(self):

for componentName, componentDict in self.components.items():
subdirectory = componentDict['subdirectory']
imageFileName = componentDict['imageFileName']
imageFileName = _to_jpg(componentDict['imageFileName'])
replacements = {'@componentDir': subdirectory,
'@componentName': componentName,
'@firstImage': imageFileName}
Expand Down Expand Up @@ -555,7 +557,9 @@ def _get_required_xml_text(root, tag, fileName):

def _generate_image_text(self, imageFileName, imageDict):
"""fill in the template for a given image with the desired content"""
replacements = {'@imageFileName': imageFileName}
thumbnailFileName = _to_jpg(imageFileName)
replacements = {'@imageFileName': imageFileName,
'@thumbnailFileName': thumbnailFileName}
for tag in ['imageSize', 'imageDescription', 'imageCaption',
'thumbnailDescription', 'orientation', 'thumbnailWidth',
'thumbnailHeight']:
Expand Down Expand Up @@ -627,11 +631,11 @@ def _generate_quick_link_text(self, groupName, groupDict):
"""

firstGallery = next(iter(groupDict['galleries'].values()))
firstImageFileName = next(iter(firstGallery['images']))
thumbnailFileName = _to_jpg(next(iter(firstGallery['images'])))

replacements = {'@analysisGroupName': groupName,
'@analysisGroupLink': groupDict['link'],
'@imageFileName': firstImageFileName}
'@thumbnailFileName': thumbnailFileName}

quickLinkText = _replace_tempate_text(self.templates['quicklink'],
replacements)
Expand Down Expand Up @@ -666,3 +670,11 @@ def _get_git_hash():

githash = githash.decode('utf-8').strip('\n').replace('"', '')
return githash


def _to_jpg(filename):
"""
change the file extention to jpg (presumably from png)
"""
filename = str(Path(filename).with_suffix('.jpg'))
return filename
2 changes: 1 addition & 1 deletion mpas_analysis/shared/html/templates/component_image.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="@imageFileName" itemprop="contentUrl" data-size="@imageSize">
<img class="@orientation" src="thumbnails/@imageFileName" itemprop="thumbnail" alt="@imageDescription" width="@thumbnailWidth" height="@thumbnailHeight"/>
<img class="@orientation" src="thumbnails/@thumbnailFileName" itemprop="thumbnail" alt="@imageDescription" width="@thumbnailWidth" height="@thumbnailHeight"/>
</a>
<figcaption itemprop="caption description">@imageCaption</figcaption>
<div class="desc-small">@thumbnailDescription</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div class="gallery-link small">
<a href="#@analysisGroupLink">
<img class="small" src="thumbnails/fixed_@imageFileName" alt="@analysisGroupName">
<img class="small" src="thumbnails/fixed_@thumbnailFileName" alt="@analysisGroupName">
</a>
<div class="desc-small">@analysisGroupName</div>
</div>

0 comments on commit 7b0b1af

Please sign in to comment.