gpt4 book ai didi

python - 尝试将像素图另存为 png 文件时出现 ValueError

转载 作者:太空狗 更新时间:2023-10-29 18:22:06 25 4
gpt4 key购买 nike

如何将像素图保存为 .png 文件?
我这样做:

    image = gtk.Image()
image.set_from_pixmap(disp.pixmap, disp.mask)
pixbf=image.get_pixbuf()
pixbf.save('path.png')

我收到这个错误:

    pixbf=image.get_pixbuf()
ValueError: image should be a GdkPixbuf or empty

最佳答案

来自documentation ,

The get_pixbuf() method gets the gtk.gdk.Pixbuf being displayed by the gtk.Image. The return value may be None if no image data is set. If the storage type of the image is not either gtk.IMAGE_EMPTY or gtk.IMAGE_PIXBUF the ValueError exception will be raised.

(强调我的)

因为您需要一个png 文件,您可以按照here 中的说明进行操作。

pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,has_alpha=False, bits_per_sample=8, width=width, height=height)
pixbuf.get_from_drawable(disp.pixmap, disp.pixmap.get_colormap(), 0, 0, 0, 0, width, height)
pixbuf.save('path.png')

这将从您的 pixmap 创建一个 pixbuf,它是 disp.pixmap。这可以稍后使用 pixbuf.save

保存

关于python - 尝试将像素图另存为 png 文件时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29058763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com