python输入数字成数组_python – 将黑白图像转换为数字数组?

这篇博客介绍了一种方法,将PNG图像转换为数组,并根据像素的亮度将其量化到0到1的范围内,较暗像素接近1。作者首先使用TensorFlow加载和解码图像,然后定义了一个函数`convertRgbToWeight`来计算每个像素的亮度权重,最后通过会话运行TensorFlow操作并将结果转换为数组。博客提供了代码示例和输出结果。
摘要由CSDN通过智能技术生成

gC1x7.png

如上图所示,如何将图像向左转换为数组,表示图像的暗度介于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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值