开发者

How to store an image after generating it (Python imaging, Plone 3)

开发者 https://www.devze.com 2023-03-31 22:59 出处:网络
I would开发者_开发技巧 like to generate an image using text and a custom font. As it is mentioned here Output text as GIF or PNG for use in eBook

I would开发者_开发技巧 like to generate an image using text and a custom font. As it is mentioned here Output text as GIF or PNG for use in eBook

How can I store the file in my content's file field?


All you need to do is call the field accessor with a file-like object, preferably one that includes the file mime-type (image/png or image/gif in your case). Zope's OFS.File.File provides such info. Let's say your file field is simply called file (so it's setter is called setFile) and you have your image in a string variable named imagedata containing a PNG image:

from OFS.Image import File
image = File('ignored-id', 'ignored-title', imagedata, 'image/png')
contentobject.setFile(image)

Note that you may want to change your field to type ImageField though; it provides a richer interface for images, including HTML tag generation and scaling.


Get the python file-like object corresponding to the image and pass it as an argument to the field mutator method. If the field if called fooImage, then the mutator, by default, is object.setFooImage(image_file_here). Or you could pass it in using the object.update(fooImage=image_file_here) method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号