首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >( matplotlib.pyplot )将错误保存到图片

( matplotlib.pyplot )将错误保存到图片
EN

Stack Overflow用户
提问于 2022-11-04 08:45:17
回答 1查看 47关注 0票数 0

matplotlib.pyplot将错误保存到图片

工具:在Ubuntu22.04.1LTS上使用CV2 4.6.0

问题:

我的代码可以输出plt.imshow(image) ,但不能输出 plt.imsave**save**

AttributeError:'tuple‘对象没有属性'shape’

  • .py脚本:

代码语言:javascript
运行
复制
import numpy as np
import glob
import matplotlib.pyplot as plt
import skimage.io
import skimage.color
import skimage.filters


from PIL import Image
import pytesseract
import cv2 as cv
import numpy as np

# load the image
image = skimage.io.imread("/home/joy/桌面/test_11_4/Img_after_sharpen.png")


# image = imageio.imread(image_name)[:,:,:3]
# img = rgb2gray(image)

fig, ax = plt.subplots()
plt.imshow(image)


# convert the image to grayscale
gray_image = skimage.color.rgb2gray(image)

# blur the image to denoise
blurred_image = skimage.filters.gaussian(gray_image, sigma=1.0)

fig, ax = plt.subplots()
plt.imshow(blurred_image, cmap="gray")

# create a histogram of the blurred grayscale image
histogram, bin_edges = np.histogram(blurred_image, bins=256, range=(0.0, 1.0))

fig, ax = plt.subplots()
plt.plot(bin_edges[0:-1], histogram)
plt.title("Grayscale Histogram")
plt.xlabel("grayscale value")
plt.ylabel("pixels")
plt.xlim(0, 1.0)

# create a mask based on the threshold
t1 = 0.8
t2 = 0.05
binary_mask = blurred_image < t1 

fig, ax = plt.subplots()
plt.imshow(binary_mask, cmap="gray")

aaa = plt.imshow(binary_mask, cmap="gray")

plt.show() 


plt.imsave('output_remove_gray_area.png', aaa)


img = Image.open('output_remove_gray_area.png')
text = pytesseract.image_to_string(img, lang='eng')

print("file name" ,"output_remove_gray_area", ".png")

print("size")

print(img.size)

print(text)

  • output:

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "/home/joy/桌面/test_11_4/1.py", line 57, in <module>
    plt.imsave('output_remove_gray_area.png', aaa)
  File "/home/joy/miniconda3/lib/python3.9/site-packages/matplotlib/pyplot.py", line 2118, in imsave
    return matplotlib.image.imsave(fname, arr, **kwargs)
  File "/home/joy/miniconda3/lib/python3.9/site-packages/matplotlib/image.py", line 1625, in imsave
    pil_shape = (rgba.shape[1], rgba.shape[0])
AttributeError: 'tuple' object has no attribute 'shape'

我希望保存im.show已经成功的图像

  • im.show输出

https://imgur.com/a/KONNxWY

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-07 06:43:29

使用plt.imsave("output_remove_gray_area.png", binary_mask, cmap="gray")而不是保存aaa,这是plt.imshow()的返回值,而不是实际的映像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74314299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档