-
Notifications
You must be signed in to change notification settings - Fork 4
/
images.py
97 lines (82 loc) · 3.07 KB
/
images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__version__ = '0.1'
__author__ = 'Andrey Derevyagin'
__copyright__ = 'Copyright © 2014'
from PIL import Image
import json
import requests
try:
from gdrive_cdn import UploadToCDN
except ImportError, e:
pass
import StringIO
class UploadToDeviantsart(object):
def __init__(self):
super(UploadToDeviantsart, self).__init__()
def upload(self, content, content_type='image/jpeg', filename='image.jpg'):
r = requests.post('http://deviantsart.com', files={'file': (filename, content, content_type)})
try:
rv = json.loads(r.text)
except ValueError:
return False
return rv.get('url', False)
class ImageManager(object):
def __init__(self):
super(ImageManager, self).__init__()
def upload(self, image_data, content_type='image/jpeg', filename='image.jpg'):
return UploadToDeviantsart().upload(image_data, content_type, filename)
def upload_user_profile_image(self, img_data, filename='image.jpg'):
try:
cdn = UploadToCDN()
except NameError, e:
cdn = UploadToDeviantsart()
im = Image.open(img_data)
(width, height) = im.size
if width > 200 or height > 200:
im.thumbnail((200, 200), Image.ANTIALIAS)
dt = StringIO.StringIO()
im.save(dt, format="JPEG", quality=95)
url = cdn.upload(dt.getvalue(), content_type='image/jpeg', filename=filename)
dt.close()
return url
def crop_and_upload_profile_image(self, img_data, filename='kanojo', upload_full_image=True):
try:
cdn = UploadToCDN()
except NameError, e:
cdn = UploadToDeviantsart()
im = Image.open(img_data)
cr = im.crop((94, 40, 170+94, 170+40))
cr.thumbnail((88, 88), Image.ANTIALIAS)
dt = StringIO.StringIO()
cr.save(dt, format="png")
crop_url = cdn.upload(dt.getvalue(), content_type='image/png', filename=u'%ss.png'%filename)
dt.close()
full_url = None
if cdn and upload_full_image:
dt = StringIO.StringIO()
im.save(dt, format="png")
full_url = cdn.upload(dt.getvalue(), content_type='image/png', filename=u'%s.png'%filename)
dt.close()
return (crop_url, full_url)
if __name__=='__main__':
im = Image.open('1.png')
#im = Image.open(StringIO.StringIO(buffer))
cr = im.crop((94, 40, 170+94, 170+40))
cr.thumbnail((88, 88), Image.ANTIALIAS)
dt = StringIO.StringIO()
cr.save(dt, format="png")
crop_url = UploadToCDN().upload(dt.getvalue(), content_type='image/png', filename=u'best_girl.png')
#crop_url = UploadToDeviantsart().upload(dt.getvalue(), content_type='image/png')
dt.close()
full_url = None
try:
cdn = UploadToCDN()
except NameError, e:
cdn = None
if cdn:
dt = StringIO.StringIO()
im.save(dt, format="png")
full_url = UploadToCDN().upload(dt.getvalue(), content_type='image/png', filename=u'fk.png')
dt.close()
print crop_url, full_url