- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据项目,它会在不同的目录中生成许多具有相同名称的图。查看这些图有助于了解数据是否发生了任何奇怪的情况,但是尝试将它们全部打开或将它们全部复制到一个目录,重命名它们,然后打开我的标准图像将是一件痛苦的事情查看器。
了解一些 wxPython GUI 编程后,我决定开发一个应用程序,该应用程序将接收这些图像的目录列表,并且我可以使用一些箭头按钮幻灯片显示它们。然后,随着更多绘图的出现,我可以将其添加到此列表中。
我的应用程序运行正常,但现在当我加载大约 23 ~3 MB 的图像时,我的程序内存使用量接近 10 GB。
代码如下,但简而言之,我在显示之前将它们全部加载为 wxPython 图像,这就是内存攀升的地方。通过堆栈在图像之间切换似乎不会使内存进一步攀升。我认为可能是我保留了不再需要的资源,但使用情况似乎与图像的大小和数量不成比例。
有人能指出我方法中的缺陷吗?
```
class Slide(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Slide Show")
self.display = wx.GetDisplaySize()
self.image_list = None
self.current = None # holds currently displayed bitmap
# stacks that hold the bitmaps
self.future = []
self.past = []
self.left = wx.BitmapButton(self, id=101, size=(100,-1), bitmap=wx.ArtProvider.GetBitmap(wx.ART_GO_BACK))
self.left.Enable(False)
self.right = wx.BitmapButton(self, id=102, size=(100,-1), bitmap=wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD))
self.right.Enable(False)
self.open = wx.BitmapButton(self, id=103, size=(100,-1), bitmap=wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN))
# Testing code
#png = wx.Image("set001_fit_001_1.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#image = wx.ImageFromBitmap(png)
#image = image.Scale(self.display[0]/1.4, self.display[1]/1.4, wx.IMAGE_QUALITY_HIGH)
#png = wx.BitmapFromImage(image)
# make empty image slot
self.img = wx.StaticBitmap(self, -1, size=(self.display[0]/1.4, self.display[1]/1.4))
## Main sizers
self.vertSizer = wx.BoxSizer(wx.VERTICAL)
# sub sizers
self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
# place buttons together
self.buttonSizer.Add(self.left, flag=wx.ALIGN_CENTER)
als.AddLinearSpacer(self.buttonSizer, 15)
self.buttonSizer.Add(self.right, flag=wx.ALIGN_CENTER)
# place arrows and open together
self.vertSizer.Add(self.img, flag=wx.ALIGN_CENTER)
self.vertSizer.Add(self.open, flag=wx.ALIGN_CENTER)
als.AddLinearSpacer(self.vertSizer, 15)
self.vertSizer.Add(self.buttonSizer, flag=wx.ALIGN_CENTER)
als.AddLinearSpacer(self.vertSizer, 15)
self.Bind(wx.EVT_BUTTON, self.onOpen, id=103)
self.Bind(wx.EVT_BUTTON, self.onLeft, id=101)
self.Bind(wx.EVT_BUTTON, self.onRight, id=102)
self.SetSizer(self.vertSizer)
self.vertSizer.Fit(self)
def onOpen(self, evt):
print("Opening")
openFileDialog = wx.FileDialog(self, "Open Image List", "", "", "List (*.ls)|*.ls", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if openFileDialog.ShowModal() == wx.ID_CANCEL:
return
fileName = str(openFileDialog.GetPath())
print(fileName)
# put image list in a python list
image_list = []
f = open(fileName, 'r')
for line in f:
image_list.append(line.rstrip())
f.close()
print(image_list)
# convert every image to wx Image
png_list = []
for image in image_list:
png = wx.Image(image, wx.BITMAP_TYPE_ANY)
png_list.append(png)
# store pngs
print(image_list[::-1])
png_list = png_list[::-1]
for png in png_list[:-1]:
self.future.append(png)
# display first png
self.current = png_list[-1] # put into current buffer
png = self.current.Scale(self.display[0]/1.4, self.display[1]/1.4, wx.IMAGE_QUALITY_HIGH)
png = wx.BitmapFromImage(png)
self.img.SetBitmap(png)
self.Refresh()
self.right.Enable(True)
def onLeft(self, evt):
# put current image into future stack and grab a new current image from past stack
self.future.append(self.current)
self.current = self.past.pop()
png = self.current.Scale(self.display[0]/1.4, self.display[1]/1.4, wx.IMAGE_QUALITY_HIGH)
png = wx.BitmapFromImage(png)
self.img.SetBitmap(png)
self.Refresh()
if len(self.past) <= 0:
self.left.Enable(False)
self.right.Enable(True)
def onRight(self, evt):
# put current image into past stack and load in a new image from future stack
self.past.append(self.current)
self.current = self.future.pop()
png = self.current.Scale(self.display[0]/1.4, self.display[1]/1.4, wx.IMAGE_QUALITY_HIGH)
png = wx.BitmapFromImage(png)
self.img.SetBitmap(png)
self.Refresh()
if len(self.future) <= 0:
self.right.Enable(False)
self.left.Enable(True)
```
最佳答案
根据需要加载它们可能是减少内存使用的最佳方法。或者也许将当前、下一个和上一个图像保留在内存中以帮助加快 View 之间的切换。或者可以使用缓存实现将N
最近查看的图像(加上序列中的下一个图像)保留在内存中。然后,您可以调整 N
的大小,看看什么效果最好,并且是内存和速度之间的合理折衷。
另一个可能的改进是不在每次需要显示图像时缩放图像,只需在读取图像时缩放一次并将其保留在内存(或缓存)中。您可能还想尝试将它们预先转换为 wx.Bitmaps
,因为图像到位图的转换并不是一个简单的操作。
关于python - wxPython图像占用大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40622028/
您好,我想知道是否有办法检查是否按住了某个键。 这是一个情况示例 self.button2.Bind(wx.EVT_LEFT_DOWN, self.clickedbutton) def clicked
我正在使用 wxPython 设计一个表单。请注意图片中的搜索面板:“搜索依据:”标签、嵌套在它上面的组合框和搜索控件——它们的文本都与顶部对齐。如何将它们全部对齐(垂直)在中间? 这是搜索面板代码:
我对以下代码有一个我不明白的问题。 为什么是panel1不扩大? 谢谢。 import wx class MyFrame(wx.Frame): def __init__(self, paren
我刚刚开始使用 wxPython。我有以下代码: import wx class SASFrame(wx.Frame): def __init__(self,parent,id,title):
是否可以以列表的形式获取wx.ListCtrl列中的所有值(项目)? 我在文档中看到您可以获取指定的项目,但不能获取整个列,GetValue() 也不起作用,有什么帮助吗? 最佳答案 这是一种方法:
我需要通过单击按钮在 wxpython 中创建一个额外的新窗口(与主窗口在物理上是分开的)。我想在不关闭最后一个窗口的情况下执行此操作。 这是我到目前为止所拥有的: class Prototype(w
我正在创建一个文件对话框,允许用户在我的应用程序中编辑文件后保存文件。我想在对话框中添加一个复选框,以便用户可以选择保存文件的格式。我想我需要创建一些继承自 FileDialog 的新类,并将一个复选
我正在创建一个文件对话框,允许用户在我的应用程序中编辑文件后保存文件。我想在对话框中添加一个复选框,以便用户可以选择保存文件的格式。我想我需要创建一些继承自 FileDialog 的新类,并将一个复选
我已经编写了我的第一个 wxpython 应用程序并且它运行良好,但是我在正确调整主窗口的大小方面遇到了困难。该面板包含一个 wx.BoxSizer,后者又包含一个 wx.FlexGridSizer,
在 wxpython 中使用 SetTransparent(val) 可以使整个框架透明。但是我可以让其中的单个面板透明吗? 我试过使用 panelobj.SetTransparent(val) 但没
我读了这个Style Guide for wxPython code其中说: 3. Don't use IDs. There is very rarely a good reason to use t
我的顶级窗口需要知道自定义控件的内部状态何时发生变化,以便它可以更新 GUI 的各个其他部分。如何在控件中生成自定义事件,以便它可以传播并由顶级窗口处理? 最佳答案 我知道这是一个老问题,但是在 wx
如果这是正确的词,我正在寻找一种方法来获取小部件,例如面板设备上下文。我想获取面板的位图及其所有子小部件,以便我可以在油漆 DC 中操纵它以淡入淡出面板的动画。这是可能的,如果是这样,代码是什么?我希
我在我的 RHEL6 系统上安装了 Anaconda Python。为了运行 gnuradio,我需要 wxPython。出于这个原因,我做到了 conda install wxpython 问题是当
我想要一个只接受数字的文本控件。 (只是整数值,例如 45 或 366) 最好的方法是什么? 最佳答案 我必须做类似的事情,检查字母数字代码。 EVT_CHAR 上的提示是正确的: class Tes
用于 wxPython 的最佳拖放式 GUI 生成器?我试过 wxGlade 但它不是拖放,发现它很难理解,你必须使用 sizer。我更喜欢像 VisualTinker 那样完全拖放的应用程序,但对于
问题:我在 Ubuntu 12.04 上安装 wxPython 时遇到问题。我想找到一种简单直接的方法来做到这一点。 到目前为止我做了什么:到目前为止我能找到的最相关的说明可以在这里找到:http:/
我正在尝试在 wxPython 2.8(使用 Python 2.7)中编写一个 GUI,其中涉及在 sizer 中布置几个 wx.StaticText。我想要的是一个 sizer,如果它检测到当前行的
我有一个只有一个 child 的 wx.Frame。在设置 child 的 wx.Sizer 时,我可以使用 wx.SHAPED 标志,它保持 child 的宽高比锁定。但是,Frame 在纵横比方面
我已经尝试了很多次来找到这个路径但无济于事(链接:How do I import modules in pycharm?)我认为这是因为我使用的是较新版本的 PyCharm,但这是我的项目解释器: 我
我是一名优秀的程序员,十分优秀!