How to draw square pixel by pixel (Python, PIL)
Asked Answered
C

1

6

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()
Chekiang answered 24/1, 2019 at 11:53 Comment(2)
Have you looked into PIL.ImageDraw? pillow.readthedocs.io/en/3.0.x/reference/…Ephrem
No, I can't use it, it's too complex for my task, it have to draw something on my own, pixel by pixel.Chekiang
B
8

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

Bight answered 24/1, 2019 at 12:57 Comment(1)
Thank you! I must have put img.putpixel in wrong place in code.Chekiang

© 2022 - 2024 — McMap. All rights reserved.