| 1 | ################################################################################
|
|---|
| 2 | # _pilutil.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Imports.
|
|---|
| 20 | import tempfile
|
|---|
| 21 | # Product Imports.
|
|---|
| 22 | import _fileutil
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | class pilutil:
|
|---|
| 26 |
|
|---|
| 27 | def __init__(self, context):
|
|---|
| 28 | self.context = context
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | enabled__roles__ = None
|
|---|
| 32 | def enabled(self):
|
|---|
| 33 | try:
|
|---|
| 34 | from PIL import Image
|
|---|
| 35 | return True
|
|---|
| 36 | except:
|
|---|
| 37 | return False
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | thumbnail__roles__ = None
|
|---|
| 41 | def thumbnail(self, img, maxdim, qual=75):
|
|---|
| 42 | # Resize image
|
|---|
| 43 | size = (maxdim, maxdim)
|
|---|
| 44 | thumb = self.resize( img, size, mode='thumbnail', qual=qual)
|
|---|
| 45 |
|
|---|
| 46 | # Returns resulting image
|
|---|
| 47 | image = self.context.ImageFromData(thumb.getData(),thumb.getFilename())
|
|---|
| 48 | return image
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | resize__roles__ = None
|
|---|
| 52 | def resize(self, img, size, mode='resize', sffx='_thumbnail', qual=75):
|
|---|
| 53 | """
|
|---|
| 54 | Resize image.
|
|---|
| 55 | """
|
|---|
| 56 | try:
|
|---|
| 57 | from PIL import Image
|
|---|
| 58 | except:
|
|---|
| 59 | pass
|
|---|
| 60 |
|
|---|
| 61 | # Save image in temp-folder
|
|---|
| 62 | tempfolder = tempfile.mktemp()
|
|---|
| 63 | filepath = _fileutil.getOSPath('%s/%s'%(tempfolder,img.filename))
|
|---|
| 64 | _fileutil.exportObj(img,filepath)
|
|---|
| 65 |
|
|---|
| 66 | # Resize image
|
|---|
| 67 | im = Image.open(filepath)
|
|---|
| 68 | im = im.convert('RGB')
|
|---|
| 69 | maxdim = max(list(size))
|
|---|
| 70 | if mode == 'thumbnail':
|
|---|
| 71 | try:
|
|---|
| 72 | im.thumbnail((maxdim,maxdim),Image.ANTIALIAS)
|
|---|
| 73 | except:
|
|---|
| 74 | im.thumbnail((maxdim,maxdim))
|
|---|
| 75 | im.save(infile,"JPEG", quality=qual)
|
|---|
| 76 | elif mode == 'resize':
|
|---|
| 77 | try:
|
|---|
| 78 | im = im.resize(size,Image.ANTIALIAS)
|
|---|
| 79 | except:
|
|---|
| 80 | im = im.resize(size)
|
|---|
| 81 | elif mode == 'square':
|
|---|
| 82 | try:
|
|---|
| 83 | width, height = im.size
|
|---|
| 84 | dst_width, dst_height = maxdim, maxdim
|
|---|
| 85 | if width > height:
|
|---|
| 86 | delta = width - height
|
|---|
| 87 | left = int(delta/2)
|
|---|
| 88 | upper = 0
|
|---|
| 89 | right = height + left
|
|---|
| 90 | lower = height
|
|---|
| 91 | else:
|
|---|
| 92 | delta = height - width
|
|---|
| 93 | left = 0
|
|---|
| 94 | upper = int(delta/2)
|
|---|
| 95 | right = width
|
|---|
| 96 | lower = width + upper
|
|---|
| 97 | im = im.crop(( left, upper, right, lower))
|
|---|
| 98 | im = im.resize((dst_width, dst_height), Image.ANTIALIAS)
|
|---|
| 99 | except:
|
|---|
| 100 | im.resize(size)
|
|---|
| 101 | im.save(filepath,"JPEG", quality=qual)
|
|---|
| 102 |
|
|---|
| 103 | # Read resized image from file-system
|
|---|
| 104 | f = open(filepath,'rb')
|
|---|
| 105 | result_data = f.read()
|
|---|
| 106 | thumb_sffx = str(sffx)
|
|---|
| 107 | getfilename = _fileutil.extractFilename(filepath).split('.')
|
|---|
| 108 | filename = getfilename[0:-1]
|
|---|
| 109 | filename = ".".join(filename)
|
|---|
| 110 | filename = filename.replace('.','_')
|
|---|
| 111 | extension = _fileutil.extractFileExt(filepath)
|
|---|
| 112 | result_filename = filename + thumb_sffx + '.' + extension
|
|---|
| 113 | result = {'data':result_data,'filename':result_filename}
|
|---|
| 114 | f.close()
|
|---|
| 115 |
|
|---|
| 116 | # Remove temp-folder and images
|
|---|
| 117 | _fileutil.remove(tempfolder,deep=1)
|
|---|
| 118 |
|
|---|
| 119 | # Returns resulting image
|
|---|
| 120 | image = self.context.ImageFromData(result['data'],result['filename'])
|
|---|
| 121 | return image
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | crop__roles__ = None
|
|---|
| 125 | def crop(self, img, box, qual=75):
|
|---|
| 126 | """
|
|---|
| 127 | Crop image.
|
|---|
| 128 | """
|
|---|
| 129 | try:
|
|---|
| 130 | from PIL import Image
|
|---|
| 131 | except:
|
|---|
| 132 | pass
|
|---|
| 133 |
|
|---|
| 134 | # Save image in temp-folder
|
|---|
| 135 | tempfolder = tempfile.mktemp()
|
|---|
| 136 | filepath = _fileutil.getOSPath('%s/%s'%(tempfolder,img.filename))
|
|---|
| 137 | _fileutil.exportObj(img,filepath)
|
|---|
| 138 |
|
|---|
| 139 | # Crop image
|
|---|
| 140 | im = Image.open(filepath)
|
|---|
| 141 | im = im.crop(box)
|
|---|
| 142 | im.save(filepath,"JPEG", quality=qual)
|
|---|
| 143 |
|
|---|
| 144 | # Read resized image from file-system
|
|---|
| 145 | f = open(filepath,'rb')
|
|---|
| 146 | result_data = f.read()
|
|---|
| 147 | result_filename = _fileutil.extractFilename(filepath)
|
|---|
| 148 | result = {'data':result_data,'filename':result_filename}
|
|---|
| 149 | f.close()
|
|---|
| 150 |
|
|---|
| 151 | # Remove temp-folder and images
|
|---|
| 152 | _fileutil.remove(tempfolder,deep=1)
|
|---|
| 153 |
|
|---|
| 154 | # Returns resulting image
|
|---|
| 155 | image = self.context.ImageFromData(result['data'],result['filename'])
|
|---|
| 156 | return image
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | rotate__roles__ = None
|
|---|
| 160 | def rotate( self, img, direction, qual=75):
|
|---|
| 161 | """
|
|---|
| 162 | Rotate image.
|
|---|
| 163 | """
|
|---|
| 164 | try:
|
|---|
| 165 | from PIL import Image
|
|---|
| 166 | except:
|
|---|
| 167 | pass
|
|---|
| 168 |
|
|---|
| 169 | # Save image in temp-folder
|
|---|
| 170 | tempfolder = tempfile.mktemp()
|
|---|
| 171 | filepath = _fileutil.getOSPath('%s/%s'%(tempfolder,img.filename))
|
|---|
| 172 | _fileutil.exportObj(img,filepath)
|
|---|
| 173 |
|
|---|
| 174 | # Rotate image
|
|---|
| 175 | im = Image.open(filepath)
|
|---|
| 176 | im = im.rotate(direction)
|
|---|
| 177 | im.save(filepath,"JPEG", quality=qual)
|
|---|
| 178 |
|
|---|
| 179 | # Read resized image from file-system
|
|---|
| 180 | f = open(filepath,'rb')
|
|---|
| 181 | result_data = f.read()
|
|---|
| 182 | result_filename = _fileutil.extractFilename(filepath)
|
|---|
| 183 | result = {'data':result_data,'filename':result_filename}
|
|---|
| 184 | f.close()
|
|---|
| 185 |
|
|---|
| 186 | # Remove temp-folder and images
|
|---|
| 187 | _fileutil.remove(tempfolder,deep=1)
|
|---|
| 188 |
|
|---|
| 189 | # Returns resulting image
|
|---|
| 190 | image = self.context.ImageFromData(result['data'],result['filename'])
|
|---|
| 191 | return image
|
|---|
| 192 |
|
|---|
| 193 | ################################################################################ |
|---|