天天看點

python輸入數字成數組_python – 将黑白圖像轉換為數字數組?

python輸入數字成數組_python – 将黑白圖像轉換為數字數組?

如上圖所示,如何将圖像向左轉換為數組,表示圖像的暗度介于0和0之間,而較暗的顔色接近1?如圖所示使用python 3`?

更新:

我曾嘗試過更多的工作.下面也有很好的答案.

# Load image

filename = tf.constant("one.png")

image_file = tf.read_file(filename)

# Show Image

Image("one.png")

#convert method

def convertRgbToWeight(rgbArray):

arrayWithPixelWeight = []

for i in range(int(rgbArray.size / rgbArray[0].size)):

for j in range(int(rgbArray[0].size / 3)):

lum = 255-((rgbArray[i][j][0]+rgbArray[i][j][1]+rgbArray[i][j][2])/3) # Reversed luminosity

arrayWithPixelWeight.append(lum/255) # Map values from range 0-255 to 0-1

return arrayWithPixelWeight

# Convert image to numbers and print them

image_decoded_png = tf.image.decode_png(image_file,channels=3)

image_as_float32 = tf.cast(image_decoded_png, tf.float32)

numpy.set_printoptions(threshold=numpy.nan)

sess = tf.Session()

squeezedArray = sess.run(image_as_float32)

convertedList = convertRgbToWeight(squeezedArray)

print(convertedList) # This will give me an array of numbers.