generativepy.bitmap module


Martin McBride, 2020-09-06
Tags bitmap
Categories generativepy generative art

The bitmap module provides the ability to paint bitmap image.

The module is based on the popular PIL Python imaging library. This provides many functions for manipulating bitmap images, such as colour manipulation, filters, enhancement, morphing, colour channel operations, and pixel access. It also provides drawing operations (rectangles, ellipses, text etc), but these are not as powerful as the vector methods available via Pycairo in the generativepy.drawing module.

generativepy actually uses the Pillow library, which is a fork of PIL. That is because Pillow is actively maintained whereas PIL is not. The Pillow library is compatible, and is still imported using the name PIL. We will refer to is as PIL in the documentation.

make_bitmap

Used to create a single PNG image.

generativepy.bitmap.make_bitmap(outfile, paint, pixel_width, pixel_height, channels=3)
Parameter Type Description
outfile String The path and filename for the output PNG file. It should end in '.png'.
paint Function A drawing function object, see below.
pixel_width int The width of the image that will be created, in pixels.
pixel_height int The height of the image that will be created, in pixels.
channels int The number of colour channels. 1 fro greyscale, 3 for RGB, 4 for RGBA

make_bitmap creates a PIL Image object, then calls the user supplied paint function to fill the image.

The paint function must have the following signature:

paint(image, pixel_width, pixel_height, frame_no, frame_count)
Parameter Type Description
image Image A PIL Image obejct that the paint function should work on.
pixel_width int The width of the image in pixels. This is the same value as the width parameter in make_bitmap.
pixel_height int The height of the image in pixels. This is the same value as the height parameter in make_bitmap.
frame_no int The number of the current frame.
frame_count int The total number of frames.

frame_no and frame_count only apply to functions that create a sequence of images. The make_bitmap function only ever creates one image, so the frame_no will always be 0 and the frame_count will always be 1.

Scaler class

A PIL Image object always works in pixels. Unlike a Pycairo context used by make_image and similar functions, Image has no concept of a user space.

However, it is sometimes useful to be able to calculate values in a user space and convert them to image pixels (device space), or vice versa.

The Scaler class provides this functionality. You can create a scaler object with the dimensions of the image and the required user space. The scaler object can then be used to convert coordinates from one space to another. See the user space tutorial for more details.

Scaler constructor

Creates an Scaler object.

Scaler(self, pixel_width, pixel_height, width=None, height=None, startx=0, starty=0)
Parameter Type Description
pixel_width int The width of the image in pixels. Use the value passed into paint.
pixel_height int The height of the image in pixels. Use the value passed into painty.
width float The required image width in user coordinates.
height float The required image height in user coordinates.
startx float The user space x coordinate of the device space origin.
starty float The user space y coordinate of the device space origin.

device_to_user

Converts a device coordinate to user space.

device_to_user(self, device_x, device_y)
Parameter Type Description
device_x int The x coordinate in device space.
device_y int The y coordinate in device space.

Returns float (x, y), a tuple of the equivalent coordinates in user space.

user_to_device

Converts a user coordinate to device space.

user_to_device(self, user_x, user_y)
Parameter Type Description
user_x float The x coordinate in user space.
user_y float The y coordinate in user space.

Returns int (x, y), a tuple of the equivalent coordinates in device space.

For an example, see the user space tutorial.

Copyright (c) Axlesoft Ltd 2020