Is there a good color map for converting an image from gray to bright using python PIL?

Matplotlib has many good color maps, but it doesn’t work well. I am writing code to make the image in gray, where interpolation with a color map is a good idea. I wonder if there are open source color maps or demo code available to use Pillow to convert images from gray to vibrant colors through colormap?




Specify:

  • Matplotlib is good for demo use, but works poorly for thounsands of images.
  • Matplotlib speakers
  • You can match grayscale images with colors to get vibrant colors.

Demo:

The first image is in shades of gray, the second is in the inkjet cmap, and the third is hot.

Matplotlib demo

The problem is that I know little about colors, and I would like to achieve such effects in PIL for better performance.

+2
python matplotlib colors python-imaging-library
Apr 17 '17 at 18:31 on
source share
2 answers

I realized with the duplicate answer mentioned in @ImportanceOfBeingErnest ( How to convert a Numpy array to a PIL image using the matplotlib template )

import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np import timeit from PIL import Image def pil_test(): cm_hot = mpl.cm.get_cmap('hot') img_src = Image.open('test.jpg').convert('L') img_src.thumbnail((512,512)) im = np.array(img_src) im = cm_hot(im) im = np.uint8(im * 255) im = Image.fromarray(im) im.save('test_hot.jpg') def rgb2gray(rgb): return np.dot(rgb[:,:,:3], [0.299, 0.587, 0.114]) def plt_test(): img_src = mpimg.imread('test.jpg') im = rgb2gray(img_src) f = plt.figure(figsize=(4, 4), dpi=128) plt.axis('off') plt.imshow(im, cmap='hot') plt.savefig('test2_hot.jpg', dpi=f.dpi) plt.close() t = timeit.timeit(pil_test, number=30) print('PIL: %s' % t) t = timeit.timeit(plt_test, number=30) print('PLT: %s' % t) 

The result of work:

 PIL: 1.7473899199976586 PLT: 10.632971412000188 

They both give me a similar result with the hot color map.

test image with hot CMap

+3
Apr 23 '17 at 4:46 on
source share

You can use color maps from matplotlib and apply them without any matplotlib digits, etc. This will significantly speed up the work:

 import matplotlib.pyplot as plt # Get the color map by name: cm = plt.get_cmap('gist_rainbow') # Apply the colormap like a function to any array: colored_image = cm(image) # Obtain a 4-channel image (R,G,B,A) in float [0, 1] # But we want to convert to RGB in uint8 and save it: Image.fromarray((colored_image[:, :, :3] * 255).astype(np.uint8)).save('test.png') 

Note:

  • If your input image is floating, the values ​​should be in the range [0.0, 1.0] .
  • If your input image is integer, integers should be in the range [0, N) , where N is the number of colors on the map. But you can redo the map to any number of values ​​according to your needs:

     # If you need 8 color steps for an integer image with values from 0 to 7: cm = plt.get_cmap('gist_rainbow', lut=8) 
0
Dec 18 '17 at 10:38 on
source share



All Articles