Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw square pixel by pixel (Python, PIL)

On blank canvas I want to draw a square, pixel by pixel, using Pillow.

I have tried using img.putpixel((30,60), (155,155,55)) to draw one pixel but it doesn't do anything.

from PIL import Image

def newImg():
    img = Image.new('RGB', (1280,768))
    img.save('sqr.png')

    return img

wallpaper = newImg()

wallpaper.show()
like image 432
Ewa Avatar asked Jul 26 '26 14:07

Ewa


1 Answers

Running the code you say you have tried totally works, see below.

To draw the rectangle, repeat the img.putpixel((30,60), (155,155,55)) command with other coordinates.

from PIL import Image

def newImg():
    img = Image.new('RGB', (100, 100))
    img.putpixel((30,60), (155,155,55))
    img.save('sqr.png')

    return img

wallpaper = newImg()
wallpaper.show()

sqr.png

black image with a pixel1

like image 85
SimonF Avatar answered Jul 29 '26 04:07

SimonF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!