4

I was wondering, what is the simplest way to draw a pixel in python with x and y values?

Ale Kid
  • 79
  • 1
  • 1
  • 5

1 Answers1

7

Probably the simplest way is use PIL (Python Imaging Library) which makes it possible in 4 lines:

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

The documentation shows lots of other ways to customize it http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#examples.

  • 2
    I would suggest [Pillow](https://pypi.python.org/pypi/Pillow/4.1.1) which it's a fork of PIL but maintaned by Plone team. Otherwise, +1 because your solution still so easy with a short code. – Chiheb Nexus May 25 '17 at 23:44
  • firstly, what does the 'RGBA' value do? – Ale Kid May 27 '17 at 03:58
  • second of all, how would i do this for multiple random positions? – Ale Kid May 27 '17 at 04:00
  • @AleKid, you're right, `'RGBA'` is unnecessary in this example, the parameters for a new [Image.new(mode, size) ⇒ image](http://effbot.org/imagingbook/image.htm#functions), defined as: `'mode of an image defines the type and depth of a pixel in the image: RGBA (4x8-bit pixels, true colour with transparency mask)'`I just took it arbitrarily from this list [concepts.htm#mode](http://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#modes), more simple would be use `'1 (1-bit pixels, black and white, stored with one pixel per byte)'`, I updated this in the answer – chickity china chinese chicken May 27 '17 at 06:57
  • Must i save the pixel? I am trying to draw many random pixels, and store their position so i can move them later, to simulate air movement. There doesn't happen to be any way to store the pixel position using PIL? thank you @downshift. – Ale Kid May 29 '17 at 06:06