-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b30ddfe
commit 88367ce
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from PIL import Image | ||
import base64 | ||
from io import BytesIO | ||
import imghdr | ||
|
||
|
||
iconsize = 256 | ||
|
||
def open_image(data, b64=False): | ||
"""Returns an image object, given image binary data (gotten from request.get_file) """ | ||
if b64: | ||
data = base64.b64decode(data) | ||
|
||
io = BytesIO(data) | ||
|
||
image_type = imghdr.what(io) | ||
if image_type: | ||
return (Image.open(io), image_type) | ||
else: | ||
return None | ||
|
||
|
||
def to_icon(img): | ||
"""Takes an Image object, and mutates it into a thumbname with the size iconsize""" | ||
reducerate = 1 # the ratio to times the img height and width | ||
if max(img.height, img.width) > iconsize: | ||
reducerate = 256 / max(img.height, img.width) | ||
|
||
return img.resize((int(img.width*reducerate), int(img.height*reducerate)), Image.ANTIALIAS) |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import back_end.image as img | ||
import unittest | ||
|
||
class TestImageMethods(unittest.TestCase): | ||
def test_image1(self): | ||
x = img.open_image(open('correct1.jpg', 'rb').read()) | ||
self.assertFalse(isinstance(x, type(None))) | ||
self.assertEqual(x[1], 'jpeg') | ||
|
||
def test_image2(self): | ||
x = img.open_image(open('correct1.jpg', 'rb').read())[0] | ||
x.save("out1.jpg") | ||
x = img.to_icon(x) | ||
x.save("out2.jpg") | ||
self.assertFalse(False) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
88367ce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omg @Jackywathy you are awesome
88367ce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#38 (referencing for if maybe related :) )