Python decode_jpeg()返回零矩阵

Python decode_jpeg()返回零矩阵,python,image,tensorflow,Python,Image,Tensorflow,我正在使用一个脚本将图像加载到TensorFlow,这显然对每个人都有效,但当我尝试它时,我最终得到了黑色图像(零矩阵)。我尝试了几个图像文件,它总是零,当我故意拼错图像位置字符串时,它会报告一个错误(应该是这样的)。返回图像张量的大小正确(256256,3)。这是脚本,有人看到错误了吗 file_names = ['/home/marko/Data/train_27.jpg'] filename_queue = tf.train.string_input_producer(file_names

我正在使用一个脚本将图像加载到TensorFlow,这显然对每个人都有效,但当我尝试它时,我最终得到了黑色图像(零矩阵)。我尝试了几个图像文件,它总是零,当我故意拼错图像位置字符串时,它会报告一个错误(应该是这样的)。返回图像张量的大小正确
(256256,3)
。这是脚本,有人看到错误了吗

file_names = ['/home/marko/Data/train_27.jpg']
filename_queue = tf.train.string_input_producer(file_names)

image_reader = tf.WholeFileReader()
title, image_file = image_reader.read(filename_queue)

image = tf.image.decode_jpeg(image_file,channels=3)

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    image_tensor = sess.run(image)
    print(image_tensor)
    print(image_tensor.shape)

    coord.request_stop()
    coord.join(threads)
你的代码是正确的。 我也有同样的问题,从图像

Tensorflow似乎无法正确检测这些文件的颜色空间,或者图像中编码的颜色空间信息不正确

Tensorflow似乎不允许在解码图像时强制使用颜色空间。因此,最简单的方法可能是“修复”图像

我使用了ImageMagic工具箱中的“转换”实用程序:

    ls train-jpg/ | \
        parallel convert -colorspace sRGB train-jpg/{} fixed-train-jpg/{}

我有另一个解决方案,它不需要按照Evgeny的建议通过命令行进行转换,而是使用Pizz加载图像:

import numpy as np
from PIL import Image

def load_image(img_path, resize=[256, 256]):

    pil_img = Image.open(img_path).convert("RGB")
    img = np.asarray(pil_img) / 255
    img_tensor = tf.convert_to_tensor(img)
    img_final = tf.image.resize(img_tensor, resize)
    return img_final

同样的代码也适用于我。使用以下命令显示图像。从PIL导入图像;img=Image.fromarray(Image_张量,'RGB');show()我确实做了,但因为它不重要,所以没有包括在这里。输出只是黑色图像。