是否有任何好的色彩映射表,可以使用python的PIL将灰度图像转换为彩色图像? [英] Is there any good color map to convert gray-scale image to colorful ones using python's PIL?

查看:648
本文介绍了是否有任何好的色彩映射表,可以使用python的PIL将灰度图像转换为彩色图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib有很多好的颜色图,但是性能很差.我正在编写一些代码来使灰度图像变得色彩斑where,在彩色地图上进行插值是个好主意.我想知道是否有可用的开源颜色图或演示代码,以使用Pillow通过colormap将灰度图像转换为彩色图像?

Matplotlib has a lot of good color maps, but is bad in performance. I'm writing some code to make gray-scale image colorful where interpolate with color map is a good idea. I wonder whether there are open source color maps available or demo code to use Pillow to convert gray-scale images into colorful ones via colormap?

说明:

  1. Matplotlib可以很好地用于演示,但是对于大量图像却表现不佳.
  2. Matplotlib颜色图
  3. 您可以将灰度图像映射到颜色图以获得彩色图像.

演示:

第一张图片是灰度图像,第二张图片是在'jet'cmap中映射的,第三张图片是'hot'.

The first image is grayscale, second is mapped in 'jet' cmap, third being 'hot'.

问题是我对颜色了解不多,我想在PIL中获得这种效果以获得更好的性能.

The problem is that I do not know much about colors, and I'd like to achieve such effects in PIL for better performance.

推荐答案

我想出了@ImportanceOfBeingErnest(

I figured out with the duplicate answer mentioned by @ImportanceOfBeingErnest (How to convert Numpy array to PIL image applying matplotlib colormap)

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)

性能结果是:

PIL: 1.7473899199976586
PLT: 10.632971412000188

他们都给我类似的hot颜色图结果.

They both give me similar result with hot color map.

这篇关于是否有任何好的色彩映射表,可以使用python的PIL将灰度图像转换为彩色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆