Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL: using fromarray() with binary data and writing coloured text

I've a basic problem with Python's library PIL. I have some .txt files containing only 0 and 1 values arranged in matrices. I have transformed the "binary" data in an image with the function Image.fromarray() included in PIL. The format of my data produces black&white images if I multiply it by 255, and that's fine for me. Now I want to add some text to the image, using the appropriate text function included in PIL, but I want that text to be coloured. Clearly, I can't do it because the image obtained from fromarray has a grayscale colormap. How can I change it?

like image 948
Francesco Turci Avatar asked Jan 17 '11 09:01

Francesco Turci


People also ask

Is PIL a RGB or BGR?

The basic difference between OpenCV image and PIL image is OpenCV follows BGR color convention and PIL follows RGB color convention and the method of converting will be based on this difference.

What is PIL image format?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.

What is PIL image in Python?

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image.

What is image Fromarray?

fromarray() from the PIL package. The Python Imaging Library ( PIL ) is a library in Python with various image processing functions. The Image. fromarray() function takes the array object as the input and returns the image object made from the array object.29-Dec-2020.


1 Answers

You can get a RGB image from a monochromatic one like this:

from PIL import Image
from numpy import eye                                                            
arr = (eye(200)*255).astype('uint8') # sample array
im = Image.fromarray(arr) # monochromatic image
imrgb = im.convert('RGB') # color image
imrgb.show()
like image 157
François Avatar answered Oct 21 '22 03:10

François