Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting with alpha/transparency in a PDF #108

Closed
jdonley opened this issue Sep 25, 2015 · 10 comments
Closed

Exporting with alpha/transparency in a PDF #108

jdonley opened this issue Sep 25, 2015 · 10 comments

Comments

@jdonley
Copy link
Contributor

jdonley commented Sep 25, 2015

I am losing transparency in a PDF file for areas that have alpha. MATLAB exports the PDF with the vector components displayed correctly with transparency but export_fig does not.
This sounds similar to issue #3 but I am pretty sure MATLAB isn't embedding a rastered version in the PDF (I can zoom all the way in and the shaded areas are still sharp).
export_fig
matlab

@altmany
Copy link
Owner

altmany commented Sep 25, 2015

What are the two commands that you're using (export_fig and print)? in other words, which parameters are you using? Also, which platform and Matlab release?

@jdonley
Copy link
Contributor Author

jdonley commented Sep 25, 2015

MATLAB R2015a 64bit
print('-dpdf', 'blah_matlab.pdf');
export_fig('blah.pdf');

Also doesn't work with '-transparent' flag, but that is expected according to the docs for export_fig

@jdonley
Copy link
Contributor Author

jdonley commented Sep 25, 2015

Oh, I see... export_fig saves in EPS then converts back to PDF.
EPS doesn't support transparency but can we maintain the transparency though the conversion using pdfmark?

@altmany
Copy link
Owner

altmany commented Sep 26, 2015

I am not familiar with pdfmark, but if you can modify eps2pdf.m in a way that would fix the problem then I would be happy to incorporate your fix in the main version.

@jdonley
Copy link
Contributor Author

jdonley commented Sep 27, 2015

I am working on a workaround for this.
I need to add the line:
{alpha} .setopacityalpha true
to the eps file with the appropriate alpha value in replacement of {alpha} whenever there is a transparent object.
Any ideas on how I can locate only the elements which had transparency in the EPS file?

@altmany
Copy link
Owner

altmany commented Sep 27, 2015

I think it would be best to create a super-simple white figure that only has a single simple semi-transparent alpha-patch, and then generate the EPS export using the following command:

print(gcf, '-painters', '-loose', '-depsc2', 'test.eps')

This would make the EPS text file much smaller and you should be able to easily identify the alpha patch near the bottom of the file. EPS files are simply text files that contain PostScript directives, so you can easily experiment with your solution by simply editing the generated EPS file in any text editor (e.g., the Matlab editor) and then re-displaying the image file to see if the change helped.

Once you figure out how to post-process the EPS manually, this can be automated. You can look at print2eps.m (below line #233) for some EPS post-processing examples. The basic idea is to use the regexprep Matlab function to replace an existing EPS (=PostScript) directive with a corrected one.

Let me know if you still need my additional help with this.

@jdonley
Copy link
Contributor Author

jdonley commented Sep 27, 2015

I have already done pretty much exactly what you've suggested haha.
What I mentioned before is exactly what needs to be done and it works (i have tested).
I just can't isolate the patches in the EPS that will require transparency, that is the tricky bit.
The information is lost when we go to EPS. I need to know which patch objects in the EPS originally had an alpha component in the matlab figure.

This works for making all patches transparent:
fstrm = strrep(fstrm, sprintf('L\ncp\nf\nGR'), sprintf('L\ncp\n.5 .setopacityalpha true\nf\nGR'));

@altmany
Copy link
Owner

altmany commented Sep 27, 2015

Suggestion: take a look at the color values. If you don't see anything obvious, you could possibly modify them as a signal. For example:

  1. save the original colors of all alpha objects in the fig
  2. then modify all alpha patches to have a color of [101,102,id]/255 where id is a numeric sequence id (1 for the 1st patch, 2 for the 2nd etc.). This assumes that a [101,102,*] color is not used by any handle in the figure; otherwise, you can dynamically select any different static RG-duo that is not in use.
  3. then generate the EPS using print
  4. then post-process the EPS file and replace all patches that have an RGB of [101,102,id] with the real RGB value for each patch id, and its appropriate alpha directive
  5. finally, remember to restore the fig's patches back to their original RGBs

@jdonley
Copy link
Contributor Author

jdonley commented Sep 27, 2015

This shows the fix (#111) (notice the thin lines from export_fig compared to the thick lines from MATLAB (yet still the thinnest MATLAB can do) )
export_fig_fixed
matlab

Fix may get stuck on rare cases where ID color values are the exact same as other patches but should be good for most of the time.

@altmany
Copy link
Owner

altmany commented Sep 28, 2015

Fixed via merge of pull #111

@altmany altmany closed this as completed Sep 28, 2015
moffat pushed a commit to moffat/export_fig that referenced this issue Sep 30, 2015
moffat pushed a commit to moffat/export_fig that referenced this issue Sep 30, 2015
dcherian added a commit to dcherian/export_fig that referenced this issue Dec 2, 2015
* upstream/master: (37 commits)
  Fixed clipboard export in R2015b (thanks to Dan K via FEX)
  Update README URLs based on HTTP redirects
  Custom GS installation webpage for MacOS. Thanks to Andy Hueni via FEX
  bug fix
  Typo fix: earlier commit was for issue altmany#112, not altmany#110
  Fixed issue altmany#110: use same renderer in print2eps as export_fig (thanks to Jesús Pestana Puerta)
  Suggest a workaround for issue altmany#41 (missing font path; thanks Mariia Fedotenkova)
  Do not suggest workarounds for certain errors that have already been handled previously
  typo fix; added credit comment
  Moved transparency/alpha fix to print2eps.m Changed to always maintain alpha (removed `-transparent` flag dependency) Check whether `using_hg2` is true and only apply for HG2
  Implemented a fix to maintain transparency (alpha) in PDF files (issue altmany#108) Requires use of '-transparent' argument
  Alert if trying to export transparent patches/areas to non-PNG outputs (issue altmany#108)
  Fixed issue altmany#103: magnification must never become negative; also fixed reported error msg in parsing input params
  Fixed issue altmany#91 (thanks to Carlos Moffat)
  Fixed problem with anti-aliasing on old Matlab releases
  Fixed issue altmany#83: use numeric handles in HG1
  Added -nofontswap option to avoid font-swapping in EPS/PDF
  Added extra debug info in case of ghostscript error; code indentation
  Added -update option to download and install the latest version of export_fig
  Suppress warning in R2015b; cache result for improved performance
  ...
dcherian added a commit to dcherian/export_fig that referenced this issue Mar 10, 2016
* upstream/master:
  Better support + backward compatibility for transparency (issue altmany#108)
  Bug fix from latest release of the eps2pdf.m file (workaround for issue altmany#41)
  Added -c option for specifying crop amounts separately for each of the 4 image sides
altmany added a commit that referenced this issue Apr 13, 2023
…ssues #97, #318); clarified warning in case of PDF/EPS export of transparent patches (issues #94, #106, #108)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants