Skip to content

Commit

Permalink
Add optional to calculate the average color for an image
Browse files Browse the repository at this point in the history
  • Loading branch information
phibos committed Jan 10, 2022
1 parent dc0248f commit 2561b6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Defaults:
}
```

`PHOTO_RESULT_IMAGE_AVERAGE_COLOR = False`
: Calculate the average color for an result image. This can be used to provide a default background color while using lazy loading.

The plug-in automatically resizes the photos and publishes them to the following output folder:

./output/photos
Expand Down
50 changes: 38 additions & 12 deletions pelican/plugins/photos/photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ class BaseNote:
cache_class = None

def __init__(self, source_image):
print(self.cache_class)
self.cache = self.cache_class.from_cache(source_image)
self._value = self.cache.get_value(source_image)

Expand Down Expand Up @@ -398,8 +397,11 @@ def __init__(
self.source_image = SourceImage.from_cache(src)
self.dst = dst

self._average_color = None
self._height: Optional[int] = None
self._width: Optional[int] = None
self._result_info_loaded = False
self._result_info_allowed_names = ("_average_color", "_height", "_width")

if spec is None:
if specs is None:
Expand Down Expand Up @@ -459,6 +461,16 @@ def __init__(
def __str__(self):
return self.web_filename

@property
def average_color(self) -> Optional[str]:
"""Average color"""
if self._average_color is None:
self._load_result_info()
if self._average_color:
return "#{:02x}{:02x}{:02x}".format(*self._average_color)
else:
return None

@property
def caption(self) -> Optional[Caption]:
"""Caption of the image"""
Expand Down Expand Up @@ -496,21 +508,34 @@ def width(self) -> int:
self._load_result_info()
return self._width

def _load_result_info(self):
def _load_result_info(self, image: Optional[PILImage.Image] = None):
"""Load the information from the result image"""
img: PILImage.Image = PILImage.open(self.output_filename)
self._height = img.height
self._width = img.width
if self._result_info_loaded:
if image is None:
image: PILImage.Image = PILImage.open(self.output_filename)

if pelican_settings["PHOTO_RESULT_IMAGE_AVERAGE_COLOR"]:
image2: PILImage.Image = image.resize((1, 1), PILImage.ANTIALIAS)
self._average_color = image2.getpixel((0, 0))

self._height = image.height
self._width = image.width

results = {}
for name in self._result_info_allowed_names:
results[name] = getattr(self, name)
self._result_info_loaded = True
return results

def apply_result_info(self, info: Dict[str, Any]):
"""
Apply the information from the result image if it has been processed in a
different process.
"""
allowed_names = ("_height", "_width")
for name in allowed_names:
for name in self._result_info_allowed_names:
if name in info:
setattr(self, name, info[name])
self._result_info_loaded = True

def manipulate_exif(self, img: PILImage.Image) -> Tuple[PILImage.Image, str]:
try:
Expand Down Expand Up @@ -545,8 +570,8 @@ def manipulate_exif(self, img: PILImage.Image) -> Tuple[PILImage.Image, str]:

def process(self, key: str) -> Tuple[str, Dict[str, Any]]:
"""Process the image"""
self.resize()
return key, {"_height": self._height, "_width": self._width}
image: PILImage.Image = self.resize()
return key, self._load_result_info(image=image)

def reduce_opacity(self, im: PILImage.Image, opacity) -> PILImage.Image:
"""Reduces Opacity.
Expand All @@ -572,7 +597,7 @@ def remove_alpha(img: PILImage.Image, bg_color) -> PILImage.Image:
background.paste(img, mask=img.split()[3]) # 3 is the alpha channel
return background

def resize(self):
def resize(self) -> PILImage.Image:
"""Resize the image"""
spec = self.spec

Expand Down Expand Up @@ -640,8 +665,7 @@ def resize(self):
# exif=exif_copy,
**image_options,
)
self._height = im.height
self._width = im.width
return im

@staticmethod
def rotate(img: PILImage.Image, exif_dict) -> PILImage.Image:
Expand Down Expand Up @@ -915,6 +939,8 @@ def initialized(pelican: Pelican):
}
}

pelican.settings.setdefault("PHOTO_RESULT_IMAGE_AVERAGE_COLOR", False)

global pelican_settings
pelican_settings = pelican.settings
global pelican_output_path
Expand Down

0 comments on commit 2561b6b

Please sign in to comment.