【问题标题】:Best way to draw pixel in python [closed]在python中绘制像素的最佳方法[关闭]
【发布时间】:2017-05-25 22:43:43
【问题描述】:

我想知道,在 python 中用 x 和 y 值绘制像素的最简单方法是什么?

【问题讨论】:

标签: python


【解决方案1】:

可能最简单的方法是使用PIL(Python 图像库),它可以在 4 行中实现:

from PIL import Image, ImageColor

im = Image.new('1', (1,1)) # create the Image of size 1 pixel 
im.putpixel((0,0), ImageColor.getcolor('black', '1')) # or whatever color you wish

im.save('simplePixel.png') # or any image format

该文档显示了许多其他自定义方法http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#examples

【讨论】:

  • 我建议 Pillow 它是 PIL 的一个分支,但由 Plone 团队维护。否则,+1,因为您的解决方案仍然很容易使用简短的代码。
  • 首先,'RGBA'值有什么作用?
  • 其次,我将如何为多个随机位置执行此操作?
  • @AleKid,你说得对,'RGBA'在这个例子中是不需要的,新的Image.new(mode, size) ⇒ image的参数,定义为:'mode of an image defines the type and depth of a pixel in the image: RGBA (4x8-bit pixels, true colour with transparency mask)'我只是从这个列表中随意取的@987654325 @,更简单的是使用'1 (1-bit pixels, black and white, stored with one pixel per byte)',我在答案中更新了这个
  • 我必须保存像素吗?我正在尝试绘制许多随机像素,并存储它们的位置,以便稍后移动它们以模拟空气运动。没有任何方法可以使用 PIL 存储像素位置吗?谢谢@downshift。
猜你喜欢
  • 2014-09-10
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多