-
-
Save jeepkd/6f372ce7c4aaadab57ce3c9fcb3039d4 to your computer and use it in GitHub Desktop.
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
import PIL | |
import numpy | |
def scale(image, max_size, method=PIL.Image.ANTIALIAS): | |
""" | |
resize 'image' to 'max_size' keeping the aspect ratio | |
and place it in center of a noisy 'max_size' image | |
""" | |
im_aspect = float(image.size[0])/float(image.size[1]) | |
out_aspect = float(max_size[0])/float(max_size[1]) | |
if im_aspect >= out_aspect: | |
scaled = image.resize((max_size[0], int((float(max_size[0])/im_aspect) + 0.5)), method) | |
else: | |
scaled = image.resize((int((float(max_size[1])*im_aspect) + 0.5), max_size[1]), method) | |
offset = (((max_size[0] - scaled.size[0]) / 2), ((max_size[1] - scaled.size[1]) / 2)) | |
imarray = numpy.random.rand(max_size[0], max_size[1], 3) * 256 | |
back = PIL.Image.fromarray(imarray.astype('uint8')).convert('RGB') | |
back.paste(scaled, offset) | |
return back |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment