Image Module Maker API

The imagedata module handles reading images, perhaps converting images, and writing image data to a text file. Typically a text file that is a Python module.

class imm.imagedata.Generator(output='', writemode='WRITE', encoding='utf-8')[source]

This class implements a context manager for opening, or using a previouisly opened, Python module text file and writing image data to it.

  • output – Can be a string naming the output text file to be created or it can be an already opened output file object. If not specificed, a string is assumed and the default value is “”.
  • writemode – A string defining the write mode. Legal values are ‘WRITE’ and ‘APPEND’, the default is ‘WRITE’.
  • encoding – A string defining the write encoding of the output. Defaults to ‘utf-8’,

Note that if the output parameter represents an already opened output file object, then this context manager does not own the output file object resource and will therefore not close it upon exiting the context manager’s with statement code block.


Close the output write stream.

If imm.imagedata.Generator() is used has a context manager, this close() method will be called automatically if necessary upon exit of the context’s with block.

write(imagefile, imagevarname=None)[source]

This method writes the image data read from imagefile to the output Python module text file.

  • imagefile – A string specifying the image file name to read.
  • imagevarname – A string specifying the image data’s variable name.

If imagefile is NOT a .png file, then it will be converted in memory to a PNG image before being written to the output Python module text file.

If imagevarname is NOT specified, then a legal Python variable name will be derived from the imagefile name. If no legal Python identifier can be derived from the image file name, the a random identifer will be generated.

imm.imagedata.id_generator(size=7, chars='abcdefghijklmnopqrstuvwxyz0123456789')[source]

Returns a random python identifier that is size characters in length and composed of characters from the set of chars.


Modify the input string, s, until we can return a valid Python identifier.

The process is as follows:

  1. Replace all spaces and dashes with underscores
  2. Remove any invalid Python identifier characters (any char that is not 0-9, a-z, A-Z, or underscore)
  3. If an empty identifier remains, generate a random identifier
  4. If the identifier begins with a digit, prefix an underscore
  5. If the identifier begins with an underscore, prefix the string ‘image’


The IMM library utilizes the Python Standard Library’s logging module.

A child logger descended from the root logger is intialized and a null logging handler is added so that if the code utilizing the IMM library does not configure logging, any logging message will be handled by the null logging handler.

For more details see Configuring Logging for a Library from the Python Standard Library documentation.