transparency?

53 views
Skip to first unread message

Timothy Lillicrap

unread,
Feb 29, 2012, 7:13:27 AM2/29/12
to glumpy-users
Hi there,

new user to glumpy. So far I've been very impressed. Was wondering
if there is a simple way to do transparency in glumpy, or whether it
has to be handled manually by digging into opengl api?

Specifically, I have a 2D image which I wish to have as a background
and I wish to display/move around a semi-transparent version of a 2nd
image over top of the first.

Thanks for any help,

Tim.

Nicolas Rougier

unread,
Feb 29, 2012, 7:31:14 AM2/29/12
to glumpy-users

Your image needs to have an alpha channel and it should work.

Nicolas


On Feb 29, 1:13 pm, Timothy Lillicrap <timothy.lillic...@gmail.com>
wrote:

Timothy Lillicrap

unread,
Feb 29, 2012, 11:56:07 AM2/29/12
to glumpy-users
Sorry if you're receiving this a second time. I had trouble with the
initial send.

Thank you very much for your quick reply. I've tried to add alpha
channels to by images, but I still have two problems.
(1) Transparency still doesn't seem to work. The addition of the
alpha channel does seem to make the image less "intense" but it does
not allow the image underneath to be seen through it. I've written a
simple piece of code (included below) which demonstrates this and the
second issue.
(2) Addition of the alpha channels slows the movement of the image
(when dragged with the mouse) immensely. Prior to addition of the
alpha channel everything is extremely fast and responsive. I looked
through some of the OpenGL docs and found something which might be
relevant:
QUOTED FROM: http://www.opengl.org/wiki/Common_Mistakes#No_Alpha_in_the_Framebuffer
"No Alpha in the Framebuffer
Be sure you create a double buffered context and make sure you ask for
a alpha component. With GLUT, you can call
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA | GLUT_DEPTH
| GLUT_STENCIL) in which GL_ALPHA asks for a alpha component."

I'm likely just doing something stupid as I'm fairly new to image
manipulation. Thanks for any suggestions you might have,

Tim.

<begin code>

import numpy, glumpy
import Image
import numpy as np

refimg = np.random.rand(512,512)
img = np.random.rand(100,100)

refimg = numpy.asarray(refimg,numpy.float32)
refimg = numpy.asarray([refimg,numpy.ones_like(refimg)]).T
img = numpy.asarray(img,numpy.float32)
img = numpy.asarray([img,0.1*numpy.ones_like(img)]).T

fig = glumpy.figure( (512,512) )
grefimg = glumpy.image.Image(refimg, format='LA')
image = glumpy.image.Image(img, format='LA')
image.x = 0.
image.y = 0.
grefimg.x = 0.
grefimg.y = 0.

@fig.event
def on_draw():
print 'Drawing requested'
fig.clear()
image.update()
image.draw(image.x,image.y,0.,width=fig.width/4.,height=fig.height/
4.)
grefimg.update()
grefimg.draw(grefimg.x,grefimg.y,
0.,width=fig.width,height=fig.height)

@fig.event
def on_mouse_motion(x, y, dx, dy):
print 'Mouse motion (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x,y,dx,dy)

@fig.event
def on_mouse_drag(x, y, dx, dy, button):
print 'Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f, button=%d)' %
(x,y,dx,dy,button)
image.x = x + dx
image.y = y + dy
fig.redraw()

fig.show()

<end code>





On Feb 29, 12:31 pm, Nicolas Rougier <nicolas.roug...@gmail.com>
wrote:

Nicolas Rougier

unread,
Feb 29, 2012, 1:45:20 PM2/29/12
to glumpy-users

Here is an example that should work:

import glumpy
import numpy as np

A = np.random.uniform(0,1, (512,512,2)).astype(np.float32)
B = np.ones((100,100,4)).astype(np.float32)
B *= (0,0,1,.1)

fig = glumpy.figure( (512,512) )
A_img = glumpy.Image(A)
A_img.x,A_img.y = 0,0

B_img = glumpy.Image(B)
B_img.x,B_img.y = 0,0

@fig.event
def on_draw():
fig.clear()

gl.glEnable( gl.GL_BLEND )
gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA )

A[...] = np.random.uniform(0,1, (512,512,2))
A_img.update()
A_img.draw(x=A_img.x, y=A_img.y, z=0.0,
width=fig.width, height=fig.height)

B_img.draw(x=B_img.x-50, y=B_img.y-50, z=1.0,
width=100, height=100)

@fig.event
def on_mouse_motion(x, y, dx, dy):
B_img.x = x
B_img.y = y
fig.redraw()

fig.show()

Timothy Lillicrap

unread,
Feb 29, 2012, 2:55:43 PM2/29/12
to glumpy-users
Phenomenal. Thanks so much. Worked nicely.

Tim.
Reply all
Reply to author
Forward
0 new messages