【发布时间】:2019-09-28 10:03:04
【问题描述】:
你好,
我正在为我的高中期末项目制作一个 python3 应用程序,但遇到了一些麻烦。如果我想在我的应用程序中显示任何图像,我必须将它们放在指定的目录中,我想做的就是从每个图像中获取 base64 字符串,将其放入我的代码中,然后从这些字符串中加载图像.这可以使我的应用程序可移植,而无需复制任何其他文件。
我为此创建了几个函数,但没有人工作
import base64
from PIL import Image
from io import BytesIO
b64imgData = "iVBORw0KGgoAAAANSUhEUgAAA3QAAABMCAYAAAAlUfXmAAAABGdBTUEAALGPC/..."
decodedImgData = base64.b64decode(imgData)
bio = BytesIO(decodedImgData)
img = Image.open(bio)
这是用来查看图像的:
wx.StaticBitmap(panel, -1, img, (50, yHalf/14+20), (xHalf - 100, yHalf/8))
当我运行代码时,我得到了这个:
Traceback (most recent call last):
File "C:\Users\dummy\Desktop\PWG\main.py", line 68, in OnInit
frame = Menu()
File "C:\Users\dummy\Desktop\PWG\main.py", line 127, in __init__
wx.StaticBitmap(panel, -1, img, (50, yHalf/14+20), (xHalf - 100, yHalf/8))
TypeError: StaticBitmap(): arguments did not match any overloaded call:
overload 1: too many arguments
overload 2: argument 3 has unexpected type 'PngImageFile'
OnInit returned false, exiting...
我的下一次尝试是:
#I used this function from another thread which looks that may work
def PIL2wx (image):
width, height = image.size
return wx.BitmapFromBuffer(width, height, image.tobytes())
import base64
from PIL import Image
from io import BytesIO
b64imgData = "iVBORw0KGgoAAAANSUhEUgAAA3QAAABMCAYAAAAlUfXmAAAABGdBTUEAALGPC/..."
decodedImgData = base64.b64decode(imgData)
bio = BytesIO(decodedImgData)
img = Image.open(bio)
finalImage = PIL2wx(img)
wx.StaticBitmap(panel, -1, finalImage, (50, yHalf/14+20), (xHalf - 100, yHalf/8))
但如果我调用该函数,它会显示图像非常模糊,并且只能以黑白方式显示
非常感谢每一个回答
【问题讨论】:
标签: python-3.x base64 wxpython