Images in generativepy


Martin McBride, 2022-01-07
Categories generativepy generativepy tutorial

The generativepy drawing module allows you to add image elements, using the Image class.

You can import any PNG image, and position it on the output image. You can also scale it.

If the source image has transparency, that will be respected when the image is added.

Adding an image

This sample code adds the image cat.png. It adds the same image three times, with different scale factors:

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


def draw(ctx, pixel_width, pixel_height, frame_no, frame_count):
    setup(ctx, pixel_width, pixel_width, 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()
    Image(ctx).of_file_position('cat.png', (50, 300)).scale(1.5).paint()


make_image("image.png", draw, 500, 600)

This code is available on github in tutorial/shapes/image.py.

Here is the result:

This is the code to add the image at its normal size, with the top left corner of the image placed at (50, 50):

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

This code adds the image at half its normal size, with the top left corner of the image placed at (50, 50):

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

As the example code shows, you can also scale the image up by using a scale factor > 1.

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

Prev

Popular tags

2d arrays abstract data type alignment and angle animation arange arc array arrays 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 design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop 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 len line linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce repeat rgb rotation roundrect scaling scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip