{"id":1273,"date":"2014-08-18T20:45:21","date_gmt":"2014-08-18T12:45:21","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=1273"},"modified":"2014-08-18T20:45:21","modified_gmt":"2014-08-18T12:45:21","slug":"matplotlib-%e5%ad%98%e5%82%a8%e5%88%b0%e5%86%85%e5%ad%98%e6%96%87%e4%bb%b6-save-to-memory-file-instead-of-disk-file-pyqt4","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1273","title":{"rendered":"matplotlib \u5b58\u50a8\u5230\u5185\u5b58\u6587\u4ef6 save to memory file(buffer) instead of disk file \/ PyQt4"},"content":{"rendered":"<p>References:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/4330812\/how-do-i-clear-a-stringio-object\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/4330812\/how-do-i-clear-a-stringio-object<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/8598673\/how-to-save-a-pylab-figure-into-in-memory-file-which-can-be-read-into-pil-image\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/8598673\/how-to-save-a-pylab-figure-into-in-memory-file-which-can-be-read-into-pil-image<\/a><br \/>\n&nbsp;<br \/>\nIn fact\u00a0Python have a <em>StringIO<\/em> module to deal with this problem, it works pretty fine with PyQt and matplotlib.<br \/>\n<strong>Procedures:<\/strong><br \/>\n1. [SAVE] Create a StringIO object (or cStringIO which have faster speed)<br \/>\n2. [SAVE] Call <em>savefig<\/em> method of matplotlib and use the StringIO object instead as the input variable<br \/>\n3. [LOAD] Use QImage to read from StringIO object (via <em>fromData<\/em> method)<br \/>\n4. [LOAD] Use QPixmap and blabla to show the image&#8230;.<br \/>\n&nbsp;<br \/>\n<strong>Code:<\/strong><br \/>\n&nbsp;<\/p>\n<pre class=\"lang:python decode:true \"># StringIO with matplotlib test\nimport matplotlib.pyplot as plt\nimport cStringIO\nfrom PyQt4 import QtCore, QtGui\nimport sys\nimport cPickle\nfig = plt.figure()\nplt.plot([1, 2])\nbuf = cStringIO.StringIO()\nplt.savefig(buf, format='png')\nbuf.seek(0)\napp = QtGui.QApplication(sys.argv)\nwidget = QtGui.QWidget()\nwidget.resize(250, 150)\nwidget.setWindowTitle('simple')\nqimg = QtGui.QImage.fromData(buf.getvalue())\npixmap = QtGui.QPixmap.fromImage(qimg)\nlabel = QtGui.QLabel(widget)\nlabel.setPixmap(pixmap)\nlabel.setGeometry(0, 0, 250, 150)\nwidget.show()\nsys.exit(app.exec_())\n<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>References: http:\/\/stackoverflow.com\/questions\/4330812\/how-do-i-clear-a-stringio-object http:\/\/stackoverflow.com\/questions\/8598673\/how-to-save-a-pylab-figure-into-in-memory-file-which-can-be-read-into-pil-image &nbsp; In fact\u00a0Python have a StringIO module to deal with this problem, it works pretty fine with PyQt and matplotlib. Procedures: 1. [SAVE] Create a StringIO object (or cStringIO which have faster speed) 2. [SAVE] Call savefig method of matplotlib and use the StringIO object instead as the input variable 3. [LOAD] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[88,89],"class_list":["post-1273","post","type-post","status-publish","format-standard","hentry","category-study","category-technical","tag-pyqt","tag-python"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1273"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1273\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}