-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file cache for bypassing re-rendering images already created
- Loading branch information
Lorena Mesa
committed
Jul 17, 2024
1 parent
7ddbcbd
commit 01ad24e
Showing
4 changed files
with
90 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ruff | ||
Pillow | ||
Pillow | ||
sweepai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,20 @@ | |
|
||
import argparse | ||
import hashlib | ||
import io | ||
from PIL import Image, ImageDraw | ||
import time | ||
|
||
from sweepai.logn.cache import file_cache | ||
|
||
__author__ = "Lorena Mesa" | ||
__email__ = "[email protected]" | ||
|
||
|
||
class Identicon: | ||
|
||
def __init__(self, input_str: str) -> None: | ||
self.md5hash_str: str = self._convert_string_to_sha_hash(input_str) | ||
def __init__(self) -> None: | ||
self.md5hash_str: str = None | ||
self.grid_size: int = 5 | ||
self.square_size: int = 64 | ||
self.identicon_size: tuple = (self.grid_size * self.square_size, self.grid_size * self.square_size) | ||
|
@@ -58,18 +62,24 @@ def _generate_pixel_fill_color(self, md5hash_str: str) -> tuple: | |
""" | ||
return tuple(int(md5hash_str[i:i+2], base=16) for i in range(0, 2*3, 2)) | ||
|
||
def render(self, filename: str=None, dimensions: int=0) -> Image: | ||
@file_cache() | ||
def render(self, input_str: str, filename: str="identicon", dimensions: int=0) -> Image: | ||
""" | ||
Function that generates a grid - a list of lists - indicating which pixels | ||
are to be filled and uses the md5hash_str to generate an image fill color. | ||
Function creates a PIL Image, drawing it, and saving it. By default a 320 | ||
pixel by 320 pixel identicon is rendered, if upon executing the code a | ||
dimensions parameter is passed in the image will be resized. | ||
:param input_str: unique identifer input string used to generate identicon | ||
:param filename: filename of PIL png image generated | ||
:return: None | ||
""" | ||
|
||
# Can uncomment to confirm the @file_cache is working | ||
# import time; time.sleep(5) | ||
|
||
self.md5hash_str = self._convert_string_to_sha_hash(input_str) | ||
fill_color: tuple = self._generate_pixel_fill_color(self.md5hash_str) | ||
grid: list[list] = self._build_grid() | ||
|
||
|
@@ -96,19 +106,25 @@ def render(self, filename: str=None, dimensions: int=0) -> Image: | |
row * self.square_size + self.square_size | ||
] | ||
draw.rectangle(shape_coords, fill=fill_color) | ||
|
||
if not filename: | ||
filename: str = 'example' | ||
|
||
if dimensions: | ||
# Possible resampling filters here: https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize | ||
# BICUBIC and LANCZOS take longer to process than NEAREST, but the quality of the former is better. | ||
width_percent: float = (dimensions / float(image.size[0])) | ||
height: int = int((float(image.size[1]) * float(width_percent))) | ||
image = image.resize((dimensions, height), Image.Resampling.LANCZOS) | ||
|
||
image.save(f'{filename}.png') | ||
|
||
# Return a unique string with the input str value and the image bytes array | ||
# to allow a cache hit | ||
|
||
byteIO = io.BytesIO() | ||
image.save(byteIO, format='PNG') | ||
im_bytes = byteIO.getvalue() | ||
# import pdb; pdb.set_trace() | ||
return f'{input_str}_{im_bytes}' | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
|
@@ -140,7 +156,8 @@ def dimensions_gt_zero(input_dimensions: str): | |
"-o", | ||
"--output", | ||
type=len_gt_zero, | ||
help="Name for output square identicon image generated.", | ||
help="Name for output square identicon PNG image generated.", | ||
default='identicon' | ||
) | ||
parser.add_argument( | ||
"-d", | ||
|
@@ -151,5 +168,9 @@ def dimensions_gt_zero(input_dimensions: str): | |
|
||
args = parser.parse_args() | ||
|
||
identicon = Identicon(input_str=args.string) | ||
identicon.render(filename=args.output, dimensions=args.dimensions) | ||
# Add timer to confirm performance of code | ||
t0 = time.time() | ||
identicon = Identicon() | ||
result = identicon.render(input_str=args.string, filename=args.output, dimensions=args.dimensions) | ||
t1 = time.time() | ||
print(f"{t1-t0} seconds to render {args.output}.png is now available to download!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters