Skip to content

Commit 7b0b1af

Browse files
authored
Merge pull request #1055 from xylar/switch-thumbnails-to-jpg
Switch to writing thumbnails as jpgs
2 parents 2340566 + c4ce510 commit 7b0b1af

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

mpas_analysis/shared/html/image_xml.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
# distributed with this code, or at
1010
# https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/main/LICENSE
1111

12+
import datetime
1213
import os
13-
import sys
1414
import socket
1515
import subprocess
16-
import datetime
16+
import sys
17+
from pathlib import Path
18+
1719
from lxml import etree
1820
from PIL import Image
1921

@@ -196,12 +198,21 @@ def _generate_thumbnails(imageFileName, directory):
196198
fixedWidth = 480
197199
fixedHeight = 360
198200

199-
image = Image.open('{}/{}'.format(directory, imageFileName))
200-
thumbnailDir = '{}/thumbnails'.format(directory)
201+
# more vertical than horizontal
202+
aspectRatioThreshold = 0.75
203+
204+
directory = Path(directory)
205+
206+
image = Image.open(directory / imageFileName)
207+
image = image.convert('RGB')
208+
thumbnailDir = directory / 'thumbnails'
209+
thumbnailFilename = Path(imageFileName).with_suffix('.jpg')
210+
fixedFilename = f'fixed_{str(thumbnailFilename)}'
201211

202212
imageSize = image.size
213+
aspectRatio = imageSize[0] / float(imageSize[1])
203214

204-
if imageSize[0] < imageSize[1]:
215+
if aspectRatio < aspectRatioThreshold:
205216
orientation = 'vert'
206217
thumbnailHeight = 320
207218
else:
@@ -212,7 +223,8 @@ def _generate_thumbnails(imageFileName, directory):
212223
factor = image.size[1] / float(thumbnailHeight)
213224
thumbnailSize = [int(dim / factor + 0.5) for dim in image.size]
214225
thumbnail = image.resize(thumbnailSize, Image.LANCZOS)
215-
thumbnail.save('{}/{}'.format(thumbnailDir, imageFileName))
226+
227+
thumbnail.save(thumbnailDir / thumbnailFilename)
216228

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

236-
thumbnail.save('{}/fixed_{}'.format(thumbnailDir, imageFileName))
248+
thumbnail.save(thumbnailDir / fixedFilename)
237249

238250
return imageSize, thumbnailSize, orientation
239251

mpas_analysis/shared/html/pages.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
# distributed with this code, or at
1010
# https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/main/LICENSE
1111

12-
import pkg_resources
13-
from os import makedirs
14-
from lxml import etree
15-
from collections import OrderedDict
16-
import subprocess
1712
import os
13+
import subprocess
1814
import sys
15+
from collections import OrderedDict
16+
from os import makedirs
17+
from pathlib import Path
18+
19+
import pkg_resources
20+
from lxml import etree
1921

2022
import mpas_analysis.version
2123
from mpas_analysis.shared.io.utility import build_config_full_path, copyfile
@@ -222,7 +224,7 @@ def generate(self):
222224

223225
for componentName, componentDict in self.components.items():
224226
subdirectory = componentDict['subdirectory']
225-
imageFileName = componentDict['imageFileName']
227+
imageFileName = _to_jpg(componentDict['imageFileName'])
226228
replacements = {'@componentDir': subdirectory,
227229
'@componentName': componentName,
228230
'@firstImage': imageFileName}
@@ -555,7 +557,9 @@ def _get_required_xml_text(root, tag, fileName):
555557

556558
def _generate_image_text(self, imageFileName, imageDict):
557559
"""fill in the template for a given image with the desired content"""
558-
replacements = {'@imageFileName': imageFileName}
560+
thumbnailFileName = _to_jpg(imageFileName)
561+
replacements = {'@imageFileName': imageFileName,
562+
'@thumbnailFileName': thumbnailFileName}
559563
for tag in ['imageSize', 'imageDescription', 'imageCaption',
560564
'thumbnailDescription', 'orientation', 'thumbnailWidth',
561565
'thumbnailHeight']:
@@ -627,11 +631,11 @@ def _generate_quick_link_text(self, groupName, groupDict):
627631
"""
628632

629633
firstGallery = next(iter(groupDict['galleries'].values()))
630-
firstImageFileName = next(iter(firstGallery['images']))
634+
thumbnailFileName = _to_jpg(next(iter(firstGallery['images'])))
631635

632636
replacements = {'@analysisGroupName': groupName,
633637
'@analysisGroupLink': groupDict['link'],
634-
'@imageFileName': firstImageFileName}
638+
'@thumbnailFileName': thumbnailFileName}
635639

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

667671
githash = githash.decode('utf-8').strip('\n').replace('"', '')
668672
return githash
673+
674+
675+
def _to_jpg(filename):
676+
"""
677+
change the file extention to jpg (presumably from png)
678+
"""
679+
filename = str(Path(filename).with_suffix('.jpg'))
680+
return filename

mpas_analysis/shared/html/templates/component_image.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

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

22
<div class="gallery-link small">
33
<a href="#@analysisGroupLink">
4-
<img class="small" src="thumbnails/fixed_@imageFileName" alt="@analysisGroupName">
4+
<img class="small" src="thumbnails/fixed_@thumbnailFileName" alt="@analysisGroupName">
55
</a>
66
<div class="desc-small">@analysisGroupName</div>
77
</div>

0 commit comments

Comments
 (0)