Skip to content

Commit

Permalink
added image processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackywathy committed Feb 2, 2017
1 parent b30ddfe commit 88367ce
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
29 changes: 29 additions & 0 deletions back_end/image.py
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)
12 changes: 12 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pip
import sys
import os

try:
argument = sys.argv[1].lower()
except:
Expand All @@ -8,5 +10,15 @@
def install():
pip.main(['install', '-r', 'requirements.txt'])

# make the database
if not os.path.exists('db.db'):
from subprocess import call
try:
call(["python", "db/db_create.py"])
except:
call(["python3", "db/db_create.py"])



if __name__ == "__main__":
install()
Binary file added unittests/image/correct1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions unittests/image/image.py
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()

2 comments on commit 88367ce

@tracey-le
Copy link
Collaborator

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

@tracey-le
Copy link
Collaborator

@tracey-le tracey-le commented on 88367ce Feb 2, 2017

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 :) )

Please sign in to comment.