【问题标题】:Save matplotlib image throws dimension error保存 matplotlib 图像会引发尺寸错误
【发布时间】:2020-12-25 07:06:09
【问题描述】:

首先:我是新手,请原谅我的无知。

我尝试从TFRecord-File 加载图像,并且可以使用plt.show() 显示它们,但是当我尝试使用plt.imsave() 保存图像时出现错误。

这是我尝试做的:

import tensorflow as tf
import matplotlib.pyplot as plt

reader = tf.data.TFRecordDataset(input_file)

for raw_record in reader.take(1):
    example = tf.train.Example()
    example.ParseFromString(raw_record.numpy())
    raw_record = example.features.feature['image/encoded']
    img = example.features.feature["image/encoded"].bytes_list.value[0]
    decoded = tf.io.decode_jpeg(img)

    plt.figure(figsize = (20,3))
    plt.imshow(decoded)
    plt.show()
    plt.imsave(output_file, decoded)

错误如下:

Traceback (most recent call last):
  File "/home/freddy/PycharmProjects/ocr/visualize_fsns.py", line 30, in <module>
    plt.imsave(flags.output_file, decoded)
  File "/home/freddy/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2235, in        imsave
    return matplotlib.image.imsave(fname, arr, **kwargs)
  File "/home/freddy/.local/lib/python3.8/site-packages/matplotlib/image.py", line 1567, in imsave
    rgba = sm.to_rgba(arr, bytes=True)
  File "/home/freddy/.local/lib/python3.8/site-packages/matplotlib/cm.py", line 305, in to_rgba
    xx = np.empty(shape=(m, n, 4), dtype=x.dtype)
TypeError: data type not understood

你能帮我解开这个谜吗?

【问题讨论】:

  • 你可以试试xx = np.empty((m, n, 4))

标签: python matplotlib image-processing save typeerror


【解决方案1】:

matplotlib 可能会感到困惑,因为tf.io.decode_jpeg() 返回一个张量;这可以解释数据类型错误消息。尝试在绘图前将decoded 转换为带有.numpy() 的numpy 数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多