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

2010-04-22 Thread stas zytkiewicz
On Fri, Apr 23, 2010 at 2:59 AM, Greg Ewing  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,
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


Re: [pygame] Pygame, PyOpenGl, and py2exe, OH MY!

2010-04-22 Thread Casey Duncan
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

On Apr 22, 2010, at 8:08 PM, Keith Nemitz wrote:

> 
> 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 
>  File "zipextimporter.pyo", line 82, in load_module
>  File "pygl2d\__init__.pyo", line 5, in 
>  File "zipextimporter.pyo", line 82, in load_module
>  File "pygl2d\window.pyo", line 3, in 
>  File ".\OpenGL\GL\__init__.py", line 2, in 
>from OpenGL.raw.GL import *
>  File ".\OpenGL\raw\GL\__init__.py", line 6, in 
>from OpenGL.raw.GL.constants import *
>  File ".\OpenGL\raw\GL\constants.py", line 7, in 
>from OpenGL import platform, arrays
>  File ".\OpenGL\arrays\__init__.py", line 22, in 
>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 
>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_m

Re: [pygame] Pygame, PyOpenGl, and py2exe, OH MY!

2010-04-22 Thread James Paige
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

On Thu, Apr 22, 2010 at 07:08:43PM -0700, Keith Nemitz wrote:
> 
> 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 
>   File "zipextimporter.pyo", line 82, in load_module
>   File "pygl2d\__init__.pyo", line 5, in 
>   File "zipextimporter.pyo", line 82, in load_module
>   File "pygl2d\window.pyo", line 3, in 
>   File ".\OpenGL\GL\__init__.py", line 2, in 
> from OpenGL.raw.GL import *
>   File ".\OpenGL\raw\GL\__init__.py", line 6, in 
> from OpenGL.raw.GL.constants import *
>   File ".\OpenGL\raw\GL\constants.py", line 7, in 
> from OpenGL import platform, arrays
>   File ".\OpenGL\arrays\__init__.py", line 22, in 
> 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 
> 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 = Non

[pygame] Pygame, PyOpenGl, and py2exe, OH MY!

2010-04-22 Thread Keith Nemitz

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 
  File "zipextimporter.pyo", line 82, in load_module
  File "pygl2d\__init__.pyo", line 5, in 
  File "zipextimporter.pyo", line 82, in load_module
  File "pygl2d\window.pyo", line 3, in 
  File ".\OpenGL\GL\__init__.py", line 2, in 
from OpenGL.raw.GL import *
  File ".\OpenGL\raw\GL\__init__.py", line 6, in 
from OpenGL.raw.GL.constants import *
  File ".\OpenGL\raw\GL\constants.py", line 7, in 
from OpenGL import platform, arrays
  File ".\OpenGL\arrays\__init__.py", line 22, in 
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 
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
  

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

2010-04-22 Thread Greg Ewing

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


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

2010-04-22 Thread Lee Buckingham
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)





On Thu, Apr 22, 2010 at 2:12 PM, Lee Buckingham wrote:

> 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-
>
>
>
>
> On Thu, Apr 22, 2010 at 12:46 PM, stas zytkiewicz <
> stas.zytkiew...@gmail.com> wrote:
>
>> On Thu, Apr 22, 2010 at 6:56 PM, Lee Buckingham
>>  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 :-)
>>
>> 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
>>
>
>


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

2010-04-22 Thread Lee Buckingham
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-



On Thu, Apr 22, 2010 at 12:46 PM, stas zytkiewicz  wrote:

> On Thu, Apr 22, 2010 at 6:56 PM, Lee Buckingham
>  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 :-)
>
> 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
>


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

2010-04-22 Thread stas zytkiewicz
On Thu, Apr 22, 2010 at 6:56 PM, Lee Buckingham
 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 :-)

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


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

2010-04-22 Thread Lee Buckingham
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-

On Thu, Apr 22, 2010 at 7:59 AM, stas zytkiewicz
wrote:

> 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
>


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

2010-04-22 Thread stas zytkiewicz
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