Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Use Pillow for image info #1138

Merged
merged 19 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 29 additions & 45 deletions lektor/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,45 +776,35 @@ def url_path(self):
class Image(Attachment):
"""Specific class for image attachments."""

def __init__(self, pad, data, page_num=None):
Attachment.__init__(self, pad, data, page_num)
self._image_info = None
self._exif_cache = None

def _get_image_info(self):
if self._image_info is None:
with open(self.attachment_filename, "rb") as f:
self._image_info = get_image_info(f)
return self._image_info
@cached_property
def _image_info(self):
return get_image_info(self.attachment_filename)

@property
@cached_property
def exif(self):
"""Provides access to the exif data."""
if self._exif_cache is None:
with open(self.attachment_filename, "rb") as f:
self._exif_cache = read_exif(f)
return self._exif_cache
return read_exif(self.attachment_filename)

@property
def width(self):
"""The width of the image if possible to determine."""
rv = self._get_image_info()[1]
rv = self._image_info[1]
if rv is not None:
return rv
return Undefined("Width of image could not be determined.")

@property
def height(self):
"""The height of the image if possible to determine."""
rv = self._get_image_info()[2]
rv = self._image_info[2]
if rv is not None:
return rv
return Undefined("Height of image could not be determined.")

@property
def format(self):
"""Returns the format of the image."""
rv = self._get_image_info()[0]
rv = self._image_info[0]
if rv is not None:
return rv
return Undefined("The format of the image could not be determined.")
Expand Down Expand Up @@ -863,55 +853,49 @@ def wrapper(*args, **kwargs):
class Video(Attachment):
"""Specific class for video attachments."""

def __init__(self, pad, data, page_num=None):
Attachment.__init__(self, pad, data, page_num)
self._video_info = None

def _get_video_info(self):
if self._video_info is None:
try:
self._video_info = get_video_info(self.attachment_filename)
except RuntimeError:
# A falsy value ensures we don't retry this video again
self._video_info = False
return self._video_info
@cached_property
def _video_info(self):
try:
return get_video_info(self.attachment_filename)
except RuntimeError:
return {}

@property
@require_ffmpeg
def width(self):
"""Returns the width of the video if possible to determine."""
rv = self._get_video_info()
if rv:
return rv["width"]
return Undefined("The width of the video could not be determined.")
try:
return self._video_info["width"]
except KeyError:
return Undefined("The width of the video could not be determined.")

@property
@require_ffmpeg
def height(self):
"""Returns the height of the video if possible to determine."""
rv = self._get_video_info()
if rv:
return rv["height"]
return Undefined("The height of the video could not be determined.")
try:
return self._video_info["height"]
except KeyError:
return Undefined("The height of the video could not be determined.")

@property
@require_ffmpeg
def duration(self):
"""Returns the duration of the video if possible to determine."""
rv = self._get_video_info()
if rv:
return rv["duration"]
return Undefined("The duration of the video could not be determined.")
try:
return self._video_info["duration"]
except KeyError:
return Undefined("The duration of the video could not be determined.")

@require_ffmpeg
def frame(self, seek=None):
"""Returns a VideoFrame object that is thumbnailable like an Image."""
rv = self._get_video_info()
if not rv:
duration = self.duration
if is_undefined(duration):
return Undefined("Unable to get video properties.")

if seek is None:
seek = rv["duration"] / 2
seek = duration / 2
return VideoFrame(self, seek)


Expand Down