How to export non-blurry eps images?
Asked Answered
K

10

16

I'm exporting an image in Matlab using the eps format, but it smooths the image. Matlab does not blur the image using other formats such as png. I would like to know how to export a non-blurry image with eps format. Here is the resulting image using png: enter image description here

And here is the resulting image using eps: enter image description here

Krute answered 7/7, 2011 at 16:53 Comment(0)
B
11

UPDATE:

The problem is reproducible on a Mac, and the issue is with the eps renderer rather than MATLAB. For e.g., saving imagesc(rand(20)) and viewing with Preview and GSview results in the following:

Preview screenshot

enter image description here

GSview screenshot

enter image description here

Clearly, the information is not lost. It is just not interpreted/read correctly by some EPS viewers. The solution is simple: use GSview to view your eps images. You can download it from here

On Macs especially, if your end application is latex/pdflatex, you will have to explicitly set it to use GS/GSview, because otherwise, it will default to the Quartz engine, which is baked into the OS.


PREVIOUS ANSWER:

I am unable to reproduce the behavior your described. Here is the code I used, tested using R2010b on WinXP 32-bit:

M = fspecial('gaussian',[20 20],5);
imagesc(M)
print('-dpng','a.png')
print('-depsc2','b.eps')

a.png

a.png

b.eps

b.eps

Perhaps this is an issue with your EPS viewer...

Brunildabruning answered 7/7, 2011 at 21:46 Comment(8)
Great answer, but can you please elucidate how to explicitly set it to use GS/GSview for LaTeX? I tried googling, but didn't find a way of doing so. Thanks!Inhospitable
@SibbsGambling: I'm mostly a Windows user, the Mac part of the answer was edited by yoda at the time (who has since changed account to r.m. if I'm not mistaken)... I guess what he meant was to associate PDF and EPS files to open with GSView by default instead of the default viewer in OSX (which apparently does some sort of anti-aliasing which explains the blurry looking images). Unlike other OSX apps (Preview, Keynote, ..), GSView seems to have its own rendering engine and apparently does not use the one provided by the system (Quartz).Brunildabruning
I see. Thanks a lot. I only found the Ghostscript option in Distiller. Is it the right place? If it is the right place, then it is weird, since I have already ticked Ghostscript and unticked Apple Distiller, but the .eps is still blurry.Inhospitable
@Brunildabruning In which system did you use to open the files? I think I need to have Ubuntu Linux with GSview in VirtualBox, just for previewing .eps files in OS X El Capitan.Euphemism
@Masi actually I'm on Windows where I use any of GSView, SumatraPDF, or Evince to open PostScript files (they also handle PDF). None of them show any blurriness.. Perhaps you can install Evince on OSX (I think it's available in both homebrew and fink). btw the mac part of the answer is not me :)Brunildabruning
@Brunildabruning I profiled Evince with my test signals. It loses data in opening .eps files. Do you have more recommendations for .eps viewers?Euphemism
@Masi did you try the mac version of GSView as originally suggested (it uses ghostscript as backend)?Brunildabruning
@Brunildabruning No. They have servers down so I could not download their images. I instead set up Ubuntu Linux in VirtualBox where I use gv directly. I need to have several benchmarks such that I can evaluate each viewer's quality. I try to get some standard built-image of GSView working when they are hosted for OS X.Euphemism
N
5

not sure why it works but you can try doing the following:

eps2eps oldfile newfile

does the trick for me (on a mac os)

Nobby answered 13/11, 2013 at 14:18 Comment(2)
For me it works as well, on Mac OS X. (All other approaches failed.)Everick
What is the version of your psutils? Here report that not working with 1.23 https://mcmap.net/q/728465/-how-to-export-non-blurry-eps-imagesEuphemism
S
4

At first I thought you were doing something incorrectly, but then I remembered that this was an issue that was bothering the hell out of me a year or so ago. I couldn't come up with a way to "fix" this behaviour and from what I've researched, this is most likely a bug and several others have had this problem too and there is no known solution. Of course, I could be wrong about the last part and there might be solutions out there that have come out since I looked for them.

Any way, my workaround this problem was to use pcolor with shading flat instead of imagesc. When you export this to an eps format it preserves the image correctly. Example:

pcolor(rand(20));
shading flat
print('-depsc','figure.eps')

enter image description here

NOTE: You might see the appearance of thin, faint white lines along the anti-diagonals of each little square (depends on the OS & viewer). These are the edges of the graphics primitives that are used to render the image. However, this is not a flaw in MATLAB's export, but rather a fault in rendering in your EPS/PDF viewer. For e.g., with the default settings in Preview on my mac, these lines show up, whereas with the default in Adobe Reader 9.4, they don't appear.

Spires answered 7/7, 2011 at 17:33 Comment(1)
This works perfectly! No faint white lines appear when using LaTeX's epstopdf conversion. Thanks a lot!Tonsorial
H
2

If anyone is still interested in a workaround: Open the .eps-file with text editor and search for "interpolate". You'll probably find "/Interpolate true def" two or three times. Replace "true" with "false" and be happy :)

Hartmann answered 14/11, 2011 at 13:26 Comment(3)
Does this work? I thought pdfkit on Mac didn't respect the Interpolate option? Also, in my matlab generated EPS I don't find the interpolate line. Can you give some more details about where it should be inserted?Jiffy
I did not find any Interpolate in my epsUniverse
/Interpolate true is PDF not EPS.Uterus
E
1

A note regarding Yoda's answer: in Preview in Mac OS X, you can make the thin white diagonal lines across each of the squares disappear by unchecking "Anti-alias text and screen art". Of course, the downside is that then any text (e.g. figure axes, etc) is not anti-aliased. Unfortunately, unchecking that has no effect on blurriness if you're using imagesc.

Another note is that if you use preview to make a pdf from your eps, the resulting pdf still displays correctly (non-blurry) when you open it in Acrobat.

Erbium answered 1/8, 2011 at 23:31 Comment(1)
Very interesting notice! How can you uncheck Anti-alias text and screen art in Preview's Settings? I cannot find it in El Capitan. Do you have any picture what the white diagonal lines look like. I am very interested in seeing their appearance. I opened a new thread about your claim here apple.stackexchange.com/q/231745/15504Euphemism
S
1

I've been long struggling with this problem as well. So far, GSView is the only viewer I've found that displays the eps figures produced by Matlab (R2015b) correctly. eps2eps did not work for me (psutils 1.23). The following eventually worked for me:

  1. Export the figure to pdf, following the instructions here
  2. pdf2ps file.pdf file.eps
Semifinalist answered 4/3, 2016 at 10:38 Comment(1)
Please, do not use saveas. It is very buggy in Matlab 2016a. The command export_fig is much more robust mathworks.com/matlabcentral/fileexchange/23629-export-figEuphemism
S
0

The blurring actually depends on the rendering software your viewer application or printer uses. To get good results all the time, make each pixel in your image an 8x8 block of pixels of the same color, i.e. resize the image like this:

im2 = imresize(im1, 8, 'nearest');

The blurring then only affects the pixels at the edge of each block. 8x8 blocks are best as they compress without nasty artifacts using DCT compression (sometimes used in eps files).

Servitor answered 11/1, 2012 at 16:15 Comment(0)
C
0

Apply opengl renderer to the figure

figure(gcf);
set(gcf,'renderer','opengl');
Catalyze answered 18/10, 2013 at 1:28 Comment(2)
You should describe more why this works. Or what this all means.Batista
This only works because by changing the renderer to opengl it saves it as a raster-ish EPS rather than a true vector graphic.Meeks
S
0

I just wrote this simple drop-in replacement for imagesc. It doesn't support all but the most basic features, but I still hope it helps.

function h = imagesc4pdf(C)

[ny nx] = size(C);

px = bsxfun(@plus, [-0.5; 0.5; 0.5; -0.5], reshape(1:nx, [1 1 nx]));
py = bsxfun(@plus, [-0.5; -0.5; 0.5; 0.5], 1:ny);

n = numel(C);
px = reshape(repmat(px, [1 ny 1]), 4, n);
py = reshape(repmat(py, [1 1 nx]), 4, n);

h = patch(px, py, reshape(C,1,n), 'linestyle', 'none');

xlim([.5 nx+.5]);
ylim([.5 ny+.5]);
set(gca, 'ydir', 'reverse');
Sundog answered 4/7, 2014 at 13:31 Comment(1)
Can you please describe what your code is doing? Example output would also be great.Euphemism
D
0

This page helped me a lot: http://tech.mof-mof.co.jp/blog/machine-learning-octave.html (written in Japanese, please use google translate for it)

And this is also helpful: Octave-Gnuplot-AquaTerm error: set terminal aqua enhanced title "Figure 1"...unknown terminal type"

I also answered at https://www.coursera.org/learn/machine-learning/discussions/weeks/2/threads/Dh-aRfqSEeaHSQ6l4xnh6g.

I reinstalled gnuplot like this:

$ brew cask install xquartz
$ brew cask install aquaterm
$ brew uninstall gnuplot
$ brew install gnuplot --with-aquaterm --with-x11 --with-qt  # you can show other options by `$ brew options gnuplot`

You may edit ~/.octaverc like this:

setenv("GNUTERM", "qt")

and in octave window, after typing "system gnuplot", then

set pm3d interpolate 2, 2

After saving the file, open octave-cli.app, and type

imagesc(magic(3)), colorbar

I got this.

enter image description here

Dobbs answered 3/2, 2018 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.