I was wondering, what is the simplest way to draw a pixel in python with x and y values?
Asked
Active
Viewed 1.7k times
4
-
5This may be more than a pixel but it still draws a pixel: `print('.')` – whackamadoodle3000 May 25 '17 at 22:53
-
2Also, https://stackoverflow.com/questions/434583/what-is-the-fastest-way-to-draw-an-image-from-discrete-pixel-values-in-python – whackamadoodle3000 May 25 '17 at 23:05
1 Answers
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.

chickity china chinese chicken
- 7,709
- 2
- 20
- 49
-
2I 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
-
-
-
@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