Image

By Martin McBride, 2021-01-01
Tags: geometry image
Categories: generativepy generative art


The Image class renders an image on a drawing context.

This is intended for very simple display of images:

  • The image must be available as a PNG file.
  • The full image will be rendered as a rectangle, the same size as the original image.
  • The image can optionally be scaled by a factor.

The rendered image size in user space units will match the pixel size of the original PNG image.

The image can be transformed using standard PyCairo context calls, so you can rotate, mirror, stretch or shear the image. You can also apply a clip path prior to rendering the image, to change its shape. Transparent PNG images are supported.

Image class methods

The Image is not derived from Shape, so it doesn't inherit any of its methods.

It has the following methods:

  • of_file_position
  • scale
  • paint

Constructor

The Image constructor accepts a context object (similar to Shape objects):

Image(ctx)

of_file_position

Specifies an image file and a position.

of_file_position(filename, position)
Parameter Type Description
filename string The file path of a PNG file contianing the image.
position (number, number) A tuple of two numbers, giving the (x, y) position of the top left corner of the image.

By default, the image will be rendered with its top left corner at position. If user space is mirrored or rotated, it may appear differently on the page.

scale

Sets the image scale factor.

scale(factor)
Parameter Type Description
scale number The scale factor for the image

Scales the image by a factor. For example, a factor of 2 will make the image appear twice as big on the page.

If this function is not called, a scale factor of 1 is used by default.

paint

Renders the image.

paint()

Example

Here is some example code that draws an image on the page. The same image is drawn twice, once at normal size and once scaled to half size. The full code can be found on github. The code assumes there is an image cat.png in the current folder:

from generativepy.drawing import make_image, setup
from generativepy.color import Color
from generativepy.geometry import Image

'''
Draw an image
'''

def draw(ctx, width, height, frame_no, frame_count):
    setup(ctx, width, height, width=500, background=Color(0.8))

    Image(ctx).of_file_position('cat.png', (50, 50)).paint()
    Image(ctx).of_file_position('cat.png', (300, 50)).scale(0.5).paint()

make_image("/tmp/geometry-image.png", draw, 400, 200)

Here is the result:

See also

If you found this article useful, you might be interested in the book NumPy Recipes or other books by the same author.

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest