【问题标题】:How to use BytesIO with matplotlib and pyqt5?如何将 BytesIO 与 matplotlib 和 pyqt5 一起使用?
【发布时间】:2017-09-14 22:30:23
【问题描述】:

我在 matplotlib 中制作了一个图表,并希望将其制作成一个图像并在我的 pyqt5 应用程序中使用它。有人建议我为此使用 BytesIO。到目前为止,这是我的代码:

绘制我的图表:

...
plt.axis('equal')
buff = io.BytesIO()
plt.savefig(buff, format="png")
print(buff)
return buff

然后在另一个脚本中调用它:

def minionRatioGraphSetup(self, recentMinionRatioAvg):
    image = minionRatioGraph(recentMinionRatioAvg)
    label = QtWidgets.QLabel()
    pixmap = QtGui.QPixmap(image)
    label.setPixmap(pixmap)
    label.setGeometry(QtCore.QRect(0,0,200,200))

它在pixmap = QtGui.QPixmap(image) 停止工作,我不确定为什么。另外:我怎么能把它放在我的主窗口中?因为我怀疑那里的代码会起作用哈哈

【问题讨论】:

    标签: python matplotlib io pyqt5 bytesio


    【解决方案1】:

    我确信有使用缓冲区的解决方案。但是,正确获取字节格式似乎相当复杂。因此,另一种方法是将图像保存到磁盘,然后从那里加载。

    import sys
    from PyQt4 import QtGui
    import matplotlib.pyplot as plt
    import numpy as np
    
    def minionRatioGraph():
        plt.plot([1,3,2])
        plt.savefig(__file__+".png", format="png")
    
    
    class App(QtGui.QWidget):
    
        def __init__(self):
            super(App, self).__init__()
            self.setGeometry(300, 300, 250, 150)
            self.setLayout(QtGui.QVBoxLayout())
            label = QtGui.QLabel()
            label2 = QtGui.QLabel("Some other text label") 
    
            minionRatioGraph()
    
            qimg = QtGui.QImage(__file__+".png")  
            pixmap = QtGui.QPixmap(qimg)
    
            label.setPixmap(pixmap)
            self.layout().addWidget(label)
            self.layout().addWidget(label2)
            self.show()
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication([])
        ex = App()
        sys.exit(app.exec_())
    

    【讨论】:

    • 谢谢你,你是救生员。我一直在为此拉头发哈哈
    【解决方案2】:

    使用枕头的 sn-p,可能有助于避免文件 io

     im = PIL.Image.open("filename")
     with BytesIO() as f:
         im.save(f, format='png')
         f.seek(0)
         image_data = f.read()
         qimg = QImage.fromData(image_data)
         patch_qt = QPixmap.fromImage(qimg)
    

    【讨论】:

      【解决方案3】:

      这里有一个只使用缓冲区的解决方案,你需要使用PIL创建一个ImageQT然后加载到QPixap中

      import matplotlib.pyplot as plt
      import io
      from PIL.ImageQt import ImageQt
      from PIL import Image
      
      ...
      
      buff = io.BytesIO()
      plt.savefig(buff, format="png")
      img = Image.open(buff)
      img_qt = ImageQt(img)
      return img_qt
      
      

      然后,在您的 GUI 设置中,调用您的函数以返回 ImageQT 并使用 QPixmap.fromImage() 生成 QPixmap

      def minionRatioGraphSetup(self, recentMinionRatioAvg):
          image = minionRatioGraph(recentMinionRatioAvg)
          label = QtWidgets.QLabel()
          pixmap = QtGui.QPixmap.fromImage(image)
          label.setPixmap(pixmap)
          label.setGeometry(QtCore.QRect(0,0,200,200))
      
      

      【讨论】:

        猜你喜欢
        • 2017-04-29
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 2014-10-17
        • 2022-08-06
        • 2020-11-19
        • 2014-06-30
        • 1970-01-01
        相关资源
        最近更新 更多