Skip to content

Commit

Permalink
Merge pull request #49 from ykiu/reduce-memory-usage
Browse files Browse the repository at this point in the history
Make processors.Resize memory-efficient
  • Loading branch information
vstoykov authored Jan 14, 2021
2 parents 388c651 + 809f488 commit b0b19d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pilkit/processors/resize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .base import Anchor
from .utils import resolve_palette
from ..lib import Image


Expand All @@ -20,7 +21,7 @@ def __init__(self, width, height, upscale=True):

def process(self, img):
if self.upscale or (self.width < img.size[0] and self.height < img.size[1]):
img = img.convert('RGBA')
img = resolve_palette(img)
img = img.resize((self.width, self.height), Image.ANTIALIAS)
return img

Expand Down
12 changes: 11 additions & 1 deletion pilkit/processors/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ def histogram_entropy_py(image):

# Select the Pillow native histogram entropy function - if
# available - and fall back to the Python implementation:
histogram_entropy = getattr(Image.Image, 'entropy', histogram_entropy_py)
histogram_entropy = getattr(Image.Image, 'entropy', histogram_entropy_py)

def resolve_palette(image):
""" Convert a palette image to a non-palette image. """

# We need to load the image before accessing the palette
image.load()

if image.palette is None:
return image
return image.convert(image.palette.mode)

0 comments on commit b0b19d1

Please sign in to comment.