Skip to content

Commit 88367ce

Browse files
committed
added image processing
1 parent b30ddfe commit 88367ce

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

back_end/image.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from PIL import Image
2+
import base64
3+
from io import BytesIO
4+
import imghdr
5+
6+
7+
iconsize = 256
8+
9+
def open_image(data, b64=False):
10+
"""Returns an image object, given image binary data (gotten from request.get_file) """
11+
if b64:
12+
data = base64.b64decode(data)
13+
14+
io = BytesIO(data)
15+
16+
image_type = imghdr.what(io)
17+
if image_type:
18+
return (Image.open(io), image_type)
19+
else:
20+
return None
21+
22+
23+
def to_icon(img):
24+
"""Takes an Image object, and mutates it into a thumbname with the size iconsize"""
25+
reducerate = 1 # the ratio to times the img height and width
26+
if max(img.height, img.width) > iconsize:
27+
reducerate = 256 / max(img.height, img.width)
28+
29+
return img.resize((int(img.width*reducerate), int(img.height*reducerate)), Image.ANTIALIAS)

setup.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pip
22
import sys
3+
import os
4+
35
try:
46
argument = sys.argv[1].lower()
57
except:
@@ -8,5 +10,15 @@
810
def install():
911
pip.main(['install', '-r', 'requirements.txt'])
1012

13+
# make the database
14+
if not os.path.exists('db.db'):
15+
from subprocess import call
16+
try:
17+
call(["python", "db/db_create.py"])
18+
except:
19+
call(["python3", "db/db_create.py"])
20+
21+
22+
1123
if __name__ == "__main__":
1224
install()

unittests/image/correct1.jpg

11.7 KB
Loading

unittests/image/image.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import back_end.image as img
2+
import unittest
3+
4+
class TestImageMethods(unittest.TestCase):
5+
def test_image1(self):
6+
x = img.open_image(open('correct1.jpg', 'rb').read())
7+
self.assertFalse(isinstance(x, type(None)))
8+
self.assertEqual(x[1], 'jpeg')
9+
10+
def test_image2(self):
11+
x = img.open_image(open('correct1.jpg', 'rb').read())[0]
12+
x.save("out1.jpg")
13+
x = img.to_icon(x)
14+
x.save("out2.jpg")
15+
self.assertFalse(False)
16+
17+
18+
if __name__ == "__main__":
19+
unittest.main()

0 commit comments

Comments
 (0)