Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do I easily generate an image file from some source data?

I have some data that I would like to visualize. Each byte of the source data roughly corresponds to a pixel value of the image.

What is the easiest way to generate an image file (bitmap?) using Python?

like image 510
Jesse Vogt Avatar asked Jun 24 '09 13:06

Jesse Vogt


People also ask

How do you generate an image using Python code?

new() method creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels. The color is given as a single value for single-band images, and a tuple for multi-band images (with one value for each band).


2 Answers

You can create images with a list of pixel values using Pillow:

from PIL import Image

img = Image.new('RGB', (width, height))
img.putdata(my_list)
img.save('image.png')
like image 171
Armandas Avatar answered Oct 20 '22 11:10

Armandas


Have a look at PIL and pyGame. Both of them allow you to draw on a canvas and then save it to a file.

like image 40
scvalex Avatar answered Oct 20 '22 10:10

scvalex