|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # $File: facepp.py |
3 | | -# $Date: Fri Apr 05 17:29:44 2013 +0800 |
| 3 | +# $Date: Thu May 16 14:59:36 2013 +0800 |
4 | 4 | # $Author: jiakai@megvii.com |
5 | 5 | # |
6 | 6 | # This program is free software. It comes without any warranty, to |
|
39 | 39 | import urllib2 |
40 | 40 | import json |
41 | 41 | import os |
| 42 | +import os.path |
42 | 43 | import itertools |
43 | 44 | import mimetools |
44 | 45 | import mimetypes |
45 | 46 | import time |
| 47 | +import tempfile |
46 | 48 | from collections import Iterable |
47 | 49 | from cStringIO import StringIO |
48 | 50 |
|
49 | 51 | class File(object): |
50 | 52 | """an object representing a local file""" |
51 | 53 | path = None |
| 54 | + content = None |
52 | 55 | def __init__(self, path): |
53 | 56 | self.path = path |
54 | | - |
55 | | - def get_content(self): |
56 | | - with open(self.path, 'rb') as f: |
57 | | - return f.read() |
| 57 | + self._get_content() |
| 58 | + |
| 59 | + def _resize_cv2(self, ftmp): |
| 60 | + try: |
| 61 | + import cv2 |
| 62 | + except ImportError: |
| 63 | + return False |
| 64 | + img = cv2.imread(self.path) |
| 65 | + assert img is not None and img.size != 0, 'Invalid image' |
| 66 | + bigdim = max(img.shape[0], img.shape[1]) |
| 67 | + downscale = max(1., bigdim / 600.) |
| 68 | + img = cv2.resize(img, |
| 69 | + (int(img.shape[1] / downscale), |
| 70 | + int(img.shape[0] / downscale))) |
| 71 | + cv2.imwrite(ftmp, img) |
| 72 | + return True |
| 73 | + |
| 74 | + def _resize_PIL(self, ftmp): |
| 75 | + try: |
| 76 | + import PIL.Image |
| 77 | + except ImportError: |
| 78 | + return False |
| 79 | + |
| 80 | + img = PIL.Image.open(self.path) |
| 81 | + bigdim = max(img.size[0], img.size[1]) |
| 82 | + downscale = max(1., bigdim / 600.) |
| 83 | + img = img.resize( |
| 84 | + (int(img.size[0] / downscale), int(img.size[1] / downscale))) |
| 85 | + img.save(ftmp) |
| 86 | + return True |
| 87 | + |
| 88 | + def _get_content(self): |
| 89 | + """read image content; resize the image if necessary""" |
| 90 | + |
| 91 | + if os.path.getsize(self.path) > 2 * 1024 * 1024: |
| 92 | + ftmp = tempfile.NamedTemporaryFile( |
| 93 | + suffix = '.jpg', delete = False).name |
| 94 | + try: |
| 95 | + if not (self._resize_cv2(ftmp) or self._resize_PIL(ftmp)): |
| 96 | + raise APIError(-1, None, 'image file size too large') |
| 97 | + with open(ftmp, 'rb') as f: |
| 98 | + self.content = f.read() |
| 99 | + finally: |
| 100 | + os.unlink(ftmp) |
| 101 | + else: |
| 102 | + with open(self.path, 'rb') as f: |
| 103 | + self.content = f.read() |
58 | 104 |
|
59 | 105 | def get_filename(self): |
60 | | - return self.path |
| 106 | + return os.path.basename(self.path) |
61 | 107 |
|
62 | 108 |
|
63 | 109 | class APIError(Exception): |
@@ -162,7 +208,7 @@ def __call__(self, post = False, *args, **kargs): |
162 | 208 | for (k, v) in kargs.iteritems(): |
163 | 209 | if isinstance(v, File): |
164 | 210 | add_form = True |
165 | | - form.add_file(k, v.get_filename(), v.get_content()) |
| 211 | + form.add_file(k, v.get_filename(), v.content) |
166 | 212 |
|
167 | 213 | if post: |
168 | 214 | url = self._urlbase |
|
0 commit comments