Adrian Garner
Adrian Garner

Reputation: 5345

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

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?

Upvotes: 1

Views: 252

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1123420

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.

Upvotes: 2

Ross Patterson
Ross Patterson

Reputation: 5742

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.

Upvotes: 1

Related Questions