Skip to content

How about Numpy to get a translucent image clipped faster? #159

Answered by hx2A
villares asked this question in Q&A
Discussion options

You must be logged in to vote

I like this question. There is a good solution using py5's numpy tools.

Using a Py5Graphics object to create the mask is a good idea:

    clip_mask = create_graphics(width, height)
    clip_mask.begin_draw()
    clip_mask.fill(255)
    clip_mask.circle(mouse_x, mouse_y, 250)
    clip_mask.end_draw()

Then you can do something like this:

    clip_mask.load_np_pixels()
    offscreen.load_np_pixels()
    # copy clip_mask's red channel to offscreen's alpha channel
    offscreen.np_pixels[:, :, 0] = clip_mask.np_pixels[:, :, 1]
    offscreen.update_np_pixels()
    image(offscreen, 0, 0)

You should also put all the create_graphics() calls in setup(). It is a slow operation to create a new one fo…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@hx2A
Comment options

hx2A Sep 6, 2022
Maintainer

@hx2A
Comment options

hx2A Sep 6, 2022
Maintainer

@villares
Comment options

villares Sep 6, 2022
Collaborator Author

Answer selected by villares
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants