-
Notifications
You must be signed in to change notification settings - Fork 0
/
_pilutil.py
197 lines (166 loc) · 5.64 KB
/
_pilutil.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
################################################################################
# _pilutil.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
import tempfile
# Product Imports.
import _fileutil
class pilutil:
def __init__(self, context):
self.context = context
enabled__roles__ = None
def enabled(self):
try:
from PIL import Image
return True
except:
try:
import Image
return True
except:
return False
thumbnail__roles__ = None
def thumbnail(self, img, maxdim, qual=75):
# Resize image
size = (maxdim, maxdim)
thumb = self.resize( img, size, mode='thumbnail', qual=qual)
# Returns resulting image
image = self.context.ImageFromData(thumb.getData(),thumb.getFilename())
return image
resize__roles__ = None
def resize(self, img, size, mode='resize', sffx='_thumbnail', qual=75):
"""
Resize image.
"""
try:
from PIL import Image
except:
import Image
# Save image in temp-folder
tempfolder = tempfile.mktemp()
filepath = _fileutil.getOSPath('%s/%s'%(tempfolder,img.filename))
_fileutil.exportObj(img,filepath)
# Resize image
im = Image.open(filepath)
im = im.convert('RGB')
maxdim = max(list(size))
if mode == 'thumbnail':
try:
im.thumbnail((maxdim,maxdim),Image.ANTIALIAS)
except:
im.thumbnail((maxdim,maxdim))
im.save(infile,"JPEG", quality=qual)
elif mode == 'resize':
try:
im = im.resize(size,Image.ANTIALIAS)
except:
im = im.resize(size)
elif mode == 'square':
try:
width, height = im.size
dst_width, dst_height = maxdim, maxdim
if width > height:
delta = width - height
left = int(delta/2)
upper = 0
right = height + left
lower = height
else:
delta = height - width
left = 0
upper = int(delta/2)
right = width
lower = width + upper
im = im.crop(( left, upper, right, lower))
im = im.resize((dst_width, dst_height), Image.ANTIALIAS)
except:
im.resize(size)
im.save(filepath,"JPEG", quality=qual)
# Read resized image from file-system
f = open(filepath,'rb')
result_data = f.read()
thumb_sffx = str(sffx)
getfilename = _fileutil.extractFilename(filepath).split('.')
filename = getfilename[0:-1]
filename = ".".join(filename)
filename = filename.replace('.','_')
extension = _fileutil.extractFileExt(filepath)
result_filename = filename + thumb_sffx + '.' + extension
result = {'data':result_data,'filename':result_filename}
f.close()
# Remove temp-folder and images
_fileutil.remove(tempfolder,deep=1)
# Returns resulting image
image = self.context.ImageFromData(result['data'],result['filename'])
return image
crop__roles__ = None
def crop(self, img, box, qual=75):
"""
Crop image.
"""
try:
from PIL import Image
except:
import Image
# Save image in temp-folder
tempfolder = tempfile.mktemp()
filepath = _fileutil.getOSPath('%s/%s'%(tempfolder,img.filename))
_fileutil.exportObj(img,filepath)
# Crop image
im = Image.open(filepath)
im = im.crop(box)
im.save(filepath,"JPEG", quality=qual)
# Read resized image from file-system
f = open(filepath,'rb')
result_data = f.read()
result_filename = _fileutil.extractFilename(filepath)
result = {'data':result_data,'filename':result_filename}
f.close()
# Remove temp-folder and images
_fileutil.remove(tempfolder,deep=1)
# Returns resulting image
image = self.context.ImageFromData(result['data'],result['filename'])
return image
rotate__roles__ = None
def rotate( self, img, direction, qual=75):
"""
Rotate image.
"""
try:
from PIL import Image
except:
import Image
# Save image in temp-folder
tempfolder = tempfile.mktemp()
filepath = _fileutil.getOSPath('%s/%s'%(tempfolder,img.filename))
_fileutil.exportObj(img,filepath)
# Rotate image
im = Image.open(filepath)
im = im.rotate(direction)
im.save(filepath,"JPEG", quality=qual)
# Read resized image from file-system
f = open(filepath,'rb')
result_data = f.read()
result_filename = _fileutil.extractFilename(filepath)
result = {'data':result_data,'filename':result_filename}
f.close()
# Remove temp-folder and images
_fileutil.remove(tempfolder,deep=1)
# Returns resulting image
image = self.context.ImageFromData(result['data'],result['filename'])
return image
################################################################################