gpt4 book ai didi

tensorflow - 在 Tensorflow 中将 RGBA 图像转换为黑白图像

转载 作者:行者123 更新时间:2023-12-01 04:27:57 24 4
gpt4 key购买 nike

tf.image.decode_png()可以输出灰度、RGB和RGBA图像。

但我想在 Tensorflow 中将 RGBA 转换为纯黑白(不使用 pillow)。

请给我一些建议。

最佳答案

使用tf.select 进行阈值处理

pil_image = PilImage.open('/temp/panda.png')
show_pil_image(pil_image)

enter image description here

pil_buf = open('/temp/panda.png').read()
contents = tf.placeholder(dtype=tf.string)
decode_op = tf.image.decode_png(contents, channels=1)
gray_image = tf.squeeze(decode_op) # shape (127,127,1) -> shape (127,127)
sess = create_session()
[decoded] = sess.run([gray_image], feed_dict={contents: pil_buf})
show_pil_image(PilImage.fromarray(decoded))

enter image description here

contents = tf.placeholder(dtype=tf.string)
decode_op = tf.image.decode_png(contents, channels=1)
gray_image = tf.squeeze(decode_op) # shape (127,127,1) -> shape (127,127)
select_op = tf.select(gray_image>127, 255*tf.ones_like(gray_image), tf.zeros_like(gray_image))
sess = create_session()
[decoded] = sess.run([select_op], feed_dict={contents: pil_buf})
show_pil_image(PilImage.fromarray(decoded))

enter image description here

关于tensorflow - 在 Tensorflow 中将 RGBA 图像转换为黑白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35836326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com