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 9318737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:12:18+00:00 2026-06-19T03:12:18+00:00

This follows on a question I recently asked: In Pygame, how can I save

  • 0

This follows on a question I recently asked:

In Pygame, how can I save a screen image in headless mode?

I am able to save screen images involving non-transparent surface objects. However, the technique does not work for transparent surface objects. The following code illustrates the problem:

import sys
import os
import pygame
from pygame.color import THECOLORS as RGB


class PygameProblemDemo(object):
    
    def __init__(self):
        screen_width, screen_height = 200, 200
        os.environ['SDL_VIDEODRIVER'] = 'dummy'
        
        pygame.init()
        pygame.display.init()
        
        self.screen = pygame.display.set_mode((screen_width, screen_height))
    
    def save_non_alpha_image(self):
        r = 50
        color = RGB['blue']
        img_path = '/tmp/non_alpha_image.png'
        
        background = pygame.Surface((200, 200), pygame.SRCALPHA, 32)
        pygame.draw.rect(background, RGB['lightgray'], (0, 0, 200, 200), 0)
        
        self.screen.fill(RGB['black'])
        self.screen.blit(background, (0,0))
        pygame.draw.circle(self.screen, color, (100,100), r, 0)
        
        pygame.image.save(self.screen, img_path)
        print "image saved to %s" % (img_path)
    
    def save_alpha_image(self):
        r = 50
        color = RGB['blue']
        img_path = '/tmp/alpha_image.png'
        
        background = pygame.Surface((200, 200), pygame.SRCALPHA, 32)
        pygame.draw.rect(background, RGB['lightgray'], (0, 0, 200, 200), 0)
        
        transparent_circle = self.draw_transparent_circle(r, color, 50)
        
        self.screen.fill(RGB['black'])
        self.screen.blit(background, (0,0))
        self.screen.blit(transparent_circle, (50,50))
        
        pygame.image.save(self.screen, img_path)
        print "image saved to %s" % (img_path)
    
    def draw_transparent_circle(self, radius, color, transparency=0):
        """transparency is value between 0 and 100, 0 is opaque,
        100 invisible"""
        width, height = radius*2, radius*2
        
        # transparent base surface
        flags = pygame.SRCALPHA
        depth = 32
        base = pygame.Surface((width, height), flags, depth)
        
        # alpha surface
        alpha = int(round(255 * (100-transparency) / 100.0))
        alpha_surface = pygame.Surface((width, height))
        alpha_surface.set_colorkey(RGB['black'])
        alpha_surface.set_alpha(alpha)
        
        # draw circle (to alpha surface)
        pygame.draw.circle(alpha_surface, color, (radius,radius), radius)
        
        # draw alpha surface to base surface
        base.blit(alpha_surface, (0,0))
        
        return base
    
    
demo = PygameProblemDemo()
demo.save_non_alpha_image()
demo.save_alpha_image()

Here are the results:

non_alpha_image.png

non_alpha_image.png

alpha_image.png

alpha_image.png


Update

If I comment out the line os.environ['SDL_VIDEODRIVER'] = 'dummy' in the constructor, the transparent image is successfully saved:

alpha_image.png

alpha_image.png

However, that is the line the suppresses the window from opening up, so the script can no longer run headlessly. :\

  • 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-06-19T03:12:20+00:00Added an answer on June 19, 2026 at 3:12 am

    I’ve figured it out. Pygame display needed to be initiated with alpha channel enabled. To fix my example above, simply replace this line in the constructor:

    self.screen = pygame.display.set_mode((screen_width, screen_height))
    

    With this:

    self.screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-on question from the one I asked here . Can constraints
An interviewer recently asked me this question: given three boolean variables, a, b, and
OK, so this is leading on from another question I asked here recently. Basically,
i recently asked this question and i got a lot of great answers breaking
i recently asked this question on how to have a container div width set
This question follows on from the questions here and here . I have recently
How can I generate a DebuggerStepThroughAttribute over a getter/setter with CodeDOM? This question follows
This question follows on from a previous question, that has raised a further issue.
This question follows on from a previous question. However stackoverflow presents me from commenting
this follows on from my last question which I thought was answered but for

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.