[Zope3-dev] image libraries implementation in zope3 ?

gawel gawel75 at gmail.com
Fri May 26 09:22:10 EDT 2006


Skipped content of type multipart/alternative-------------- next part --------------
=========================
Image library interface.
=========================

This package provides a set of utilities and adapters
to implements some image libraries (python-imaging (PIL)
and ImageMagick) functionalities.


Image is adaptable with IReadFile and IWriteFile::

  >>> from zope.configuration import xmlconfig
  >>> context = xmlconfig.string("""
  ... <configure
  ...     xmlns='http://namespaces.zope.org/zope'
  ...     >
  ...   <include package="zope.app.component" file="meta.zcml" />
  ... <adapter 
  ...    for="zope.app.file.interfaces.IFile"
  ...    provides="zope.app.filerepresentation.interfaces.IReadFile"
  ...    factory="zope.app.file.file.FileReadFile"
  ...    />
  ... <adapter 
  ...    for="zope.app.file.interfaces.IFile"
  ...    provides="zope.app.filerepresentation.interfaces.IWriteFile"
  ...    factory="zope.app.file.file.FileWriteFile"
  ...    />
  ...   <include package="imagelib" file="configure.zcml" />
  ... </configure>
  ... """)

We can create an Image object and use the transformer to resize it.
We don't have to take care of which image library is used::

  >>> import imagelib
  >>> from imagelib.tests import getTestImage
  >>> from imagelib.interfaces import IImageTransformer
  >>> image = getTestImage()
  >>> image.getImageSize()
  (145, 42)
  >>> transformer = IImageTransformer(image) 
  >>> transformer.resize((100,100))
  >>> size = image.getImageSize()
  >>> 100 in size
  True

We can also rotate the image::

  >>> image = getTestImage()
  >>> image.getImageSize()
  (145, 42)
  >>> transformer = IImageTransformer(image) 
  >>> transformer.rotate(90)
  >>> image.getImageSize()
  (42, 145)

We can work on a copy of the original image::

  >>> transformer2 = transformer.copy()
  >>> image is transformer.getObject()
  True
  >>> image is transformer2.getObject()
  False

ImageTransformer also implement IReadFile::

  >>> from zope.app.filerepresentation.interfaces import IReadFile
  >>> IReadFile.providedBy(transformer)
  True
  >>> image.data == transformer.read()
  True

PIL is used as default image library but
we can force which librairie is used::

  >>> image = getTestImage()
  >>> image.getImageSize()
  (145, 42)
  >>> transformer = IImageTransformer(image) 
  >>> transformer.library
  u'pil'
  >>> transformer.library = 'im'
  >>> transformer.library
  u'im'
  >>> transformer.rotate(90)
  >>> image.getImageSize()
  (42, 145)

We can also change the default library via a zcml directive::

  >>> context = xmlconfig.string("""
  ... <configure
  ...     xmlns='http://namespaces.zope.org/zope'
  ...     >
  ...   <imagelibrary
  ...       name="im"
  ...       default="True"
  ...       />
  ... </configure>
  ... """,context)
  >>> transformer = IImageTransformer(image) 
  >>> transformer.library
  u'im'

We can't use a non-exist library::

  >>> transformer.library = 'mylib'
  Traceback (most recent call last):
  ...
  ComponentLookupError: 'No utility named mylib.'

But we can register a new library via zcml::

  >>> context = xmlconfig.string("""
  ... <configure
  ...     xmlns='http://namespaces.zope.org/zope'
  ...     >
  ...   <imagelibrary
  ...       name="mylib"
  ...       factory="imagelib.tests.MyImageLibrary"
  ...       />
  ... </configure>
  ... """,context)

Now we can use it::

  >>> image = getTestImage()
  >>> transformer = IImageTransformer(image) 
  >>> transformer.library = 'mylib'
  >>> transformer.library
  u'mylib'
  >>> transformer.resize((100,100))

But, hum, our custom library don't resize anything::

  >>> image.getImageSize()
  (145, 42)
  












More information about the Zope3-dev mailing list