Commit 88367ce 1 parent b30ddfe commit 88367ce Copy full SHA for 88367ce
File tree 4 files changed +60
-0
lines changed
4 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 1
1
import pip
2
2
import sys
3
+ import os
4
+
3
5
try :
4
6
argument = sys .argv [1 ].lower ()
5
7
except :
8
10
def install ():
9
11
pip .main (['install' , '-r' , 'requirements.txt' ])
10
12
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
+
11
23
if __name__ == "__main__" :
12
24
install ()
Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments