简体   繁体   English

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

[英]How to draw square pixel by pixel (Python, PIL)

On blank canvas I want to draw a square, pixel by pixel, using Pillow. 我想在空白画布上使用Pillow逐像素绘制一个正方形。

I have tried using img.putpixel((30,60), (155,155,55)) to draw one pixel but it doesn't do anything. 我尝试使用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()

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. 要绘制矩形,请使用其他坐标重复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 sqr.png

[ [ 带有像素的黑色图像[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM