In this article we will create a very simple image using generativepy. The image looks like this:
Here is the code to draw the image above:
from generativepy.drawing import make_image, setup from generativepy.color import Color from generativepy.geometry import Rectangle def draw(ctx, width, height, frame_no, frame_count): setup(ctx, width, height, background=Color(0.4)) color = Color(1, 0.5, 0) Rectangle(ctx).of_corner_size((100, 150), 250, 200).fill(color) make_image("rectangle.png", draw, 500, 400)
We will now look at this in more detail.
The make_image
function, from the drawing module, is the main function that handles creating the image.
The basic parameters are:
draw
function that does the actual drawing.The draw
function is a function that we define ourselves to perform the drawing. It doesn't have to be called draw
, of course, but it must have the correct number of parameters.
draw
accepts 5 parameters:
ctx
is a Pycairo context. This is a like a virtual drawing surface that you can draw on in code. Whatever you draw will appear on the final image.width
and height
are the width and height of the image in pixels. These are the values that we passed into make_image
.frame_no
and frame_count
are only used when you want to create image sequences (for videos), so we can ignore them here.generativepy uses Pycairo as its drawing library. Pycairo has an extensive set of drawing functions that you can use to draw on the context, but generativepy also provides some extra functions that are useful for generative art. You can mix and match these functions - that is especially useful if you want to use some advanced feature of Pycairo that generativepy doesn't directly support.
Our draw
function calls the setup
function to set up the drawing area, then draws a rectangle.
The setup
function isn't mandatory, but it does some useful things so you will often want to call it.
setup
accepts 3 required parameters - the ctx
, width
and height
that were passed in to the draw
function. It has some optional parameters tat do various things.
In this case we are setting the background
parameter to Color(0.4)
which sets the background colour to a mid grey (it sets r, g and b to 40%).
We also create another colour, Color(1, 0.5, 0)
for use later on. This is 100% red, 50% green and 0% blue, which is an orange colour.
We use a Rectangle
object to draw a rectangle, like this:
Rectangle(ctx).of_corner_size((100, 150), 250, 200).fill(color)
All shapes are drawn using the same pattern:
Rectangle(ctx)
. The context is passed in at this stage.of_corner_size
defines a rectangle from the position of its corner, and its width and height. The corner is specified using an (x, y)
tuple.fill
that fills the shape with the supplied orange colour.By default, the size and position of shapes we draw using generativepy are measured in pixels, based on the size of the image we are creating.
In our case:
This diagram illustrates the coordinates of the rectangle:
The example place is a good place to start if you want to experiment with other shape objects, which all work in a similar way.
Copyright (c) Axlesoft Ltd 2020