[pygame] Blitting image with alpha channel on a transparent background

2,539 views
Skip to first unread message

stas zytkiewicz

unread,
Apr 22, 2010, 10:59:02 AM4/22/10
to pygame...@seul.org
Hello, I hope that someone can point me in the right direction.
I have a semi transparent background surface created with set_alpha(100).
On this surface I want to blit an png image which has a alpha channel.
The problem is that when I blit the png image surface to the background
surface the png image surface is also set to the same alpha value as the
background, 100 in this example.
As a result the complete image is semi transparent.

Thanks,
Stas

--
Free-source educational programs for schools
http://www.schoolsplay.org and http://wiki.laptop.org/go/Schoolsplay
http://gvr.sf.net and http://wiki.laptop.org/go/Guido_van_Robot


--
Subscription settings: http://groups.google.com/group/pygame-mirror-on-google-groups/subscribe?hl=en

Lee Buckingham

unread,
Apr 22, 2010, 12:56:42 PM4/22/10
to pygame...@seul.org
If there's no images being displayed behind the background image, then there isn't a need for it to have an alpha value at all, since you'd never see anything through it anyway.

As far as the alpha goes, my guess is that you're getting hung up because you've mixed two different alpha modes.  There's some caveats about using per-pixel alpha (like the .png file probably has) and the other modes (color key or single value alpha like you've got).

So i'd first try just eliminating the transparency on the background, if in fact there's nothing behind it anyway.

(also, I'm fairly new, so sorry if I'm making some mistakes here)

-Lee-

stas zytkiewicz

unread,
Apr 22, 2010, 3:46:40 PM4/22/10
to pygame...@seul.org
On Thu, Apr 22, 2010 at 6:56 PM, Lee Buckingham
<lee.buc...@gmail.com> wrote:

> As far as the alpha goes, my guess is that you're getting hung up because
> you've mixed two different alpha modes.  There's some caveats about using
> per-pixel alpha (like the .png file probably has) and the other modes (color
> key or single value alpha like you've got).
That's my guess to and as I'm no alpha stuff expert I turned to this list.

> So i'd first try just eliminating the transparency on the background, if in
> fact there's nothing behind it anyway.
I want to create a semi transparent GUI button with a solid image or text
so transparency of the background is the whole purpose :-)

Lee Buckingham

unread,
Apr 22, 2010, 5:12:36 PM4/22/10
to pygame...@seul.org
Instead of using set_alpha(), try using a fill, like:

 backgroundsurface.fill((0,0,0,100))

that should make the background use per-pixel alpha too, since there's a fourth number in the color (for alpha) so blitting another per-pixel alpha image shouldn't cause problems.   Hmm... maybe I'll try it out too.

-Lee-

Lee Buckingham

unread,
Apr 22, 2010, 5:25:42 PM4/22/10
to pygame...@seul.org
yeah, that worked for me, here's my (messy) example code:

import pygame
from pygame.locals import *
import os

pygame.init()

screen = pygame.display.set_mode((100,100))
screen.fill([0, 0, 0]) # blank the screen.

#random image
fartherback = pygame.image.load(os.path.join("Images", "Blocks.png")).convert_alpha() 

#equivalent to the background in your example
notasfar = pygame.Surface((100,100)).convert_alpha()
notasfar.fill((0,0,0,50))

#another random image
image = pygame.image.load(os.path.join("Images", "girl-walking-down.png")).convert_alpha()

while True:
    screen.blit(fartherback.subsurface((0,0),(100,100)), (0, 0, 100, 100))
    screen.blit(notasfar, (0,0,100,100))
    screen.blit(image, (0,0,64,64))

    pygame.display.update()
    pygame.time.wait(200)

Greg Ewing

unread,
Apr 22, 2010, 8:59:13 PM4/22/10
to pygame...@seul.org
Lee Buckingham wrote:
> Instead of using set_alpha(), try using a fill, like:
>
> backgroundsurface.fill((0,0,0,100))

Note that you will also have to create the background surface with
the SRCALPHA flag, so that it will have a per-pixel alpha channel.

If you don't use that flag, then the surface only has a single alpha
value for the whole surface. This is why the alpha of the image
you're blitting onto it gets lost -- there's nowhere for it to go.

--
Greg

Keith Nemitz

unread,
Apr 22, 2010, 10:08:43 PM4/22/10
to pygame...@seul.org

I spent the day in py2exe hell. After grinding through six separate problems, now I'm stuck. Has anyone got this combo to work? I'm using Python 2.6 and Pygame 1.9.x and pyOpenGl 3.0.x and the latest py2exe. When I run the resulting exe, I get the following runtime error:

Traceback (most recent call last):
File "main.py", line 29, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "pygl2d\__init__.pyo", line 5, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "pygl2d\window.pyo", line 3, in <module>
File ".\OpenGL\GL\__init__.py", line 2, in <module>
from OpenGL.raw.GL import *
File ".\OpenGL\raw\GL\__init__.py", line 6, in <module>
from OpenGL.raw.GL.constants import *
File ".\OpenGL\raw\GL\constants.py", line 7, in <module>
from OpenGL import platform, arrays
File ".\OpenGL\arrays\__init__.py", line 22, in <module>
formathandler.FormatHandler.loadAll()
File ".\OpenGL\arrays\formathandler.py", line 37, in loadAll
cls.loadPlugin( entrypoint )
File ".\OpenGL\arrays\formathandler.py", line 44, in loadPlugin
plugin_class = entrypoint.load()
File ".\OpenGL\plugins.py", line 14, in load
return importByName( self.import_path )
File ".\OpenGL\plugins.py", line 28, in importByName
module = __import__( ".".join(moduleName), {}, {}, moduleName)
File ".\OpenGL\arrays\strings.py", line 8, in <module>
psas = ctypes.pythonapi.PyString_AsString
File "ctypes\__init__.pyo", line 366, in __getattr__
File "ctypes\__init__.pyo", line 371, in __getitem__
AttributeError: function 'PyString_AsString' not found




Here is the script I'm currently using, based on the pygame wiki script:


# This will create a dist directory containing the executable file, all the data
# directories. All Libraries will be bundled in executable file.
#
# Run the build process by entering 'pygame2exe.py' or
# 'python pygame2exe.py' in a console prompt.
#
# To build exe, python, pygame, and py2exe have to be installed. After
# building exe none of this libraries are needed.

try:
from distutils.core import setup
import py2exe, pygame
from modulefinder import Module
import glob, fnmatch
import sys, os, shutil
import operator
except ImportError, message:
raise SystemExit, "Unable to load module. %s" % message

#---HACK
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ["sdl_ttf.dll","sdl_mixer.dll","libogg-0.dll"]:
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
#---ENDHACK


class pygame2exe(py2exe.build_exe.py2exe): #This hack make sure that pygame default font is copied: no need to modify code for specifying default font
def copy_extensions(self, extensions):
#Get pygame default font
pygamedir = os.path.split(pygame.base.__file__)[0]
pygame_default_font = os.path.join(pygamedir, pygame.font.get_default_font())

#Add font to list of extension to be copied
extensions.append(Module("pygame.font", pygame_default_font))
py2exe.build_exe.py2exe.copy_extensions(self, extensions)

class BuildExe:
def __init__(self):
#Name of starting .py
self.script = "main.py"

#Name of program
self.project_name = "Around"

#Project url
self.project_url = "about:none"

#Version of program
self.project_version = "0.0.0.1"

#License of the program
self.license = "MyApps License"

#Auhor of program
self.author_name = "Keith Nemitz"
self.author_email = "musenik atya hoo doh com"
self.copyright = "Copyright (c) 2010 Keith Nemitz. All rights reserved."

#Description
self.project_description = "..."

#Icon file (None will use pygame default icon)
self.icon_file = None

#Extra files/dirs copied to game
self.extra_datas = ["data","OpenGL","extras/Microsoft.VC90.CRT.manifest",
"extras/msvcm90.dll","extras/msvcp90.dll","extras/msvcr90.dll"]

#Extra/excludes python modules
self.extra_modules = []
self.exclude_modules = ["OpenGL"]

#DLL Excludes
self.exclude_dll = ['']

#Zip file name (None will bundle files in exe instead of zip file)
self.zipfile_name = None

#Dist directory
self.dist_dir ='dist'

## Code from DistUtils tutorial at http://wiki.python.org/moin/Distutils/Tutorial
## Originally borrowed from wxPython's setup and config files
def opj(self, *args):
path = os.path.join(*args)
return os.path.normpath(path)

def find_data_files(self, srcdir, *wildcards, **kw):
# get a list of all files under the srcdir matching wildcards,
# returned in a format to be used for install_data
def walk_helper(arg, dirname, files):
if '.svn' in dirname:
return
names = []
lst, wildcards = arg
for wc in wildcards:
wc_name = self.opj(dirname, wc)
for f in files:
filename = self.opj(dirname, f)

if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
names.append(filename)
if names:
lst.append( (dirname, names ) )

file_list = []
recursive = kw.get('recursive', True)
if recursive:
os.path.walk(srcdir, walk_helper, (file_list, wildcards))
else:
walk_helper((file_list, wildcards),
srcdir,
[os.path.basename(f) for f in glob.glob(self.opj(srcdir, '*'))])
return file_list

def run(self):
if os.path.isdir(self.dist_dir): #Erase previous destination dir
shutil.rmtree(self.dist_dir)

#Use the default pygame icon, if none given
if self.icon_file == None:
path = os.path.split(pygame.__file__)[0]
self.icon_file = os.path.join(path, 'pygame.ico')

#List all data files to add
extra_datas = []
for data in self.extra_datas:
if os.path.isdir(data):
extra_datas.extend(self.find_data_files(data, '*'))
else:
extra_datas.append(('.', [data]))

setup(
cmdclass = {'py2exe': pygame2exe},
version = self.project_version,
description = self.project_description,
name = self.project_name,
url = self.project_url,
author = self.author_name,
author_email = self.author_email,
license = self.license,

# targets to build
windows = [{
'script': self.script,
'icon_resources': [(0, self.icon_file)],
'copyright': self.copyright
}],
options = {'py2exe': {'optimize': 2, 'bundle_files': 1, 'compressed': True, "includes": ["ctypes", "logging"],
'excludes': self.exclude_modules, 'packages': self.extra_modules,
'dll_excludes': self.exclude_dll} },
zipfile = self.zipfile_name,
data_files = extra_datas,
dist_dir = self.dist_dir
)

if os.path.isdir('build'): #Clean up build dir
shutil.rmtree('build')

if __name__ == '__main__':
if operator.lt(len(sys.argv), 2):
sys.argv.append('py2exe')
BuildExe().run() #Run generation
raw_input("Press enter key to continue") #Pause to let user see that things ends

James Paige

unread,
Apr 22, 2010, 11:58:43 PM4/22/10
to pygame...@seul.org
pyopengl 3.0 is notoriously difficult to py2exe.

However, I was able to find this page, which suggests a possible
solution:

http://blog.vrplumber.com/index.php?/archives/325-Got-py2exe-+-PyOpenGL-limping-across-the-line...-Need-to-add-the-.egg-files-to-the-path.html

---
James Paige

Casey Duncan

unread,
Apr 23, 2010, 12:37:33 AM4/23/10
to pygame...@seul.org
What you are lacking is actually part of Python itself (or the stdlib):

Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> ctypes.pythonapi.PyString_AsString
<_FuncPtr object at 0x3697b0>

It might be that py2exe is unable to DWIM that you are using the ctypes.pythonapi subpackage.

Perhaps you need to add an 'import ctypes.pythonapi' to one of your modules (like the main one) or otherwise explicitly tell py2exe to include it?

-Casey

stas zytkiewicz

unread,
Apr 23, 2010, 1:46:41 AM4/23/10
to pygame...@seul.org
On Fri, Apr 23, 2010 at 2:59 AM, Greg Ewing <greg....@canterbury.ac.nz> wrote:
> Lee Buckingham wrote:
>>
>> Instead of using set_alpha(), try using a fill, like:
>>
>>  backgroundsurface.fill((0,0,0,100))
>
> Note that you will also have to create the background surface with
> the SRCALPHA flag, so that it will have a per-pixel alpha channel.

Thank you guys, it worked :-)
I've combined the comments from Lee and Greg and came up with this
little piece of test code for a semi transparent button like image which
I just post here in case anyone else needs such a thing.

import pygame
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((400,400))
screen.fill([70, 70, 70]) # blank the screen.

# button background
back = pygame.Surface((200,200),SRCALPHA)
back.fill((0,0,0,50))
pygame.draw.rect(back,(0,0,0),back.get_rect(),2)
# random image
image = pygame.image.load('test1.png').convert_alpha()
back.blit(image,(10,10))

# draw some lines to get stuff in the background
for y in range(0,300,10):
pygame.draw.line(screen,(0,0,0),(0,y),(300,y))

screen.blit(back,(10,10))
pygame.display.update()

while True:
pygame.time.wait(200)


Thanks,
Reply all
Reply to author
Forward
0 new messages