Created
April 19, 2014 09:29
-
-
Save kurap/11079157 to your computer and use it in GitHub Desktop.
image resizing using PythonImagingLibrary(PIL)
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
class resize: | |
def __init__(self, input_image): | |
self.input_image = input_image | |
self.output_image = StringIO() | |
def resize_image(self, width=400, height=300, aspecto_width=4, aspect_height=3): | |
""" | |
resize image | |
:param self | |
:param width default=200px | |
:param height default=150px | |
""" | |
# im memory instance | |
if not self.input_image: | |
return None | |
im = Image.open(StringIO(self.input_image.getvalue())) | |
w, h = im.size | |
print w | |
print h | |
left = 0 | |
right = w | |
a = h - ((aspect_height * float(w)) / aspecto_width) | |
top = int(a / 2) | |
print top | |
bottom = h - top | |
print bottom | |
if im.mode != 'RGB': | |
im.convert('RGB') | |
cropped = im.crop((left, top, right, bottom)) | |
cropped.thumbnail((width, height), Image.ANTIALIAS) | |
cropped.save(self.image_43, quality=85, format='JPEG') | |
util_image = resize(input_image=dest_file) | |
util_image.resize_image() | |
save_file = util.image | |
new_instance.image1.save(save_file_name, ContentFile(util_image.resize_image.getvalue())) | |
path = new_instance.image1.name | |
util_image.resize_image.truncate(0) | |
dest_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment