有什么好的颜色图可以使用 python 的 PIL 将灰度图像转换为彩色图像?
Is there any good color map to convert gray-scale image to colorful ones using python's PIL?
Matplotlib 有很多不错的颜色图,但是性能不好。我正在编写一些代码来使灰度图像变得丰富多彩,其中使用彩色图进行插值是个好主意。请问有没有开源的color map或者demo代码可以使用Pillow通过colormap将灰度图转成彩色图?
澄清:
- Matplotlib 适合演示使用,但对数千张图像表现不佳。
- Matplotlib colormaps
- 您可以将灰度图像映射到颜色图以获得彩色图像。
演示:
第一个图像是灰度图像,第二个图像映射到 'jet' cmap,第三个图像 'hot'。
问题是我对颜色了解不多,想在PIL中实现这样的效果,性能更好
我想出了@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
色图给了我相似的结果。
您可以使用 matplotlib 中的颜色图并应用它们而无需任何 matplotlib 图形等。
这将使事情变得更快:
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')
注:
- 如果您的输入图像是浮动的,则值应该在
[0.0, 1.0]
. 区间内
如果您的输入图像是整数,则整数应在 [0, N)
范围内,其中 N
是地图中的颜色数。但是您可以根据需要将地图重新采样为任意数量的值:
# If you need 8 color steps for an integer image with values from 0 to 7:
cm = plt.get_cmap('gist_rainbow', lut=8)
Matplotlib 有很多不错的颜色图,但是性能不好。我正在编写一些代码来使灰度图像变得丰富多彩,其中使用彩色图进行插值是个好主意。请问有没有开源的color map或者demo代码可以使用Pillow通过colormap将灰度图转成彩色图?
澄清:
- Matplotlib 适合演示使用,但对数千张图像表现不佳。
- Matplotlib colormaps
- 您可以将灰度图像映射到颜色图以获得彩色图像。
演示:
第一个图像是灰度图像,第二个图像映射到 'jet' cmap,第三个图像 'hot'。
问题是我对颜色了解不多,想在PIL中实现这样的效果,性能更好
我想出了@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
色图给了我相似的结果。
您可以使用 matplotlib 中的颜色图并应用它们而无需任何 matplotlib 图形等。 这将使事情变得更快:
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')
注:
- 如果您的输入图像是浮动的,则值应该在
[0.0, 1.0]
. 区间内
如果您的输入图像是整数,则整数应在
[0, N)
范围内,其中N
是地图中的颜色数。但是您可以根据需要将地图重新采样为任意数量的值:# If you need 8 color steps for an integer image with values from 0 to 7: cm = plt.get_cmap('gist_rainbow', lut=8)