Allow rotated thumbs

This commit is contained in:
dave 2018-09-09 16:47:05 -07:00
parent 75c931c7d4
commit 1ff3e2106b
1 changed files with 20 additions and 8 deletions

View File

@ -70,20 +70,32 @@ class PhotoLibrary(object):
Create a thumbnail of the given photo, scaled/cropped to the given named style Create a thumbnail of the given photo, scaled/cropped to the given named style
:return: local path to thumbnail file or None if creation failed or was blocked :return: local path to thumbnail file or None if creation failed or was blocked
""" """
styles = {"tiny": (80, 80), # style tuples: max x, max y, rotate ok)
"small": (100, 100), # rotate ok means x and y maxes can be swapped if it fits the image's aspect ratio better
"feed": (250, 250), styles = {"tiny": (80, 80, False),
"preview": (1024, 768), "small": (100, 100, False),
"big": (2048, 1536)} "feed": (250, 250, False),
"preview": (1024, 768, True),
"big": (2048, 1536, True)}
dest = os.path.join(self.cache_path, "thumbs", style, "{}.jpg".format(photo.uuid)) dest = os.path.join(self.cache_path, "thumbs", style, "{}.jpg".format(photo.uuid))
if os.path.exists(dest): if os.path.exists(dest):
return os.path.abspath(dest) return os.path.abspath(dest)
if photo.width is None: # todo better detection of images that PIL can't open if photo.width is None: # todo better detection of images that PIL can't open
return None return None
if photo.uuid not in self._failed_thumbs_cache[style]: if photo.uuid not in self._failed_thumbs_cache[style]:
width = min(styles[style][0], photo.width if photo.width > 0 else 999999999) thumb_width, thumb_height, flip_ok = styles[style]
height = min(styles[style][1], photo.height if photo.height > 0 else 999999999) # TODO this is bad. im_is_rotated = photo.orientation % 2 != 0
p = Process(target=self.gen_thumb, args=(os.path.join(self.path, photo.path), dest, width, height, photo.orientation)) i_width = photo.width
i_height = photo.height
if im_is_rotated and flip_ok:
thumb_width, thumb_height = thumb_height, thumb_width
thumb_width = min(thumb_width, i_width if i_width > 0 else 999999999) # TODO do we even have photo.width if PIL can't read the image?
thumb_height = min(thumb_height, i_height if i_height > 0 else 999999999) # TODO this seems bad
print("thumb final wxh is {}x{}".format(thumb_width, thumb_height))
p = Process(target=self.gen_thumb, args=(os.path.join(self.path, photo.path), dest, thumb_width, thumb_height, photo.orientation))
p.start() p.start()
p.join() p.join()
if p.exitcode != 0: if p.exitcode != 0: