簡體   English   中英

保存 matplotlib 圖像引發尺寸錯誤

[英]Save matplotlib image throws dimension error

首先:我對這一切都很陌生,請原諒我的無知。

我嘗試從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

你能幫我解開這個謎嗎?

matplotlib 可能會感到困惑,因為tf.io.decode_jpeg()返回一個張量; 這可以解釋數據類型錯誤消息。 在繪圖之前嘗試使用.numpy()decoded轉換為 numpy 數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM