Autogeneration of .png from plots

2,057 views
Skip to first unread message

Richard Smith

unread,
Sep 18, 2013, 11:02:27 AM9/18/13
to pyqt...@googlegroups.com
I'd like to automatically generate exports of the entire graphics scene from list of files.  Searching through the code it looks like the dialog method is all that is implemented.  Anybody done this already?

Also are there any plans to implement a .pdf exporter anytime soon?

Luke Campagnola

unread,
Sep 18, 2013, 11:11:35 AM9/18/13
to pyqt...@googlegroups.com
On Wed, Sep 18, 2013 at 11:02 AM, Richard Smith <smit...@gmail.com> wrote:
I'd like to automatically generate exports of the entire graphics scene from list of files.  Searching through the code it looks like the dialog method is all that is implemented.  Anybody done this already?


Also are there any plans to implement a .pdf exporter anytime soon?

Not at present--I have not been able to generate acceptable results with Qt's printing functionality. 
I recommend using inkscape to generate PDF from SVG (this can be automated using inkscape's --export-pdf flag).


Luke

Richard Smith

unread,
Sep 18, 2013, 12:20:43 PM9/18/13
to pyqt...@googlegroups.com


On Wednesday, September 18, 2013 11:11:35 AM UTC-4, Luke Campagnola wrote:
On Wed, Sep 18, 2013 at 11:02 AM, Richard Smith <smit...@gmail.com> wrote:
I'd like to automatically generate exports of the entire graphics scene from list of files.  Searching through the code it looks like the dialog method is all that is implemented.  Anybody done this already?

Does this answer your question?

Thanks for the quick response.

Not quite because I always get object has no attribute 'plotItem'

Here's my code.  Following the example in examples/Plotting.py I've done:

win = pg.GraphicsWindow()
p1 = win.addPlot(title="Data")
win.nextRow()
p2 = win.addPlot(title="Filter Subtraction")

Then I call p1.plot and p2.plot to make the plots.

Exporter isn't happy with either win.plotItem or p1.plotItem 
 
Also are there any plans to implement a .pdf exporter anytime soon?

Not at present--I have not been able to generate acceptable results with Qt's printing functionality. 
I recommend using inkscape to generate PDF from SVG (this can be automated using inkscape's --export-pdf flag).

That's odd since I print to .pdf using kde all the time and it seems to be ok.  What sort of things are wrong in the output? 

Luke Campagnola

unread,
Sep 18, 2013, 12:49:37 PM9/18/13
to pyqt...@googlegroups.com
On Wed, Sep 18, 2013 at 12:20 PM, Richard Smith <smit...@gmail.com> wrote:

On Wednesday, September 18, 2013 11:11:35 AM UTC-4, Luke Campagnola wrote:
On Wed, Sep 18, 2013 at 11:02 AM, Richard Smith <smit...@gmail.com> wrote:
I'd like to automatically generate exports of the entire graphics scene from list of files.  Searching through the code it looks like the dialog method is all that is implemented.  Anybody done this already?

Does this answer your question?

Thanks for the quick response.

Not quite because I always get object has no attribute 'plotItem'

Here's my code.  Following the example in examples/Plotting.py I've done:

win = pg.GraphicsWindow()
p1 = win.addPlot(title="Data")
win.nextRow()
p2 = win.addPlot(title="Filter Subtraction")

Then I call p1.plot and p2.plot to make the plots.

GraphicsWindow may contain multiple plots / views / etc. so it does not have a plotItem attribute. 
Instead, you may pass p1 or p2 to the exporter.
 

Exporter isn't happy with either win.plotItem or p1.plotItem 
 
Also are there any plans to implement a .pdf exporter anytime soon?

Not at present--I have not been able to generate acceptable results with Qt's printing functionality. 
I recommend using inkscape to generate PDF from SVG (this can be automated using inkscape's --export-pdf flag).

That's odd since I print to .pdf using kde all the time and it seems to be ok.  What sort of things are wrong in the output? 

To be honest, I don't remember; that was quite a long time ago  :)  I seem to recall a bad interaction between QGraphicsView and QPrinter..
Might be worth taking another look.
 

Luke

Richard Smith

unread,
Sep 18, 2013, 1:00:24 PM9/18/13
to pyqt...@googlegroups.com


On Wednesday, September 18, 2013 12:49:37 PM UTC-4, Luke Campagnola wrote:
On Wed, Sep 18, 2013 at 12:20 PM, Richard Smith <smit...@gmail.com> wrote:

On Wednesday, September 18, 2013 11:11:35 AM UTC-4, Luke Campagnola wrote:
On Wed, Sep 18, 2013 at 11:02 AM, Richard Smith <smit...@gmail.com> wrote:
I'd like to automatically generate exports of the entire graphics scene from list of files.  Searching through the code it looks like the dialog method is all that is implemented.  Anybody done this already?

Does this answer your question?

Thanks for the quick response.

Not quite because I always get object has no attribute 'plotItem'

Here's my code.  Following the example in examples/Plotting.py I've done:

win = pg.GraphicsWindow()
p1 = win.addPlot(title="Data")
win.nextRow()
p2 = win.addPlot(title="Filter Subtraction")

Then I call p1.plot and p2.plot to make the plots.

GraphicsWindow may contain multiple plots / views / etc. so it does not have a plotItem attribute. 
Instead, you may pass p1 or p2 to the exporter.

Passing p1 or p2 to the exporter results in the following error.

ValueError: Buffer size too small (0 instead of at least 1 bytes)
 

Grant R

unread,
Sep 18, 2013, 2:57:55 PM9/18/13
to pyqt...@googlegroups.com
There are some examples out there of exporting to pdf.  The gets pretty close, but the line becomes much thicker.  I don't know if these were similar problems you were encountering, Luke.

import pyqtgraph as pg
from PyQt4.QtGui import QPrinter, QPainter, QApplication


def main():
    view = pg.plot([1,2,3,4], [2,3,2,3])
    
    fileName = "/tmp/test.pdf"
    printer = QPrinter(QPrinter.ScreenResolution)
    printer.setPageSize(QPrinter.Letter)
    printer.setOrientation(QPrinter.Portrait)
    printer.setOutputFormat(QPrinter.PdfFormat)
    printer.setOutputFileName(fileName)
           
    p = QPainter()
    p.begin(printer)
    view.render(p)
    p.end()

if __name__=="__main__":
    
    app = QApplication([])
    main()
    app.exec_()

Luke Campagnola

unread,
Sep 18, 2013, 3:05:35 PM9/18/13
to pyqt...@googlegroups.com
On Wed, Sep 18, 2013 at 2:57 PM, Grant R <grant...@gmail.com> wrote:
There are some examples out there of exporting to pdf.  The gets pretty close, but the line becomes much thicker.  I don't know if these were similar problems you were encountering, Luke.

Thanks! The code is actually still there under pyqtgraph/exporters/PrintExporter, and looks somewhat similar to what you posted. Line widths were a major issue with SVG export before it was given an overhaul to work around those issues.. it would not surprise me if the print export had similar issues. 

 

Grant R

unread,
Sep 18, 2013, 3:14:53 PM9/18/13
to pyqt...@googlegroups.com
If I instead set:

    pen = pg.mkPen(width=0, color='r')
    view = pg.plot([1,2,3,4], [2,3,2,3], pen=pen)

it prints fine.  Seems to be the something with the pen (cosmetic or not).

Richard Smith

unread,
Sep 18, 2013, 3:38:50 PM9/18/13
to pyqt...@googlegroups.com

GraphicsWindow may contain multiple plots / views / etc. so it does not have a plotItem attribute. 
Instead, you may pass p1 or p2 to the exporter.

Passing p1 or p2 to the exporter results in the following error.

ValueError: Buffer size too small (0 instead of at least 1 bytes)

Do you need any more info from me?  Is this a bug or just my ignorance on how to use this class directly?  I've only been using pyqtgraph for about 1 day (today will make 2) so I'm still climbing up the learning curve.  

Thanks.

Luke Campagnola

unread,
Sep 18, 2013, 3:40:14 PM9/18/13
to pyqt...@googlegroups.com
Yes; the full stack trace would be helpful in this case. 
Thanks.

Richard Smith

unread,
Sep 18, 2013, 6:50:59 PM9/18/13
to pyqt...@googlegroups.com
rsmith@thinko:~$ lowpass.py Rich_1_Volts_20130918_155632.xls 
Traceback (most recent call last):
  File "/home/rsmith/bin/lowpass.py", line 203, in <module>
    process_files(args.filenames,args)
  File "/home/rsmith/bin/lowpass.py", line 156, in process_files
    exporter.export('auto.png') 
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/exporters/ImageExporter.py", line 66, in export
    self.png = pg.makeQImage(bg, alpha=True)
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/functions.py", line 957, in makeQImage
    addr = ctypes.c_char.from_buffer(imgData, 0)

Luke Campagnola

unread,
Sep 18, 2013, 1:22:44 PM9/18/13
to pyqt...@googlegroups.com
Can you provide the full traceback for this error? 

Luke Campagnola

unread,
Sep 18, 2013, 9:38:41 PM9/18/13
to pyqt...@googlegroups.com
On Wed, Sep 18, 2013 at 6:50 PM, Richard Smith <smit...@gmail.com> wrote:

rsmith@thinko:~$ lowpass.py Rich_1_Volts_20130918_155632.xls 
Traceback (most recent call last):
  File "/home/rsmith/bin/lowpass.py", line 203, in <module>
    process_files(args.filenames,args)
  File "/home/rsmith/bin/lowpass.py", line 156, in process_files
    exporter.export('auto.png') 
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/exporters/ImageExporter.py", line 66, in export
    self.png = pg.makeQImage(bg, alpha=True)
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/functions.py", line 957, in makeQImage
    addr = ctypes.c_char.from_buffer(imgData, 0)
ValueError: Buffer size too small (0 instead of at least 1 bytes)

 
Ok, I'm stumped on this. Could you post some example code demonstrating the problem? 

Richard Smith

unread,
Sep 18, 2013, 10:11:02 PM9/18/13
to pyqt...@googlegroups.com
Sure.  Attached is chopped version + data file.  

run it with ./lowpass  datafile.txt

 
datafile.txt
image_output_error.py

Richard Smith

unread,
Sep 18, 2013, 10:12:00 PM9/18/13
to pyqt...@googlegroups.com

Sure.  Attached is chopped version + data file.  

run it with ./lowpass  datafile.txt

Oops. Sorry. I renamed it:  ./image_output_error datafile.txt
 

Luke Campagnola

unread,
Sep 18, 2013, 11:55:16 PM9/18/13
to pyqt...@googlegroups.com
Thank you! 
The problem is that you are trying to export before the window has been initialized by Qt, and thus it has width=0. You can correct this by adding `QtGui.QApplication.processEvents()` immediately before creating the exporter. I will add a more useful error message..

I was also able to export the scene object to get both plots in one image. If that is still broken for you, I'd recommend trying the development branch ( git clone g...@github.com:pyqtgraph/pyqtgraph.git develop ) or try the next release when it comes out. 

 Luke

Richard Smith

unread,
Sep 19, 2013, 12:36:26 AM9/19/13
to pyqt...@googlegroups.com


Thank you! 
The problem is that you are trying to export before the window has been initialized by Qt, and thus it has width=0. You can correct this by adding `QtGui.QApplication.processEvents()` immediately before creating the exporter. I will add a more useful error message..

I was also able to export the scene object to get both plots in one image. If that is still broken for you, I'd recommend trying the development branch ( git clone g...@github.com:pyqtgraph/pyqtgraph.git develop ) or try the next release when it comes out. 

That fixed it.  The win.scene() also works now and renders both graphs.  Huge thanks.  That would have probably stumped me for quite a while if you had not helped me.

Reply all
Reply to author
Forward
0 new messages