GTK drawable area transparent background color - python

I have a GTK drawing area and I want to have an image display as the background for it, while other things can be drawn over it.
My first attempt at this involved me simply taking the image, putting it into a pixmap, and drawing it before I draw other objects. This resulted in the objects backgrounds completely covering my background image.
Now I am thinking I need to change the object's pixmap's gtk.gdk.GC so the background color it has is transparent. Here is where I am having problems. I do not know how to set a gtk.gdk.Color's alpha.
How do I set a gtk.gdk.Color's alpha? (or any other way of making the color transparent)
If this idea seems unlikely to work please let me know. I am pretty new to GTK.

I don't know if this can help you, but my best advice is to use Cairo: an open source 2D graphics library providing cross-platform support for advanced 2D drawing. It is also the easiest way to draw 2D graphics, I think
(I've tried GDK in the past and I thought it was much more tricky to use).
In python you can use the specific bindings Pycairo.
Here you can learn more about it, with easy tutorials.
For your specific problem, see more about images
and trasparency.
The main function for trasparency are:
set_source_rgba(red, green, blue[, alpha=1.0])
paint_with_alpha(alpha)
A great summary to learn how to manage images and transparency is in the example Reflected image in the same page linked before about trasparency.

Related

Simple animations in Python 2.7 [duplicate]

This question already has answers here:
simple animation using tkinter
(2 answers)
Closed 6 years ago.
In Python 2.7, I want to display some simple graphics, such as a red square moving around on a blue window. On every frame, I want to update the position of the square, and render the new window. I am looking for something simple and lightweight.
I have seen people use matplotlib for drawing shapes, but I don't want to have to deal with axes and data points. I have also seen pygame suggested, but this seems to heavyweight for what I want, as I do not want to create a game, just a simple animation.
So what I really want is something where on every frame, I just indicate the colour of every pixel on an image, and then display that image. What are some good suggestions?
Tkinter is not good for setting individual pixels. If you want to move rectangles or ovals though (a small oval will look like a pixel, but it doesn't scale for updating a whole image).
def update(x,y):
canvas.delete('all')
canvas.create_rectangle(x-1,y-1,x+1,y+1)
You can, of course, be more judicious, saving the return value of the rectangle and then only clear the appropriate elements. Or you can move existing elements directly as Bryan points out. As he explains elsewhere Tkinter of course, supports drawing images, ovals, and a slew of other things. Here's a canonical source edit: that is old and not canoncial This one's slightly better For a general source on animating with a timer loop, here's Bryan agian
Bryan also noted that you can work with pixels directly You can do that with PhotoImage.
Array-Like Pixel Access Without Graphical Extensions
A robust module like pygame will be the most scalable option. However, I've had success (in educational settings only) writing graphics engines by modifying the elements of a numpy array and then displaying it as an image (you also need this link to display the images).
This lets you do pixel level modifications; since it's relatively trivial to write C-extensions that modify numpy arrays, you can prototype fast image processing doing custom manipulations. While I've written whole graphics engines using just tkinter this way, again I can only reccomend it for educational purposes.
Otherwise, just bite the bullet and pull in openGl or pygame. You'll save yourself a ton of time in the long run.
Summary
Very simple animations can be done right in tkinter
For educational purposes, you can do arbitrary graphics with numpy and tkinter
For rhobust animations, check out a full library (openGl, matplotlib, pygame) that suits your needs (graphical rendering, statistical graphing, game development, etc.)

Python & translucent canvas

I'm looking for a way to draw real translucent lines with pygtk or wxPython, I now this is not possible with TKinter (I like this one because its already there). Also, I already now I can do this using PIL, but I want to draw direct to a canvas and it will be a dinamic content so I think using PIL will be a slow solution (and not elegant). Someone know a way to do this? Or maybe another toolkit that allows me to do this.
To get clear what I want is: If I drew lines with distinct colors that overlaps I need to be able to see both lines with a merged color at the intersection. Something like if the color I choose for the pen have a transparence instead of solid color.
Thanks.
Pretty much all "canvas" libraries have brushes with alpha.
PyGTK has the gtk.DrawingArea, often used with cairo.
Cairo can be used with wxPython as well.
QT has the Graphics View Framework.

Drawing pixels on top of Gstreamer

I'm trying to build an application in Python that can draw things on top of video. I have not found a way to do this using gstreamer + Tkinter; I don't think tk lets you do transparent Canvases. So I've looked at using gtk instead, but I'm a bit lost- I would like to be able to just drop some sort of transparent overlay on top and push pixels, but I don't think there's such a thing as a transparent DrawingArea, either. So I need a way to edit the contents of a DrawingArea after each frame of video is in it but before it's shown to the screen. I tried using expose-event but that hasn't worked.
Any suggestions on where to go from here? I want my final product to be a little interface to let the user draw lines and polygons on top of a video as well as drawing pixels programatically- and, if possible, save the result to frames and/or video. So a direction that is more likely to make that possible would be preferred.
Edit: Tried using the "handoff" signal but it flickers madly. PiTiVi has a custom Pipeline that has a state-changed signal which they use to draw lines and circles with Cairo. So this is totally doable...
Edit 2: Right, okay. PiTiVi only draws when the video is paused; I guess I can deal with that. That's what it was using the state changed signal for- you can watch for state change messages and emulate a signal. I can deal with that, but it would be really nice to draw over every frame.

Quickest way to convert Cairo Surface to Pygame on OS-X

I'm looking for a way to draw smooth animations in Python. I want to use cairo, partly because I like the sub-pixel filtering and partly because I'm familiar with the API. My first approach was to use a GTK.DrawingArea as the target for a cairo surface. While the drawing was quick I couldn't find any reliable way to tie the display / buffering to the vertical sync so the animation was jerky and unreliable.
My next approach was to try PyGame. Using the examples in the wiki as a starting point I've written some simple code to animate various bouncing balls. The different approaches on the linked page fall into two categories:
Draw to an offscreen cairo
ImageSurface and then use Numpy to
convert the pixel buffer to a Pygame
surface.
Share the same memory for both
surfaces
The first approach sucks for performance as the conversion takes about 10ms, which is most of the time-slice that I have for 60hz frames. I'm running the code on a Macbook-pro with 2.2Ghz Core2Duo and an Nvidia 8400. The time is very dependent on the size of the surface, this is for a 800x800 window.
The second approach surfaces from the ordering of pixel coordinates. Both cairo and pygame insist that they can only use RGB pixel ordering and don't support conversion. The problem is that when I setup a pygame surface it uses BGRA pixel ordering, which completely shafts me.
So now for the questions:
Is it possible to change the pixel
format used by either library on the
mac to be compatible with each
other?
If it is not possible what is the
fastest way to do the conversion
purely in Python?
If the fastest Python way still
sucks up most of the time for a
frame then how can I interface to
some C code to do the conversion?
Depending on how complex the
interface to C is, how much point is
there in using pygame instead of
just writing the SDL interface in C?
.
This tutorial also could help you: http://www.pygame.org/wiki/CairoPygame
These may be of help pygame #cairo , squirtle svg pygame
And another thread: SVG rendering in a PyGame application

Is it possible to make text translucent in wxPython?

I am adding some wx.StaticText objects on top of my main wx.Frame, which already has a background image applied. However, the StaticText always seems to draw with a solid (opaque) background color, hiding the image. I have tried creating a wx.Color object and changing the alpha value there, but that yields no results. Is there any way I can put text on the frame and have the background shine through? And furthermore, is it possible to make the text itself translucent? Thanks.
You probably need some graphics rendering widget. As far as I know, in wxPython you can use either built-in wxGraphicsContext or pyCairo directly. Cairo is more powerful. However, I don't know the details.
I would try aggdraw into a small canvas.
Any Static Text uses the platform's native label machinery, so you don't get that sort of control over it.

Categories

Resources