7

In MATLAB, how do you write a matrix into an image of EPS format?

It seems imwrite does not support EPS.

Convert is not working on the Linux server I am using:

$ convert exploss_stumps.jpg exploss_stumps.eps
convert: missing an image filename `exploss_stumps.eps' @ convert.c/ConvertImageCommand/2838

Why?


I tried gnovice's idea under terminal mode:

    figH = figure('visible','off') ;
imshow(img,'border','tight',...      %# Display in a figure window without
        'InitialMagnification',100);  %#    a border at full magnification
print(strcat(filepath,'/', dataset,'_feature_',num2str(j), '.eps'),'-depsc2');
    close(figH) ;

However I got:

??? Error using ==> imshow at 191
IMSHOW requires Java to run.

Error in ==> study_weaker at 122
imshow(img,'border','tight',... %# Display in a figure window without

191 error(eid,'%s requires Java to run.',upper(mfilename));

How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim
  • 1
  • 141
  • 372
  • 590

3 Answers3

7

One possible solution is to plot your image using IMSHOW, then print the entire figure as a .eps using PRINT:

img = imread('peppers.png');         %# A sample image
imshow(img,'Border','tight',...      %# Display in a figure window without
       'InitialMagnification',100);  %#    a border at full magnification
print('new_image.eps','-deps');      %# Print the figure as a B&W eps

One drawback to this solution is that if the image is too big to fit on the screen, IMSHOW will shrink it to fit, which will reduce the on-screen resolution of the image. However, you can adjust the final resolution of the saved image using the -r<number> option for the PRINT function. For example, you can print your figure as an Encapsulated Level 2 Color PostScript with a resolution of 300 dpi by doing the following:

print('new_image.eps','-depsc2','-r300');

EDIT: If you are unable to use IMSHOW (either because you don't have the Image Processing Toolbox or because you are using a MATLAB mode that doesn't allow it), here is an alternative way to create and print the figure:

img = imread('peppers.png');      %# A sample image
imagesc(img);                     %# Plot the image
set(gca,'Units','normalized',...  %# Set some axes properties
        'Position',[0 0 1 1],...
        'Visible','off');
set(gcf,'Units','pixels',...      %# Set some figure properties
        'Position',[100 100 size(img,2) size(img,1)]);
print(gcf,'new_image.eps','-depsc2','-r300');  %# Print the figure

You can also take a look at this documentation to see how printing works without a display.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 1
    To avoid resolution change you probably can get matrix size and specify axis size in pixels. – yuk Mar 19 '10 at 16:15
  • @AB: That option is up to the OP, based on what type of .eps file they want/need. – gnovice Mar 19 '10 at 16:26
  • Thanks! What's the difference between -depsc2 and -deps? Is the former smaller and less accurate? How is yuk's method like to keep the resolution? – Tim Mar 19 '10 at 16:30
  • @Tim: The extra `c` creates a color image, as opposed to black and white. The `2` creates a Level 2 eps which is a more recent format that usually creates smaller files than Level 1. The suggestion from yuk involves adjusting the axes and figure sizes yourself instead of letting IMSHOW do it. This *may* allow you to make figures larger than the screen, but my 1-monitor setup doesn't let me go larger than the screen size. – gnovice Mar 19 '10 at 16:49
  • Nope, not working, sorry. Go with resolution as in the answer. – yuk Mar 19 '10 at 16:50
  • @gnovice: Thank you! I would like to work in terminal mode with figure('visible', 'off'), however imshow reports error. Can it work in terminal mode? See my update to original post. – Tim Mar 19 '10 at 18:03
  • @Tim: I updated my answer with a solution that doesn't require IMSHOW. – gnovice Mar 19 '10 at 18:08
  • THanks! Will -r300 in print change the size of the image compared to the original one? Will -r0 work? – Tim Mar 22 '10 at 00:42
  • @Tim: `-r0` should use whatever the screen resolution is. For more info and the default dpi values, you can check these 2 links: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/print.html#brneyqn, http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f3-84337.html#f3-99727 – gnovice Mar 22 '10 at 03:40
0

It should work using imwrite. You would have to add a colormap for it to work though.

However, ckecking the help pages I see that it is NOT possible to use imwrite to write an EPS file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
martiert
  • 1,636
  • 2
  • 18
  • 23
0

Following code may help you to convert png file to eps.

fileName = 'FarmerStats'; % your FILE NAME as string

A = imread(fileName,'png');
set(gcf,'visible','off') %suppress figure
image(A);                
axis image               % resolution based on image
axis off                 % avoid printing axis 
set(gca,'LooseInset',get(gca,'TightInset')); % removing extra white space in figure
saveas(gcf,fileName,'epsc');   % save as COLOR eps file