matplotlib savefig在colab中作为空白图像导出

发布于 2025-02-04 07:10:19 字数 1709 浏览 4 评论 0原文

您好,我正在使用Google Colab在图像上进行一些图像处理。

在进行上述处理并进行了一些评估之后,我想用matplotlib保存数字。

这是我的代码:


def get_images():
  frame_images = glob.glob("/content/images/*.jpg")
  for n in sorted(frame_images):
    print(n)
    images = matplotlib.pyplot.imread(n)
    yield (images)

def process():
    frames_in_sharp = 0
    sharpened_image = None
    dark_frames = 0
    capture = True
    frame_count = 0
    for (frame) in get_images():
        lower = frame.max()

        if lower < 120:
            dark_frames += 1

            if dark_frames > 18 and sharpened_image is not None:
              
                
                fig1 = matplotlib.pyplot.imshow(sharpened_image, cmap = "jet")
                matplotlib.pyplot.savefig(f"/pictures/{frame_count}.png", dpi=1000)
                matplotlib.pyplot.show()
                matplotlib.pyplot.draw()
                frames_in_sharp = 0
                sharpened_image = None
        else:
            if capture and dark_frames > 18:

                sharpened_image = numpy.asarray(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY), numpy.uint32)
                frames_in_sharp = 1


            dark_frames = 0

        if frames_in_sharp > 0:
            frames_in_sharp += 1
            plus_frame = numpy.asarray(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY), numpy.uint32)
            values_k = numpy.zeros(numpy.shape(sharpened_image), numpy.uint)
            numpy.copyto(values_k, plus_frame, where = plus_frame > 10)
            sharpened_image += values_k


       
        frame_count+=1


process()

当我运行此操作并尝试其他在线发现的其他不同组合时,我会继续写一个空白的白色文件。 matplotlib.pyplot.imshow正确显示图像。

我在做什么错?

Hello I am using Google Colab to do some image processing on images.

After doing said processing and doing some evaluations I want to save figure with matplotlib.

Here is my code:


def get_images():
  frame_images = glob.glob("/content/images/*.jpg")
  for n in sorted(frame_images):
    print(n)
    images = matplotlib.pyplot.imread(n)
    yield (images)

def process():
    frames_in_sharp = 0
    sharpened_image = None
    dark_frames = 0
    capture = True
    frame_count = 0
    for (frame) in get_images():
        lower = frame.max()

        if lower < 120:
            dark_frames += 1

            if dark_frames > 18 and sharpened_image is not None:
              
                
                fig1 = matplotlib.pyplot.imshow(sharpened_image, cmap = "jet")
                matplotlib.pyplot.savefig(f"/pictures/{frame_count}.png", dpi=1000)
                matplotlib.pyplot.show()
                matplotlib.pyplot.draw()
                frames_in_sharp = 0
                sharpened_image = None
        else:
            if capture and dark_frames > 18:

                sharpened_image = numpy.asarray(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY), numpy.uint32)
                frames_in_sharp = 1


            dark_frames = 0

        if frames_in_sharp > 0:
            frames_in_sharp += 1
            plus_frame = numpy.asarray(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY), numpy.uint32)
            values_k = numpy.zeros(numpy.shape(sharpened_image), numpy.uint)
            numpy.copyto(values_k, plus_frame, where = plus_frame > 10)
            sharpened_image += values_k


       
        frame_count+=1


process()

When I run this and try other different combinations I have found online, I keep getting a blank white file written. matplotlib.pyplot.imshow shows the image correctly.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2025-02-11 07:10:19

ola @devman3211 o/

我也是Google Colab用户:)

  • 您是否检查了图块是否尊重您的状况?

  • 如果是的,请尝试更改not sharpened_image是无 sharpened_image不是没有。

您必须尊重一定优先级,请考虑以下情况以下情况:

#first generate your plot
fig1 = plt.imshow(#whatever you want to plot)

#then save it
plt.savefig('test.png', dpi=1000)

#finally .show() or .draw() it in this order
plt.show()
plt.draw()

工作证明:

结果 for Fun ):

Ola @devman3211 o/

I'm a Google Colab user too :)

  • Have you check if the plot respect your condition?

  • If yes, try to change not sharpened_image is None for sharpened_image is not None.

You have to respect a certain order of priority, update your code considering the following:

#first generate your plot
fig1 = plt.imshow(#whatever you want to plot)

#then save it
plt.savefig('test.png', dpi=1000)

#finally .show() or .draw() it in this order
plt.show()
plt.draw()

Proof of work:
enter image description here

Result (for fun):
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文