Back to index

plone3  3.1.7
Public Member Functions | Static Public Attributes
ATContentTypes.lib.imagetransform.ATCTImageTransform Class Reference
Inheritance diagram for ATContentTypes.lib.imagetransform.ATCTImageTransform:
Inheritance graph
[legend]
Collaboration diagram for ATContentTypes.lib.imagetransform.ATCTImageTransform:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def getImageAsFile
def getEXIF
def getEXIFOrientation
def getEXIFOrigDate
def transformImage
def autoTransformImage
def getTransformMap
def hasPIL

Static Public Attributes

tuple actions
tuple security = ClassSecurityInfo()

Detailed Description

Base class for images containing transformation and exif functions

* exif information
* image rotation

Definition at line 77 of file imagetransform.py.


Member Function Documentation

Auto transform image according to EXIF data

Note: isn't using mirror

Definition at line 254 of file imagetransform.py.

00254 
00255     def autoTransformImage(self, REQUEST=None):
00256         """Auto transform image according to EXIF data
00257         
00258         Note: isn't using mirror
00259         """
00260         target = self.absolute_url() + '/atct_image_transform'
00261         mirror, rotation = self.getEXIFOrientation()
00262         transform = None
00263         if rotation:
00264             transform = AUTO_ROTATE_MAP.get(rotation, None)
00265             if transform is not None:
00266                 self.transformImage(transform)
00267         if REQUEST:
00268              REQUEST.RESPONSE.redirect(target)
00269         else:
00270             return mirror, rotation, transform
             

Here is the call graph for this function:

def ATContentTypes.lib.imagetransform.ATCTImageTransform.getEXIF (   self,
  img = None,
  refresh = False 
)
Get the exif informations of the file

The information is cached in _v_image_exif

Definition at line 124 of file imagetransform.py.

00124 
00125     def getEXIF(self, img=None, refresh=False):
00126         """Get the exif informations of the file
00127         
00128         The information is cached in _v_image_exif
00129         """
00130         cache = '_image_exif'
00131         
00132         if refresh:
00133             setattr(self, cache, None)
00134         
00135         exif_data = getattr(self, cache, None)
00136         
00137         if exif_data is None or not isinstance(exif_data, dict):
00138             io = self.getImageAsFile(img, scale=None)
00139             if io is not None:
00140                 # some cameras are naughty :(
00141                 try:
00142                     io.seek(0)
00143                     exif_data = exif.process_file(io, debug=False)
00144                 except:
00145                     LOG.error('Failed to process EXIF information', exc_info=True)
00146                     exif_data = {}
00147                 # seek to 0 and do NOT close because we might work
00148                 # on a file upload which is required later
00149                 io.seek(0)
00150                 # remove some unwanted elements lik thumb nails
00151                 for key in ('JPEGThumbnail', 'TIFFThumbnail', 
00152                             'MakerNote JPEGThumbnail'):
00153                     if key in exif_data:
00154                         del exif_data[key]
00155 
00156         if not exif_data:
00157             # alawys return a dict
00158             exif_data = {}
00159         # set the EXIF cache even if the image has returned an empty
00160         # dict. This prevents regenerating the exif every time if an
00161         # image doesn't have exif information.
00162         setattr(self, cache, exif_data)
00163         return exif_data

Here is the call graph for this function:

Here is the caller graph for this function:

Get the rotation and mirror orientation from the EXIF data

Some cameras are storing the informations about rotation and mirror in
the exif data. It can be used for autorotation.

Definition at line 165 of file imagetransform.py.

00165 
00166     def getEXIFOrientation(self):
00167         """Get the rotation and mirror orientation from the EXIF data
00168         
00169         Some cameras are storing the informations about rotation and mirror in
00170         the exif data. It can be used for autorotation.
00171         """
00172         exif = self.getEXIF()
00173         mirror = 0
00174         rotation = 0
00175         code = exif.get('Image Orientation', None)
00176         
00177         if code is None:
00178             return (mirror, rotation)
00179         
00180         try:
00181             code = int(code)
00182         except ValueError:
00183             return (mirror, rotation)
00184             
00185         if code in (2, 4, 5, 7):
00186             mirror = 1
00187         if code in (1, 2):
00188             rotation = 0
00189         elif code in (3, 4):
00190             rotation = 180
00191         elif code in (5, 6):
00192             rotation = 90
00193         elif code in (7, 8):
00194             rotation = 270
00195        
00196         return (mirror, rotation)
    

Here is the call graph for this function:

Here is the caller graph for this function:

Get the EXIF DateTimeOriginal from the image (or None)

Definition at line 198 of file imagetransform.py.

00198 
00199     def getEXIFOrigDate(self):
00200         """Get the EXIF DateTimeOriginal from the image (or None)
00201         """
00202         exif_data = self.getEXIF()
00203         raw_date = exif_data.get('EXIF DateTimeOriginal', None)
00204         if raw_date is not None:
00205             # some cameras are naughty ...
00206             try:
00207                 return DateTime(str(raw_date))
00208             except:
00209                 LOG.error('Failed to parse exif date %s' % raw_date, exc_info=True)
00210         return None

Here is the call graph for this function:

def ATContentTypes.lib.imagetransform.ATCTImageTransform.getImageAsFile (   self,
  img = None,
  scale = None 
)
Get the img as file like object

Definition at line 97 of file imagetransform.py.

00097 
00098     def getImageAsFile(self, img=None, scale=None):
00099         """Get the img as file like object
00100         """
00101         if img is None:
00102             f = self.getField('image')
00103             img = f.getScale(self, scale)
00104         # img.data contains the image as string or Pdata chain
00105         data = None
00106         if isinstance(img, OFSImage):
00107             data = str(img.data)
00108         elif isinstance(img, Pdata):
00109             data = str(img)
00110         elif isinstance(img, str):
00111             data = img
00112         elif isinstance(img, file) or (hasattr(img, 'read') and
00113           hasattr(img, 'seek')):
00114             img.seek(0)
00115             return img
00116         if data:
00117             return StringIO(data)
00118         else:
00119             return None

Here is the call graph for this function:

Here is the caller graph for this function:

Get map for tranforming the image

Definition at line 272 of file imagetransform.py.

00272 
00273     def getTransformMap(self):
00274         """Get map for tranforming the image
00275         """
00276         return [{'name' : n, 'value' : v} for v, n in TRANSPOSE_MAP.items()]
    
Is PIL installed?

Definition at line 278 of file imagetransform.py.

00278 
00279     def hasPIL(self):
00280         """Is PIL installed?
00281         """
00282         return HAS_PIL
00283 
00284 InitializeClass(ATCTImageTransform)
def ATContentTypes.lib.imagetransform.ATCTImageTransform.transformImage (   self,
  method,
  REQUEST = None 
)
Transform an Image:
    FLIP_LEFT_RIGHT
    FLIP_TOP_BOTTOM
    ROTATE_90 (rotate counterclockwise)
    ROTATE_180
    ROTATE_270 (rotate clockwise)

Definition at line 212 of file imagetransform.py.

00212 
00213     def transformImage(self, method, REQUEST=None):
00214         """
00215         Transform an Image:
00216             FLIP_LEFT_RIGHT
00217             FLIP_TOP_BOTTOM
00218             ROTATE_90 (rotate counterclockwise)
00219             ROTATE_180
00220             ROTATE_270 (rotate clockwise)
00221         """ 
00222         method = int(method)
00223         if method not in TRANSPOSE_MAP:
00224             raise RuntimeError, "Unknown method %s" % method
00225         
00226         target = self.absolute_url() + '/atct_image_transform'
00227         
00228         if not HAS_PIL:
00229             if REQUEST:
00230                 REQUEST.RESPONSE.redirect(target)
00231         
00232         image = self.getImageAsFile()
00233         image2 = StringIO()
00234         
00235         if image is not None:
00236             img = PIL.Image.open(image)
00237             del image
00238             fmt = img.format
00239             img = img.transpose(method)
00240             img.save(image2, fmt, quality=zconf.pil_config.quality)
00241             
00242             field = self.getField('image')
00243             mimetype = field.getContentType(self)
00244             filename = field.getFilename(self)
00245             
00246             # because AT tries to get mimetype and filename from a file like
00247             # object by attribute access I'm passing a string along
00248             self.setImage(image2.getvalue(), mimetype=mimetype,
00249                           filename=filename, refresh_exif=False)
00250         
00251         if REQUEST:
00252              REQUEST.RESPONSE.redirect(target)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Initial value:
(
        {
        'id'          : 'transform',
        'name'        : 'Transform',
        'action'      : 'string:${object_url}/atct_image_transform',
        'permissions' : (ModifyPortalContent,),
        'condition'   : 'object/hasPIL',
         },
        )

Definition at line 84 of file imagetransform.py.

Definition at line 94 of file imagetransform.py.


The documentation for this class was generated from the following file: