Реализация Python ответа, данного teng (при условии, что по умолчанию используется matplotlib jetmap).
import pylab as plt
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def PIL2array(img):
return numpy.array(img.getdata(),
numpy.uint8).reshape(img.size[1], img.size[0], 3)
def array2PIL(arr, size):
mode = 'RGBA'
arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2])
if len(arr[0]) == 3:
arr = numpy.c_[arr, 255*numpy.ones((len(arr),1), numpy.uint8)]
return Image.frombuffer(mode, size, arr.tostring(), 'raw', mode, 0, 1)
def return_x(y,color,direction):
if color == "blue" and direction == "up":
m = (4.0 + 6.0/11.0)
c = 0.5
elif color == "blue" and direction == "down":
m = -3.226
c = 2.097
elif color == "green" and direction == "up":
m = 4.0
c = -0.5
elif color == "green" and direction == "down":
m = -3.704
c = 3.370
elif color == "red" and direction == "up":
m = 3.223
c = -1.129
elif color == "red" and direction == "down":
m = -4.545
c = 5.041
else:
print "Returning y:: INCORRECT OPTIONS"
m = 1
c = 0
return (y-c)/m
# x >= y
def big_equal(x,y):
return x > y or np.allclose(x,y)
# x <= y
def less_equal(x,y):
return x < y or np.allclose(x,y)
def convert_jet_to_grey(img_array,n):
new_image = np.zeros((img_array.shape[0],img_array.shape[1]))
for i in range(img_array.shape[0]):
for j in range(img_array.shape[1]):
pixel_blue = img_array[i,j,2]
pixel_green = img_array[i,j,1]
pixel_red = img_array[i,j,0]
if (pixel_blue < 1) and big_equal(pixel_blue,0.5) and less_equal(pixel_green,0.5) :
#print "a1"
#print "i,j = ",i,",",j
new_image[i,j] = return_x(pixel_blue,"blue","up")**n
elif np.allclose(pixel_blue,1.0) and big_equal(pixel_green,0):
#print "b1"
#print "i,j = ",i,",",j
new_image[i,j] = return_x(pixel_green,"green","up")**n
elif (pixel_blue < 1) and big_equal(pixel_blue,0.4) and big_equal(pixel_green,0.5):
#print "c1"
#print "i,j = ",i,",",j
new_image[i,j] = return_x(pixel_green,"blue","down")**n
elif (pixel_red < 1) and big_equal(pixel_red,0.4) and big_equal(pixel_green,0.5):
#print "c2"
#print "i,j = ",i,",",j
new_image[i,j] = return_x(pixel_green,"red","up")**n
elif np.allclose(pixel_red,1.0) and big_equal(pixel_green,0):
#print "b2"
#print "i,j = ",i,",",j
new_image[i,j] = return_x(pixel_green,"green","down")**n
elif (pixel_red < 1) and big_equal(pixel_red,0.5) and less_equal(pixel_green,0.5):
#print "a2"
#print "i,j = ",i,",",j
new_image[i,j] = return_x(pixel_blue,"red","down")**n
else:
print "Leaving 0:: NOT A JET IMAGE"
return new_image
def main():
img = Image.open('test.jpg')
arr = PIL2array(img)
img_new = convert_jet_to_grey(arr/255.0,1)
imgplot = plt.imshow(img_new)
plt.show()
if __name__ == '__main__':
main()