如何逐像素绘制正方形(Python,PIL)

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

在空白处canvas 我想使用 Pillow 逐个像素地绘制一个正方形。

我试过使用 img.putpixel((30,60), (155,155,55)) 来绘制一个像素,但它没有做任何事情。

from PIL import Image

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

    return img

wallpaper = newImg()

wallpaper.show()

运行 你说你试过的代码完全有效,见下文。

要绘制矩形,请使用其他坐标重复 img.putpixel((30,60), (155,155,55)) 命令。

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