fix: improve redraw speed
This commit is contained in:
+10
-2
@@ -1,3 +1,5 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from PIL import Image, ImageColor, ImageDraw
|
||||
|
||||
|
||||
@@ -26,6 +28,12 @@ COLORS = {
|
||||
}
|
||||
|
||||
|
||||
@lru_cache(maxsize=4)
|
||||
def _load_rgba_source(image_path):
|
||||
"""Load and cache a source image in RGBA form for repeated resizes."""
|
||||
return Image.open(image_path).convert("RGBA")
|
||||
|
||||
|
||||
def _rgba(color, alpha=255):
|
||||
red, green, blue = ImageColor.getrgb(color)
|
||||
return red, green, blue, alpha
|
||||
@@ -187,7 +195,7 @@ def load_cover_background(image_path, width, height, *, overlay_color=None, over
|
||||
width = max(1, int(width))
|
||||
height = max(1, int(height))
|
||||
|
||||
image = Image.open(image_path).convert("RGBA")
|
||||
image = _load_rgba_source(image_path)
|
||||
source_ratio = image.width / image.height
|
||||
target_ratio = width / height
|
||||
|
||||
@@ -198,7 +206,7 @@ def load_cover_background(image_path, width, height, *, overlay_color=None, over
|
||||
new_width = width
|
||||
new_height = int(width / source_ratio)
|
||||
|
||||
image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
||||
image = image.resize((new_width, new_height), Image.Resampling.BICUBIC)
|
||||
left = (new_width - width) // 2
|
||||
top = (new_height - height) // 2
|
||||
image = image.crop((left, top, left + width, top + height))
|
||||
|
||||
Reference in New Issue
Block a user