Skip to main content

PygameCV module is a set of functions helping game developpers using pygame to apply some transformations on their surfaces using CV.

Project description

PygameCV

PygameCV is a set of function allowing the a game developper using pygame to enhance its game by using cv's drawing functions and other effects to modify a Surface.

Tutorial

Importation

First thing first, ensure to have PygameCV installed on your python environement. For that, run the following command:

pip install pygame-cv

Then, import PygameCV with

import pygamecv

Drawing

You are now able to use pygamecv on your surfaces! For example, you might want to draw an ellipse, like that:

    import pygame
    import pygamecv

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("Lenna.png")
    pygamecv.ellipse(
        surface=img,
        center=(100, 200),
        radius_x=50,
        radius_y=40,
        color=(0, 255, 255),
        thickness=10, # This is pygame.draw's 'width' argument
        antialias=True,
        angle=0,
    )
    ... # The remaining of your game.

Which will output the following:

Ellipse

You could also be tempted to draw a rectangle, but with some transparency, like this:

    import pygame
    import pygamecv

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")
    pygamecv.rectangle(
        surface=img,
        rect=(200, 200, 150, 300),
        color=(0, 255, 255, 100),
        thickness=0
    )
    ... # The remaining of your game.

which will do the following modification: Rectangle

Finally, you can need to draw a rectangle with rounded, antialiased corners, like the following:

    import pygame
    import pygamecv

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")
    pygamecv.rounded_rectangle(
        surface=img,
        rect=(200, 200, 150, 300),
        color=(0, 255, 255, 255),
        thickness=20,
        antialias=True,
        top_left=15, # angle of the top left corner and all other unspecified corners
        bottom_right=135,

    )
    ... # The remaining of your game.

which will look like that

Rounded Rectangle

Effect

That's cool, but what you really need it to apply some effect on the image. For example, you want to saturate it.

    import pygame
    import pygamecv

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")
    pygamecv.saturate(
        surface=img,
        factor=1
    )
    ... # The remaining of your game.

Which will give you

Saturation

Great! But now you would like to shift the hue of the image, and to darken the image.

    import pygame
    import pygamecv

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")
    pygamecv.shift_hue(
        surface=img,
        value=90
    )
    pygamecv.darken(
        surface=img,
        factor=0.5
    )
    ... # The remaining of your game.

whose result is Darken and shift hue

Alright alright, now you want to gradually darken the edges of the image. For that, we use a numpy array as factor and not a float.

    import pygame
    import pygamecv
    import numpy as np

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")

    def gradient_factor(width, height, min_radius) -> np.ndarray:
        x_grid, y_grid = np.ogrid[:width, :height]
        dist_to_center = np.sqrt((x_grid - width/2 + 0.5)**2 + (y_grid - height/2 + 0.5)**2)
        unchanged = dist_to_center < min_radius
        max_radius = np.sqrt((width/2 - 0.5)**2 + (height/2-0.5)**2)
        factor = (dist_to_center - min_radius)/(max_radius - min_radius)
        factor[unchanged] = 0
        return factor

    factor=gradient_factor(*img.get_size(), min_radius=50)

    pygamecv.darken(
        surface=img,
        factor=factor
    )
    ... # The remaining of your game.

Which will give you this beautfiul vignette effect: Vignette

Finally, you decide that you need to set a value for the saturation of all pixels outside of a circle. Right, let's do it, using masks.

    import pygame
    import pygamecv
    import numpy as np

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")

    def circle_mask(width, height, radius) -> np.ndarray:
        x_grid, y_grid = np.ogrid[:width, :height]
        dist_to_center = np.sqrt((x_grid - width/2 + 0.5)**2 + (y_grid - height/2 + 0.5)**2)
        return dist_to_center < radius
 
    mask = circle_mask(*img.get_size(), 100)

    pygamecv.set_saturation(
        surface=img,
        value=0,
        mask=mask
    )
    ... # The remaining of your game.

Which removes the color saturation of the center, like that: Saturation with mask

Other needs

You are telling me that now, you what to other cv functions. For that, you will need to define your own function, using the decorator pygamecv.common.cv_transformation. This decorator transform a cv function into a pygame surface transformation.

Here, _cv_your_function transforms in-place img. The decorator turn it from an in-place array to array into an in-place surface-to-surface transformation, with a rect argument. If the rect is specified, the array to be transformed is taken from a subsurface, extracted by the rect. Otherwise, it is from the whole surface. Then, you can define another function to call it. It is adviced to do so if you need to compute a rectangle from the input of the function, that's how it is done for circles or ellipses, for example.

    import pygame
    import pygamecv
    import numpy as np

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    img = pygame.image.load("images/Lenna.png")

    @pygamecv.cv_transformation
    def _cv_your_function(img: numpy.ndarray, **kwargs):
        ...
  
    def your_function(surface: pygame.Surface, **kwargs):
        rect = ...
        _cv_your_function(surface, rect, **kwargs)
  
    your_function(img, (11, 11), 20)

Warning

Modify a Surface using cv may be long, as copying the value of the pixels needs to be done twice. For this purpose, restricting the array to the smallest Rect speeds up the computations. If you need to modify a big surface, it will take time.

Contributing

Everyone is welcome to contribute to this project by proposing new features, optimization and help with the documentation! Your feedbacks will be appreciated.

License

This project is licensed under a GNU GENERAL PUBLIC LICENSE, please refer to the license file

Documentation

PygameCV's full documentation can be found here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygame_cv-1.3.2.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygame_cv-1.3.2-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file pygame_cv-1.3.2.tar.gz.

File metadata

  • Download URL: pygame_cv-1.3.2.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for pygame_cv-1.3.2.tar.gz
Algorithm Hash digest
SHA256 e1214afaed2c608402f968668ad0d11092153b5d2b60ac5bc4dce7fb6685f7a4
MD5 5480c985a6cdbbf535938766b593eba2
BLAKE2b-256 702655194bc51961d772b4ce5b98d442995f5f4dd94cc3c100013e55f22b4bfc

See more details on using hashes here.

File details

Details for the file pygame_cv-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: pygame_cv-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for pygame_cv-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1778916f7366cc938f9ab9d03f06b92437fc3e314b807a896bb96d334fd73a9
MD5 34a083f6e39df228f41604f3cdbe5a09
BLAKE2b-256 3d7acdf71fb357920d176980fe25910f0097f4c5a82beb77e142088c337263b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page