Skip to content
Permalink
Browse files

better resizing algorithm

  • Loading branch information...
shawnmjones committed May 26, 2019
1 parent fef0b62 commit ede81cc78ac95ecf3e2525925592b1794e9b9dec
Showing with 21 additions and 48 deletions.
  1. +1 −1 config/default.py
  2. +20 −47 mementoembed/mementoimagereel.py
@@ -20,6 +20,6 @@
DEFAULT_IMAGE_PATH = "mementoembed/static/images/96px-Sphere_wireframe.svg.png"
IMAGEREEL_WORKING_FOLDER = "/tmp/mementoembed/imagereels"
IMAGEREEL_DURATION = 100
IMAGEREEL_COUNT = 20
IMAGEREEL_COUNT = 5
IMAGEREEL_WIDTH = 96
IMAGEREEL_HEIGHT = 96
@@ -72,26 +72,8 @@ def generate_imagereel(self, urim, duration, countlimit, requested_width, reques

if imagecount > countlimit:
break

maxwidth = 0
maxheight = 0

# get width, height of largest image to create a base image for others
for im in baseims:

module_logger.debug("image size is {}".format(im.size))

if im.size[0] > maxwidth:
maxwidth = im.size[0]

if im.size[1] > maxheight:
maxheight = im.size[1]

# TODO: should we just make a square of the greatest one?

module_logger.debug("creating a base image of size w:{} x h:{}".format(maxwidth, maxheight))

imout = Image.new("RGBA", (requested_width, requested_height))
imout = Image.new("RGBA", (requested_width, requested_height), "black")
imbase = Image.new("RGBA", (requested_width, requested_height), "black")

working_ims = []
@@ -101,43 +83,32 @@ def generate_imagereel(self, urim, duration, countlimit, requested_width, reques
im_width = im.size[0]
im_height = im.size[1]

# is the width or height greater
if im_width > im_height:
# width is greater

if maxwidth < requested_width:
newwidth = maxwidth
newheight = (maxwidth / im_width) * im_height
else:
newwidth = requested_width
newheight = (requested_width / im_width) * im_height
module_logger.debug("image size is {}".format(im.size))

if im_width > im_height:
newwidth = requested_width
newheight = (requested_width / im_width) * im_height

elif im_height > im_width:
# height is greater

if maxheight > requested_height:
newheight = maxheight
newwidth = (maxheight / im_height) * im_width
else:
newheight = requested_height
newwidth = (requested_height / im_height) * im_width
newheight = requested_height
newwidth = (requested_height / im_height) * im_width

elif im_height == im_width:
# either works fine
newheight = (requested_width / im_width) * im_height
newwidth = (requested_height / im_height) * im_width

module_logger.debug("resizing")

if maxwidth < requested_width:
newheight = (maxheight / im_height) * im_height
newwidth = (maxheight / im_height) * im_width
else:
newheight = (requested_height / im_height) * im_height
newwidth = (requested_height / im_height) * im_width
module_logger.debug("newimage size is {}".format( (int(newwidth), int(newheight)) ))

im = im.resize((int(newwidth), int(newheight)))
im = im.resize((int(newwidth), int(newheight)), resample=Image.BICUBIC)

working_ims.append(im)

outputims = []

module_logger.debug("building animated GIF")

# Thanks: https://stackoverflow.com/questions/2563822/how-do-you-composite-an-image-onto-another-image-with-pil-in-python
for im in working_ims:

@@ -154,17 +125,19 @@ def generate_imagereel(self, urim, duration, countlimit, requested_width, reques

outputims.append(imbase)
# TODO: loop this! also, maybe do increments of 0.01?
for i in range(1, 99, 5):
for i in range(1, 99, 10):
i = i / 100
outputims.append( Image.blend(imbase, newim, i))

for i in range(0, 30):
outputims.append(newim)

for i in range(1, 99, 5):
for i in range(1, 99, 10):
i = i / 100
outputims.append( Image.blend(newim, imbase, i) )

module_logger.debug("saving animated GIF")

with open(reelfile, 'wb') as outputfp:
imout.save(
outputfp, save_all=True, format="GIF",

0 comments on commit ede81cc

Please sign in to comment.
You can’t perform that action at this time.