1

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?

Community
  • 1
  • 1
Adrian Garner
  • 5,235
  • 2
  • 23
  • 29

2 Answers2

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

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.

Ross Patterson
  • 5,702
  • 20
  • 38