How to draw a pixel on the screen directly?

I’m wanting to do something like the following:

...
pixel[0,0] = [ 254, 0, 0 ] # Draw R at pixel x0y0
pixel[2,1] = [ 0, 254, 0 ] # Draw G at pixel x2y1
pixel[4,2] = [ 0, 0, 254 ] # Draw B at pixel x4y2
...

I hope to display many different configurations of pixels and colours in a short space of time — writing to an intermediary file would be too expensive.

How should I best go about achieving this goal in Python?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Direct answer:

This can only be done with OS-specific APIs. Some OSes do not allow changing pixels on the screen directly.

On Windows, you can use pywin32 libraries to get screen’s device context with dc = GetDC(0) call, then paint pixels with SetPixel(dc, x, y, color).

import win32gui
import win32api

dc = win32gui.GetDC(0)
red = win32api.RGB(255, 0, 0)
win32gui.SetPixel(dc, 0, 0, red)  # draw red at 0,0

Of course, what you paint this way can be erased at any moment.

Right answer:

Painting pixel by pixel is the slowest way to paint something. For example, on Windows, creating an image in memory then painting it in one operation is order of magnitude faster than painting with SetPixel.

If you need speed, use some user interface library for python, for example, Tkinter module or PyQt. Create a window and image in memory, then paint the image on the window. If you need to manipulate pixels, manipulate them in the image and repaint every time.

Solution 2

Although it does not exactely what you say (paint “by pixel”), I like to use Python, GTK and Cairo.

In this case, you create a GTK Window, add a DrawingArea, create a Cairo context, and do paint operations.

The difference is that you have a continuous canvas with floating-point coordinates, and instead of pixels you draw geometric shapes with strokes and fills. You could even draw rectangles one-pixel-sized, and fill them, but that would not be so fast.

Also, you can take any raster object (image, pixbuffer) and render it at once. Then you could generate images in a fast sequence, and render its pixels all at once, similar to a movie.

A working example can be seen in a previous answer here (second snippet):
quickest way to get started with cairo

Solution 3

Im not sure if I get your question right, but I believe you should investigate some framework or middleware that give you an access to screen. I would say PyGame or Pyglet or maybe even Panda. It may be overkill a bit because Pygame and Panda is a framework that let you develop game while Pyglet is wrapper to Opengl in python, but it will let you control pixels, windows and similar with easy.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply