Search code examples
pythonpython-3.xpyautogui

Why the python program is not creating any pixel?


import pyautogui as pug
from PIL import Image, ImageGrab
from time import sleep
from numpy import asarray

def screenshot():
    """Takes Screenshot of the current window"""
    img = ImageGrab.grab().convert('L')  #To convert the current image in B & W format 
    
    return img


if __name__ == "__main__":
    sleep(3)
    img = screenshot()
    data = img.load()
    print(asarray(img))  #Displays the image in matrix form
    #To make a black pixel over the image at coordinate(X:460 Y:320) [Not working :/]
    data[420,320] = 0
    img.show()

This code should create a black pixel over the image: "img" at the coordinate 420, 320 But the problem is that it doesn't create such pixel. What am I doing wrong?

Thank you


Solution

  • It actually works! 1 pixel is too small to see. You can increase the area you want to make black.

    Try this

    for _ in range(410, 430):
        for __ in range(310, 330):
            data[_, __] = 0
    

    Instead of

    data[420, 320] = 0