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
Related
I have written some code in Python which allows 3D objects to be defined in 3D object space and mapped onto a 2D screen. Currently the finished 2D polygons are drawn on the screen using the PyGame library, which works effectively, but I would like to go the full way and write code myself to complete the drawing operations PyGame does for me. This means I would like to manually control the drawing of each pixel on the screen, with the use of GPU support to accelerate the entire rendering process. From some reading it seems OpenGL is suitable for this sort of thing, but I'm not sure what the complete purpose of OpenGL is and whether I could achieve what I am trying to do in a better way. Do I really need to use OpenGL? Or is there another way for me to directly access my GPU to draw at the pixel by pixel level?
It sounds like OpenGL's programmable shaders are what you're looking for (in particular fragment shaders). They run massively parallel on a pixel-by-pixel basis, in the sense that basically you write a function that takes a single pixel location and computes its color. Note that this means that the individual pixels can't exchange information, though there are certain ways around that.
(Technically when I said "pixel" I meant "fragment", which is sort of a generalized version of a pixel.)
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.)
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.
I have a flash application that I have been working on for 11 months, and would like to translate it to a different language / platform, preferably Python and OpenGL.
One of the main features in my program is to draw flash vector graphics (or display objects) and then redraw them to a bitmap texture. Is there any way to do this in OpenGL? Basically to draw some polygons on the screen, and then draw these polygons onto a texture. If the texture is displayed directly below the polygons , and the polygons are in motion, then there is a dragging/drawing/painting effect.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#draw() --> here is the flash function which I use.
Hopefully someone who is knowledgable in OpenGL & Actionscript would be able to answer this question or provide me with some details. Thankyou!
OpenGL doesn't provide any features for drawing your typical 2D vector graphics. It's a very generic API, but mostly suited for 3D solutions. Implementing the rendering capabilities of Flash in OpenGL is possible, but a lot of work to do yourself.
If you want only a subset (drawing sprites, triangles, convex polygons, lines; alpha blending), then yes, OpenGL may be a good and quick solution.
Otherwise, there's a standard called OpenVG which might be what you want. There are several implementations, some of which may already run on hardware. I haven't tried it so far, though - you'll have to check that one yourself.
So I'm going through the beginning stages of producing a game in Python, and I'm looking for a library that is able to manipulate pixels and blit them relatively fast.
My first thought was pygame, as it deals in pure 2D surfaces, but it only allows pixel access through pygame.get_at(), pygame.set_at() and pygame.get_buffer(), all of which lock the surface each time they're called, making them slow to use. I can also use the PixelArray and surfarray classes, but they are locked for the duration of their lifetimes, and the only way to blit them to a surface is to either copy the pixels to a new surface, or use surfarray.blit_array, which requires creating a subsurface of the screen and blitting it to that, if the array is smaller than the screen (if it's bigger I can just use a slice of the array, which is no problem).
I don't have much experience with PyOpenGL or Pyglet, but I'm wondering if there is a faster library for doing pixel manipulation in, or if there is a faster method, in Pygame, for doing pixel manupilation. I did some work with SDL and OpenGL in C, and I do like the idea of adding vertex/fragment shaders to my program.
My program will chiefly be dealing in loading images and writing/reading to/from surfaces.
Have you tried the Python Imaging Library? You'd still have to communicate the data back to pygame via frombuffer or somesuch to do the blitting, but the PIL can handle the pixel access.
I checked out pyglet, and saw that it works well for static per pixel collision, when the image is not manipulated too much; however, I'm not sure how well it works with a dynamic image.
In short, I'm looking for a library that's able to quickly display a buffer of pixels. This buffer will be constantly changing, so fast access and blitting is essential. This could be done in C with relative ease using SDL; however, I'm looking for a similar method in Python. I'm not even too worried about hardware acceleration at this point, although it would certainly be nice.
Check Python bindings of Simple and Fast Multimedia Library. From it's documentation:
It implements the same 2D drawing and OpenGL-related functions (see their base class sf::RenderTarget for more details), the difference is that the result is stored in an off-screen texture rather than being show in a window.
Rendering to a texture can be useful in a variety of situations:
precomputing a complex static texture (like a level's background from multiple tiles)
applying post-effects to the whole scene with shaders
creating a sprite from a 3D object rendered with OpenGL
etc.
Check also methods contains and intersects of sf::Rect< T > Class Template.