【发布时间】:2017-11-14 20:13:03
【问题描述】:
我使用 PIL.Image.open 和 tf.image.decode_jpeg 将图像文件解析为数组。 但是发现PIL.Image.open()中的像素值和tf.image.decode_jpeg不一样。 为什么会这样?
谢谢!
代码输出:
tf 100 100 [132 145 161]
pil 100 100 [134 147 164]
我的代码:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from datetime import datetime
import math
import time
import numpy as np
import tensorflow as tf
def decode_jpeg(image_file):
from PIL import Image
im = Image.open(image_file)
data = np.array(im)
return data
def tfimageread(filenames):
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader(name='image_reader')
key, value = reader.read(filename_queue)
uint8image = tf.image.decode_jpeg(value, channels=3)
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = []
for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
threads.extend(qr.create_threads(sess, coord=coord, daemon=True, start=True))
image = sess.run(uint8image)
coord.request_stop()
coord.join(threads, stop_grace_period_secs=10)
return image
if __name__ == '__main__':
image_file = '。/o_1bchv9keu625336862107241874241888.jpg'
image_tf = tfimageread([image_file])
image_pil = decode_jpeg(image_file)
i, j = 100, 100
print ("tf %d %d %s" % (i,j,image_tf[i][j]))
print ("pil %d %d %s" % (i,j,image_pil[i][j]))
【问题讨论】:
-
一个疯狂的猜测 - 你确定两个模块使用相同的坐标系吗?
-
谢谢,是的,相同的坐标。 @Błotosmętek
-
可能在 PIL、tf 和 opencv 中使用 jpeg 图像的不同解压算法? Same question | Opencv issue
-
可能与OpenCV and Pillow 的问题相同。您需要确保 PIL/Pillow 使用与 TensorFlow 相同版本的 libjpeg。
标签: python image tensorflow python-imaging-library