I'm trying to figure out different ways to visualize data and how to get a grasp on image processing.
Python 2.6: Creating image from array seems to be doing something different
I thought for a simple example, I could create an image from scratch that varies in pixel intensity. I use the A_pixelIntensity
array to declare the intensities of pixel colors. I then do a transformation in the create_pixel_gradient
function to make the higher intensity pixels darker and the lower intensity pixels whiter.
How can I view this image? Is matplotlib.pyplot
the best way? What about PIL.Image
and iPython.display
?
import numpy as np
#Intensity of Image
A_pixelIntensity = np.asarray([[1,1,1,1,1],[1,2,2,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,1,1,1,1]])
# [[1 1 1 1 1]
# [1 2 2 2 1]
# [1 2 3 2 1]
# [1 2 2 2 1]
# [1 1 1 1 1]]
#Color of Image
def create_pixel_gradient(A_intensity):
maximum = np.amax(A_intensity)
init = np.zeros(A_intensity.shape,dtype=tuple) #Create empty array to populate
for i in range(A_intensity.shape[0]):
for j in range(A_intensity.shape[1]):
pixel_intensity = A_intensity[i,j] #Get original pixel intensities
value = 255 - int(pixel_intensity*(255/np.amax(A_intensity))) #Lower intensities are more white (creates a gradient)
init[i,j] = (value,255,value) #Populate array with RGB values
return(init)
create_pixel_gradient(A_pixelIntensity)
# array([[(170, 255, 170), (170, 255, 170), (170, 255, 170), (170, 255, 170),
# (170, 255, 170)],
# [(170, 255, 170), (85, 255, 85), (85, 255, 85), (85, 255, 85),
# (170, 255, 170)],
# [(170, 255, 170), (85, 255, 85), (0, 255, 0), (85, 255, 85),
# (170, 255, 170)],
# [(170, 255, 170), (85, 255, 85), (85, 255, 85), (85, 255, 85),
# (170, 255, 170)],
# [(170, 255, 170), (170, 255, 170), (170, 255, 170), (170, 255, 170),
# (170, 255, 170)]], dtype=object)