def getPILFromObject(self, obj):
        """

        """

        if isinstance(obj, ImageFile):
            # Normal file system file
            return obj
        elif isinstance(obj, FSImage):
            # FSImage
            # Read data from object
            return PIL.Image.open(obj.getObjectFSPath())
        elif isinstance(obj, OFS.Image.Image):
            # OFS Image
            # Held in the portal_skins folder, uploaded via ZMI
            data = obj.data
            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        elif isinstance(obj, ATFieldImage):
            # Read data from object
            data = obj.data
            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        elif IATImage.providedBy(obj):
            obj = obj.getImage()
            data = obj.data
            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        else:
            raise RuntimeError("Can't handle:" + str(obj))
Beispiel #2
0
 def fields(self):
     if IATImage.providedBy(self.context):
         fields = z3cfield.Fields(self.interface)
         fields['bannerImageLink'].widgetFactory = ContentTreeFieldWidget
     else:
         fields = z3cfield.Fields(self.interface).select(
             'bannerLocalOnly', 'bannerStopAcquisition')
     return fields
Beispiel #3
0
 def splash(self):
     splash_path = self.data.splash
     if not splash_path:
         return None
     if splash_path.startswith('/'):
         splash_path = splash_path[1:]        
     if not splash_path:
         return None
     portal = self.portal()
     splash = portal.unrestrictedTraverse(splash_path, default=None)        
     if splash is not None and not IATImage.providedBy(splash):
         return None        
     return splash
    def getPILFromObject(self, obj):
        """ Convert various Zope based image objects to Python Imaging Library image data.

        @param obj: Any Zope imagish object
        """

        if isinstance(obj, ImageFile):
            # Normal file system file
            return obj
        elif isinstance(obj, FSImage):
            # FSImage
            # Read data from object
            return PIL.Image.open(obj.getObjectFSPath())
        elif isinstance(obj, ATFieldImage):
            # Read data from object
            pimage = obj.data

            # TODO
            # Not sure what's going on here...
            #
            if hasattr(pimage, "data"):
                data = pimage.data
            else:
                data = pimage

            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        elif IATImage.providedBy(obj):
            obj = obj.getImage()
            data = obj.data
            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        elif isinstance(obj, OFS.Image.Image):
            # OFS Image
            # Held in the portal_skins folder, uploaded via ZMI
            data = obj.data
            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        elif HAS_BLOBS and isinstance(obj, BlobWrapper):
            # Plone 4 BLOB images
            data = obj.data
            io = cStringIO.StringIO(data)
            return PIL.Image.open(io)
        else:
            raise RuntimeError("Can't handle:" + str(obj))
    def getImageObject(self, path):
        """ Get image handle to traversable Zope object, assuming object is some sort of image

        Path must not start with slash.
        Path is relative to the site root.
        
        
        """
        site = getSite()

        img = site.restrictedTraverse(path)

        # UGH... the resource based image isn't a real class but some
        # dynamically generated Five metaclass... no f*****g clue
        # how to do interface checks against those,
        # so we just
        if ("DirContainedImageResource" in img.__class__.__name__) or ("FileResource" in img.__class__.__name__) :
            # Resource based image, on file system, handle with PIL
            # info = (width, height)
            source = img.context.path
            info = PIL.Image.open(source)
            return info
        elif isinstance(img, FSImage):
            # FSImage at /plone/logo
            # width, height = util.getImageInfo(img)
            # <implementedBy Products.CMFCore.FSImage.FSImage>
            return img
        elif isinstance(img, OFS.Image.Image):
            # image uploaded to a portal_skins/custom
            return img
        elif IATImage.providedBy(img):
            # Image is uploaded image content type
            return img.getImage()
        else:

            if callable(img):
                img = img()

            if isinstance(img, ATFieldImage):
                return img

            raise RuntimeError("Unknown image object %s:%s" % (path, str(img.__class__)))

        return info
Beispiel #6
0
    def splash(self):
        splash_path = self.data.splash
        if not splash_path:
            return None

        if splash_path.startswith('/'):
            splash_path = splash_path[1:]

        if not splash_path:
            return None

        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        portal = portal_state.portal()
        splash = portal.restrictedTraverse(splash_path, default=None)

        if splash is not None and not IATImage.providedBy(splash):
            return None

        return splash
Beispiel #7
0
    def splash(self):
        splash_path = self.data.splash
        if not splash_path:
            return None

        if splash_path.startswith('/'):
            splash_path = splash_path[1:]

        if not splash_path:
            return None

        portal_state = getMultiAdapter((self.context, self.request),
                                       name=u'plone_portal_state')
        portal = portal_state.portal()
        splash = portal.restrictedTraverse(splash_path, default=None)

        if splash is not None and not IATImage.providedBy(splash):
            return None

        return splash
 def get_title_image_tag(self):
     """Generate image tag.
     
     Note: ``target_title_image`` uses the uberselection-widget
     and does not return an object (unlike Archetypes reference
     fields).
     """
     
     image_path = self.data.target_title_image
     
     
     # it feels insane that i need to do manual strippping of the first slash in this string.
     # I must be doing something wrong
     # please make this bit more sane
     
     if image_path is None or len(image_path)==0:
         return None
     # The portal root is never a image
     
     if image_path[0]=='/':
         image_path = image_path[1:]
     image = self.portal.restrictedTraverse(image_path, default=None)
     
     # we should also check that the returned object implements the interfaces for image
     # So that we don't accidentally return folders and stuff that will make things break
     scale = self.data.scale or u"mini"
     scale = scale.encode('ascii', 'replace')
     if IImageScaleTraversable and IImageScaleTraversable.providedBy(image):
         try:
             view = image.restrictedTraverse('@@images')
             view = view.__of__(image)
             return view.tag('image', scale=scale)
         except:
             return None
     elif IATImage.providedBy(image) and image.getImage() is not None:
         return image.tag(scale=scale)
     else:
         return None
    def get_title_image_tag(self):
        """Generate image tag.

        Note: ``target_title_image`` uses the uberselection-widget
        and does not return an object (unlike Archetypes reference
        fields).
        """

        image_path = self.data.target_title_image

        # it feels insane that i need to do manual strippping of the first slash in this string.
        # I must be doing something wrong
        # please make this bit more sane

        if image_path is None or len(image_path) == 0:
            return None
        # The portal root is never a image

        if image_path[0] == '/':
            image_path = image_path[1:]
        image = self.portal.restrictedTraverse(image_path, default=None)

        # we should also check that the returned object implements the interfaces for image
        # So that we don't accidentally return folders and stuff that will make things break
        scale = self.data.scale or u"mini"
        scale = scale.encode('ascii', 'replace')
        if IImageScaleTraversable and IImageScaleTraversable.providedBy(image):
            try:
                view = image.restrictedTraverse('@@images')
                view = view.__of__(image)
                return view.tag('image', scale=scale)
            except:
                return None
        elif IATImage.providedBy(image) and image.getImage() is not None:
            return image.tag(scale=scale)
        else:
            return None
Beispiel #10
0
 def is_image(self, obj):
     return IATImage.providedBy(obj)
Beispiel #11
0
 def is_image(self):
     return IATImage.providedBy(self.context)
    def getImageObject(self, path):
        """ Get image handle to traversable Zope object, assuming object is some sort of image

        Path must not start with slash.
        Path is relative to the site root.

        @param path: Path to Zope object

        @return: Image-like object (heterogenous return values)
        """
        site = getSite()

        try:
            img = site.unrestrictedTraverse(path)
        except Unauthorized:
            # The parent folder might be private and the image public,
            # in which case we should be able to view the image after all.
            parent_path = '/'.join(path.split('/')[:-1])
            image_path = path.split('/')[-1]
            parent = site.unrestrictedTraverse(parent_path)
            img = parent.restrictedTraverse(image_path)


        # UGH... the resource based image isn't a real class but some
        # dynamically generated Five metaclass... no f*****g clue
        # how to do interface checks against those,
        # so we just
        if ("DirContainedImageResource" in img.__class__.__name__) or ("FileResource" in img.__class__.__name__) :
            # Resource based image, on file system, handle with PIL
            # info = (width, height)
            source = img.context.path
            info = PIL.Image.open(source)
            return info
        elif isinstance(img, FSImage):
            # FSImage at /plone/logo
            # width, height = util.getImageInfo(img)
            # <implementedBy Products.CMFCore.FSImage.FSImage>
            return img
        elif isinstance(img, OFS.Image.Image):
            # image uploaded to a portal_skins/custom
            return img
        elif IATImage.providedBy(img):
            # Image is uploaded image content type
            return img.getImage()
        else:

            # pointer to exposed ATImage method
            if callable(img):
                img = img()

            if HAS_BLOBS:
                # img might be <bound method ATBlob.getImage of <ATImage at /plone/test_img>
                # We resolve it to blob above
                if isinstance(img, BlobWrapper):
                    return img

            if isinstance(img, ATFieldImage):
                return img


            raise RuntimeError("Unknown image object %s:%s" % (path, str(img.__class__)))

        return info