打印Gnuplot到Kivy Widg

2024-10-06 11:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望实现这个MWE:

https://stackoverflow.com/a/44922317/6924364

但是对于Gnuplot,类似于:

https://stackoverflow.com/a/21633082/6924364

如何将Gnuplot输出传递到Kivy应用程序,并显示图像而不必在本地保存它?你知道吗


编辑1:这里有一个MWE,它只需要一些代码就可以将图像对象添加到自定义小部件中,但我不知道如何实现这一点。你知道吗

from subprocess import Popen, PIPE
from StringIO import StringIO                                            
from io import BytesIO
from os import linesep as nl
from PIL import Image as Image

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.core.image import Image as CoreImage


def gnuplot(commands, data):                                                    
    """ drive gnuplot, expects lists, returns stdout as string """              

    dfile = StringIO()                                                          
    for line in data:                                                           
        dfile.write(str(line) + nl)                                             

    args = ["gnuplot", "-e", (";".join([str(c) for c in commands]))]            
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)                       

    dfile.seek(0)                                                               
    return p.communicate(dfile.read())[0]   


def gnuplot_GifTest():
    commands = [\
        "set datafile separator ','",\
        "set terminal png",\
        "set output",\
        "plot '-' using 1:2 with linespoints, '' using 1:2 with linespoints",\
        ]
    data = [\
        "1,1",\
        "2,2",\
        "3,5",\
        "4,2",\
        "5,1",\
        "e",\
        "1,5",\
        "2,4",\
        "3,1",\
        "4,4",\
        "5,5",\
        "e",\
        ]

    return (commands, data)


class Plot(Widget):

    (commands, data) = gnuplot_GifTest()
    img = BytesIO(gnuplot(commands,data))
    anImg = CoreImage(img,ext="png")

    def __init__(self,**kwargs):
        super(Plot,self).__init__(**kwargs)

        # HMM, how to display self.anImg??
        print self.anImg

class MyApp(App):

    def build(self):
        box = BoxLayout(orientation="vertical")
        box.add_widget(Plot())
        return box

MyApp().run()

Tags: fromimageimportselfdatareturndefas
1条回答
网友
1楼 · 发布于 2024-10-06 11:21:07

经过研究,我找到了解决问题的办法。如果有其他解决方案的话,我会在周末把这个留给其他读者。你知道吗

from subprocess import Popen, PIPE
from StringIO import StringIO                                            
from io import BytesIO
from os import linesep as nl
from PIL import Image as Image

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.core.image import Image as CoreImage
from kivy.uix.image import Image as UIXImage

def gnuplot(commands, data):                                                    
    """ drive gnuplot, expects lists, returns stdout as string """              

    dfile = StringIO()                                                          
    for line in data:                                                           
        dfile.write(str(line) + nl)                                             

    args = ["gnuplot", "-e", (";".join([str(c) for c in commands]))]            
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)                       

    dfile.seek(0)                                                               
    return p.communicate(dfile.read())[0]   


def gnuplot_GifTest():
    commands = [\
        "set datafile separator ','",\
        "set terminal gif",\
        "set output",\
        "plot '-' using 1:2 with linespoints, '' using 1:2 with linespoints",\
        ]
    data = [\
        "1,1",\
        "2,2",\
        "3,5",\
        "4,2",\
        "5,1",\
        "e",\
        "1,5",\
        "2,4",\
        "3,1",\
        "4,4",\
        "5,5",\
        "e",\
        ]

    return (commands, data)


class Plot(UIXImage):

    def __init__(self,commands,data,**kwargs):
        super(Plot,self).__init__(**kwargs)
        gnu_img = BytesIO(gnuplot(commands,data))
        gnu_converted = CoreImage(gnu_img,ext="gif")
        self.texture = gnu_converted.texture


class MyApp(App):

    def build(self):
        (commands, data) = gnuplot_GifTest() # example for passing gnuplot commands and data to the plot widget
        box = BoxLayout(orientation="vertical")
        box.add_widget(Plot(commands,data))
        return box

MyApp().run()

相关问题 更多 >