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.