为什么此代码不接受此 png 图像透明度?

发布于 2024-09-10 03:03:24 字数 1980 浏览 0 评论 0原文

我已将问题的原因隔离为图像,因为该代码似乎可以与其他具有透明度的 png 图像一起使用。然而,它似乎不适用于我需要的一张图像。当我试图制作一个形状漂亮的窗户时,这将很有帮助。

图片:

“替代文本”

代码:

import wx

class PictureWindow(wx.Frame):
    def __init__(self, parent, id, title, pic_location):

        # For PNGs. Must be PNG-8 for transparency...
        self.bmp = wx.Image(pic_location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()

        framesize = (self.bmp.GetWidth(), self.bmp.GetHeight())

        # Launch a frame the size of our image. Note the position and style stuff...
        # (Set pos to (-1, -1) to let the OS place it.
        # This style wx.FRAME_SHAPED is a frameless plain window.
        wx.Frame.__init__(self, parent, id, title, size=framesize, pos = (50, 50), style = wx.FRAME_SHAPED)

        r = wx.RegionFromBitmap(self.bmp)
        self.SetShape(r)

        # Define the panel and place the pic
        panel = wx.Panel(self, -1)
        self.mainPic = wx.StaticBitmap(panel, -1, self.bmp)

        # Set an icon for the window if we'd like
        #icon1 = wx.Icon("icon.ico", wx.BITMAP_TYPE_ICO)
        #self.SetIcon(icon1)

        self.Show()

        # The paint stuff is only necessary if doing a shaped window
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Main()


    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0, True)

    def Main(self):
        sizer = wx.GridBagSizer()
        button = wx.Button(self,-1,label="Click me !")
        sizer.Add(button, (0,1))


# What pic are we opening?
pic_location = r"C:\Users\user\Pictures\CPUBAR\A4.png" # PNG must be png-8 for             transparency...

app = wx.App(redirect=0) # the redirect parameter keeps stdout from opening a wx gui window
PictureWindow(None, -1, 'Picture Viewer', pic_location)
app.MainLoop()

顺便说一句,这是在 Windows 7 中。

I've isolated the cause of the problem to be the image, since the code seems to work with other png images with transparency. However, it doesn't seem to work with the one image I need it to. This would be of great help seeing as I'm trying to make a nice shaped window.

The image:

alt text

The code:

import wx

class PictureWindow(wx.Frame):
    def __init__(self, parent, id, title, pic_location):

        # For PNGs. Must be PNG-8 for transparency...
        self.bmp = wx.Image(pic_location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()

        framesize = (self.bmp.GetWidth(), self.bmp.GetHeight())

        # Launch a frame the size of our image. Note the position and style stuff...
        # (Set pos to (-1, -1) to let the OS place it.
        # This style wx.FRAME_SHAPED is a frameless plain window.
        wx.Frame.__init__(self, parent, id, title, size=framesize, pos = (50, 50), style = wx.FRAME_SHAPED)

        r = wx.RegionFromBitmap(self.bmp)
        self.SetShape(r)

        # Define the panel and place the pic
        panel = wx.Panel(self, -1)
        self.mainPic = wx.StaticBitmap(panel, -1, self.bmp)

        # Set an icon for the window if we'd like
        #icon1 = wx.Icon("icon.ico", wx.BITMAP_TYPE_ICO)
        #self.SetIcon(icon1)

        self.Show()

        # The paint stuff is only necessary if doing a shaped window
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Main()


    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0, True)

    def Main(self):
        sizer = wx.GridBagSizer()
        button = wx.Button(self,-1,label="Click me !")
        sizer.Add(button, (0,1))


# What pic are we opening?
pic_location = r"C:\Users\user\Pictures\CPUBAR\A4.png" # PNG must be png-8 for             transparency...

app = wx.App(redirect=0) # the redirect parameter keeps stdout from opening a wx gui window
PictureWindow(None, -1, 'Picture Viewer', pic_location)
app.MainLoop()

This is in Windows 7, btw.

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

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

发布评论

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

评论(4

<逆流佳人身旁 2024-09-17 03:03:24

wx.RegionFromBitmap 使用位图的掩码来设置形状。如果您的源图像具有 Alpha 通道,那么它将没有遮罩,因此 wx.RegionFromBitmap 将无法确定要使用的形状。您可以转换源图像,使所有像素完全不透明或完全透明,然后 wx.Image 将使用遮罩而不是 Alpha 通道加载它。或者你可以在运行时使用wx.Image的ConvertAlphaToMask方法来转换它,然后再将其转换为wx.Bitmap。

wx.RegionFromBitmap uses the bitmap's mask to set the shape. If your source image has an alpha channel then it won't have a mask and so wx.RegionFromBitmap will not be able to determine what shape to use. You can convert the source image such that all pixels are either fully opaque or fully transparent, and then wx.Image will load it with a mask instead of an alpha channel. Or you can convert it at runtime using wx.Image's ConvertAlphaToMask method before converting it to a wx.Bitmap.

旧城烟雨 2024-09-17 03:03:24

您的代码表示它需要采用 png-8 格式才能发挥透明度。
首先,图片是png-8格式的吗?
其次,为什么这是透明度的必要条件???

Your code says that it needs to be in png-8 format in order for transparency to work.
First of all, is the image in png-8 format?
second, why is this a requisite for transparency???

千笙结 2024-09-17 03:03:24

您正在将图像转换为位图 - 位图不支持透明度。

You're converting your image to a bitmap - bitmaps do not support transparency.

还如梦归 2024-09-17 03:03:24

作为解决方法,您可以使用 .gif(如果您可以忍受有限的颜色集)。
它们仅支持 1 位 Alpha 通道。

As a workaround you could use a .gif (if you can stand the limited color set).
They only support a 1-bit alpha channel.

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