MATLAB - write image into an eps file

15,459

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.

Share:
15,459
Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&amp;A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on June 12, 2022

Comments

  • Tim
    Tim almost 2 years

    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?