3

I am trying to save images in Matlab by using the print function:

myImage = magic(500);

myFigure = figure('visible','off');

r = 1;
set(myFigure, 'PaperUnits', 'inches', 'PaperPosition', [0 0 1920 1080]/r);
% the program works fine on both computers without the line above
% however, the program runs fine on one computer only with this line

imagesc(myImage); axis image;

print(myFigure, '-dpng', sprintf('-r%d',r), 'myOutput.png');

When I run this program locally with Matlab R2012b, it works as expected. However, if I try to run it on a remote machine with Matlab R2011b, I get the following error message:

Error using ghostscript (line 188)
Problem converting PostScript. System returned error: -1.Failed to convert to
output format; Ghostscript status: -100.**** Unable to open the initial device,
quitting.

and the following error which is triggered by the line with the call to print():

Error in print>LocalPrint (line 311)
        pj = ghostscript( pj );

Error in print (line 237)
    LocalPrint(pj);    

The code is more specifically:

if pj.GhostDriver
    pj = ghostscript( pj );

elseif strcmp( pj.Driver, 'hpgl' )
    hpgl( pj );
end

On my laptop, pj.GhostDriver is '' and pj.Driver is png.


One possible explanation is that I can launch ghostscript locally from the terminal:

$ ghostscript 
GPL Ghostscript 9.10 (2013-08-30)
Copyright (C) 2013 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GS>

but not remotely from the terminal (since I do not have any graphical interface on the remote computer):

$ ghostscript 
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GPL Ghostscript 9.05: Cannot open X display `(null)'.
**** Unable to open the initial device, quitting.

I would have thought Matlab already knew about it, since I launch Matlab on both computers by using:

matlab -nosplash -nodesktop -singleCompThread

Moreover, this problem is easily fixed as far as the terminal is concerned by adding the following line to ~/.bashrc:

export GS_DEVICE=display

Any hint at a solution?

Wok
  • 4,956
  • 7
  • 42
  • 64
  • most of the things you can do with `print` you can also do with `saveas` - maybe you're lucky, because saving to png should not require ghostscript. Without having a look into the functions I would guess that `print` automatically uses ghostscript for printing and converts it afterwards to png. – Robert Seifert Mar 31 '14 at 15:25
  • `-dpng` should not be using `ghostcript` according to the help of the `print` function. – Wok Mar 31 '14 at 15:27
  • Moreover, `saveas` does not work better. The culprit is the line starting with `set(myFigure, ...);` – Wok Mar 31 '14 at 15:47
  • I might have found a solution with this toolbox: http://stackoverflow.com/a/15986109/376454 – Wok Mar 31 '14 at 16:11
  • What I do is use the `-m` option to magnify the resolution. – Wok Mar 31 '14 at 16:31
  • If you found the solution, please post and elaborate or close this quesiton as a duplicate. Otherwise make sure all relevant info is edited into your question and mention what you tried of the above (including results). – Dennis Jaheruddin May 07 '14 at 14:48
  • My solution consists in using another tool, so it is not a duplicate. – Wok May 07 '14 at 16:10
  • I can't help with the ghostscript error. However, I suggest that if you want to save an image, you don't generate a figure then export that, but instead you generate the output image programmatically, then save it using imwrite. For example, if you are simply converting a matrix to a fasle color image, you could use the MATLAB file exchange submission SC. – user664303 Jul 02 '14 at 13:25

0 Answers0