Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1023467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:37:32+00:00 2026-05-16T11:37:32+00:00

I have two images: I’d like to essentially ‘cut out’ the black shape from

  • 0

I have two images:

Mask
Texture

I’d like to essentially ‘cut out’ the black shape from the texture tile so that I end up with something along these lines:

Cutout

Except transparent around the shape. Is this possible using pygame? This example I had to create in GIMP.

Additionally, would it be too performance-heavy to do this for every frame for a few sprites in a real-time environment? (30+ frames per second)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:37:33+00:00Added an answer on May 16, 2026 at 11:37 am

    I made a solution, however it is not the best either for speed either for beauty.
    You can use double blitting with setting colorkeys for transparency. In that way the mask should have only two colors: black and white.
    Note that you can’t use this for images with per pixel alpha (RGBA) only for RGB images.
    Other restriction is that it is recommended that the size of the texture and the mask image is the same (if not you should use areas for blitting).

    In words, step by step:

    • create a surface with the mask. background should be white (255,255,255), the masked parts should be black (0,0,0)
    • create or load the texture into a surface
    • set a transparency colorkey for the mask for the black color
    • blit the mask onto the texture (or onto a copy of the texture). at this point the black parts of the mask haven’t blitted to the texture because we set the black color to transparent with the set_colorkey method
    • now set the colorkey of the texture (or the copy of the texture) to white. remember that our current texture surface has white and textured parts.
    • blit the texture to the screen. the white parts won’t be blitted due to we have set it to transparent with the colorkey

    Code sample:

    #!/usr/bin/python
    # -*- coding:utf8 -*-
    
    import pygame, sys
    
    #init pygame
    pygame.init()
    
    #init screen
    screen=pygame.display.set_mode((800,600))
    screen.fill((255,0,255))
    
    #loading the images
    texture=pygame.image.load("texture.jpg").convert()
    texture_rect=texture.get_rect()
    texture_rect.center=(200,300)
    mask=pygame.Surface((texture_rect.width,texture_rect.height)) # mask should have only 2 colors: black and white
    mask.fill((255,255,255))
    pygame.draw.circle(mask,(0,0,0),(texture_rect.width/2,texture_rect.height/2),int(texture_rect.width*0.3))
    mask_rect=mask.get_rect()
    mask_rect.center=(600,300)
    
    tmp_image=texture.copy() # make a copy of the texture to keep it unchanged for future usage
    mask.set_colorkey((0,0,0)) # we want the black colored parts of the mask to be transparent
    tmp_image.blit(mask,(0,0)) # blit the mask to the texture. the black parts are transparent so we see the pixels of the texture there
    
    tmp_rect=tmp_image.get_rect()
    tmp_rect.center=(400,300)
    tmp_image.set_colorkey((255,255,255))
    screen.blit(texture,texture_rect)
    screen.blit(mask,mask_rect)
    screen.blit(tmp_image,tmp_rect)
    
    pygame.display.flip()
    
    while 1:
        event=pygame.event.wait()
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in [pygame.K_ESCAPE, pygame.K_q]):
            sys.exit()
    

    I recommend not to use jpg as mask because of its lossy format, I recommend bmp or png (bmp is better). Remember it not uses alpha so the edges won’t be anti-aliased so it is not a nice solution in 2010 🙂

    Here is the screenshot of the result:
    masked blitting

    Edit:
    Hi again,

    I made some tests with the blitting with BLEND_ADD and the results was promising. Here is the code:

    import pygame, sys
    
    #init pygame
    pygame.init()
    
    #init screen
    screen=pygame.display.set_mode((800,600))
    screen.fill((255,0,255))
    
    #loading the images
    texture=pygame.image.load("texture.jpg").convert_alpha()
    texture_rect=texture.get_rect()
    texture_rect.center=(200,300)
    mask=pygame.image.load("mask2.png").convert_alpha()
    mask_rect=mask.get_rect()
    mask_rect.center=(600,300)
    
    textured_mask=mask.copy()
    textured_rect=textured_mask.get_rect()
    textured_rect.center=400,300
    
    textured_mask.blit(texture,(0,0),None,pygame.BLEND_ADD)
    
    screen.blit(texture,texture_rect)
    screen.blit(mask,mask_rect)
    screen.blit(textured_mask,textured_rect)
    
    pygame.display.flip()
    
    while 1:
        event=pygame.event.wait()
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in [pygame.K_ESCAPE, pygame.K_q]):
            sys.exit()
    

    And the result:
    result

    With this solution you can get per pixel alpha texturing. Please note that the mask should be an image with alpha channel so jpg could not be used. The best to use is png.

    Texture image (texture.jpg):
    texture

    Mask image (mask2.png):
    mask

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two images like so. (granted that the code is indented) @Html.Image(/Images/icons/arrow-up.gif, up)
I have two images out of my whole website that look out of place
I have two images, both taken at the same time from the same detector.
I have two images on a WPF window that are on top of each
I have two images which i would like to compare and do different operations
I have two images. One that is normal, and another that is more colourised.
We have two images with transparent sections that each have a map and separately
I have two images that I want to split each into 3 parts. If
I have two png image files that I would like my android app to
I have two images that are very similar. So for example suppose I drew

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.