Can transparency be used with PostScript/EPS?
Although the EPS format does not natively support semi-transparency, it is still possible to use cairo_ps()
, that one automatically rasterizes semi-transparent areas, and the resolution at which it does this can be controlled with the argument fallback_resolution
:
cairo_ps(file = "test.eps", onefile = FALSE, fallback_resolution = 600)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))
dev.off()
All the non-semi-transparent areas then nicely stay as vector graphics.
Or even shorter you can also use :
ggsave("filename.eps", device=cairo_ps, fallback_resolution = 600)
Or use the functions to export to eps using the new export
package, which just came out on CRAN :
install.packages("export")
library(export)
graph2eps("filename.eps", fallback_resolution = 600)
That package also supports a number of other export formats, including Powerpoint (graph2ppt
), see ?graph2vector
, which also retains semi-transparency...
Matplotlib Plots Lose Transparency When Saving as .ps/.eps
The problem is that eps
does not support transparencies natively.
There are few options:
rasterize the image and embed in a eps file (like @Molly suggests) or exporting to pdf and converting with some external tool (like gs) (which usually relies as well on rasterization)
'mimic' transparency, giving a colour that looks like the transparent one on a given background.
I discussed this for sure once on the matplotlib mailing list, and I got the suggestion to rasterize, which is not feasible as you get either pixellized or huge figures. And they don't scale very nicely when put into, e.g., a publication.
I personally use the second approach, and although not ideal, I found it good enough. I wrote a small python script that implements the algorithm from this SO post to obtain a solid RGB representation of a colour with a give transparency
EDIT
In the specific case of your plot try to use the zorder
keyword to order the parts plotted. Try to use zorder=10
for the blue ellipse, zorder=11
for the green and zorder=12
for the hexbins.
This way the blue should be below everything, then the green ellipse and finally the hexbins. And the plot should be readable also with solid colors. And if you like the shades of blue and green that you have in png, you can try to play with mimic_alpha.py
.
EDIT 2
If you are 100% sure that you have to use eps, there are a couple of workarounds that come to my mind (and that are definitely uglier than your plot):
- Just draw the ellipse borders on top of the hexbins.
- Get centre and amplitude of each hexagon, (possibly discard all zero bins) and make a scatter plot using the same colour map as in
hexbin
and adjusting the marker size and shape as you like. You might want to redraw the ellipses borders on top of that
Transparency in EPS -- does pdfmark work?
I have found that using pdfmark
for transparency does work in Adobe Distiller (which converts EPS to PDF) if you replace
/AllowTransparency false
with
/AllowTransparency true
in the .joboptions
settings file.
gnuplot postscript terminal not showing transparent symbols
I think I should respond to my own question, so at least this becomes a case closed.
After some more digging, and from the comments I received, the bottom line is that the gnuplot postscript
terminal does not handle transparency, while the pdf
and pdfcairo
terminals do.
The trick is to generate an .eps file from a .pdf using pdftops
:
#!/bin/bash
gnuplot << GNU
set term pdf
set out "out.pdf"
plot "<jot -r -p 2 500 1 2" not w p pt 7 ps 4 lc rgb "#908DB6CD"
GNU
pdftops -eps out.pdf
All of my .eps files are generated to be incorporated to LaTeX documents. Then, I could just switch to PDFLaTeX and be over with it. However, at times I like to edit the .eps to tweak the bounding box and other things by hand or using awk/sed. Anyway, hope this is helpful.
How do I preserve transparency in ggplot2?
R's eps "device" doesn't support partial transparency, but, if I remember correctly, its PDF device does. Illustrator ought to be able to read PDFs with equal facility to EPSes, or if not, try converting them after generation with pdftops
(not pdf2ps
, they are totally different programs and pdf2ps's output is ... inferior).
Note that R doesn't try to optimize its PDF output at all, so even if you do get a plot that needs no postproduction, you will want to run it through a compression utility like qpdf
at the very least.
Ghostscript - EPS (with embedded TIFF with transparent background) to PNG conversion
Your file doesn't look like its transparent, it looks like its masked, possibly with a stencil mask, possibly chroma-keyed. Without seeing the file I can't tell for sure.
You are correct that PostScript (and hence EPS) doesn't support transparency, but it does support several features which have somewhat similar effects.
The color space is irrelevant, and in fact the only kind of 'transparency' supported in PostScript works when the color space is CMYK, but not when its RGB (and certainly not sRGB, which isn't even a PostScript color space, you have to manufacture it from CIEBasedABC)
As far as I can see the command line you are using is correct, but as I say I can't tell much without seeing the actual EPS program.
[EDIT]
So the Ghostscript rendering is correct, that's what is in your EPS file, there is no transparency of any kind there. So how is Illustrator able to make a transparent PNG ? Well the answer is that Illustrator isn't using the PostScript part of the EPS file.
About 1/3 of the way through the EPS file you'll see a line which reads:
%AI9_PrivateDataBegin
What follows that is an Adobe Illustrator file format. When AI reads the file it finds that line, throws away the PostScript portion of the file, and reads the AI representation of the content from the portion of the file beginning with that comment.
Now stored somewhere in there will be the information that portions of the content are transparent. Although PostScript can't represent that, Illustrator's internal format can. So when you write a PNG file from Illustrator it knows that portion is transparent and writes it as such.
Ghostscript, however, is constrained by the PostScript portion of the file, it can't read the Illustrator native format, and so renders the image with a white background.
It 'might' be possible to save a different kind of EPS from Illustrator (level 3 instead of level 2 possibly, I notice this is a language level 2 EPS file) which duplicate the effect, but from what you have here, there isn't anything a standard PostScript interpreter can do which will give you the result you want.
PHP ImageMagick png to eps conversion with transparent background
PostScript (and therefore EPS) doesn't have a concept of transparency (with some exceptions), so you can't readily convert an image with an alpha channel to an EPS.
If the 'background' is 100% transparent it is possible to create a similar effect in PostScript using masked images (that's 2 images, where the second is a mask that determines where the 1st image draws). That's a rarely used feature, and requires a level 3 PostScript interpreter. Most EPS export functionality doesn't go above level 2.
As to whether ImageMagick can create such an EPS, I have no clue, but I wouldn't be at all surprised to find it cannot. You may have to use something like Photoshop.
Regarding the image size; PNG is a compressed format, and you have specified
-compress none
So its hardly surprising that the output is larger!
Its essentially impossible to comment on your quality problem, partly because you haven't really said exactly what it is that you see as a problem. 'Quality' is rather vague, perhaps you could post an example.
If your EPS has a thumbnail preview do not be fooled into thinking this is what the EPS contains, its a low resolution bitmap preview only and its there for the benefit of EPS consumers that can't interpret the PostScript content. So that they can preview the result when you place the EPS in the final document.
How to save an .EPS file to PNG with transparency in Python
Looks like transparency is not supported. From the docs:
The EPS driver can read EPS images in L, LAB, RGB and CMYK mode, but Ghostscript may convert the images to RGB mode rather than leaving them in the original color space.
When you load in RGB (instead of RGBA) the alpha channel information is discarded and converting it to RGBA later will not recover it.
Your best shot is porting it to more recent toolkits like cairo or QT or converting the file using GhostScript directly as suggested by PM2Ring.
For the GS approach in order to set the width and height of the output file you must use the -rN
switch where N
is the resolution in PPI (pixels per inch). You must do the math in order to get target resolution from the EPS bounding box and the desired output size.
Or you can render to a fixed resolution first, lets say, 100 PPI, see the width you got and do the math in order to get the correct resolution. For example, if rendering with -r100
gives you a file 500 pixels wide but you want it to be 1024:
desired_resolution = initial_resolution * desired_width // initial_width
In order to get a file 1024 pixels wide:
>>> 100 * 1024 // 500
204
So you must render the EPS again using -r204
.
Edit 1:
I got the solution from this Question
We can set custom width and height using -gNNNNxMMMM
but the dpi value crops only a small area. I tried with the usual 72dpi and I got a decent output(I'm not sure if it's perfect or not). Now I need to find how to execute this command every time when I run the program and provide the custom image size value. :\
Related Topics
Sending in Column Name to Ddply from Function
Inserting Stargazer or Xable Table into Knitr Document
How to Use a Character as Attribute of a Function
Concatenate Values Across Columns in Data.Table, Row by Row
Fastest Way to Filter a Data.Frame List Column Contents in R/Rcpp
How to Make Shinyapp to Use Environmental Variables When Deployed on the Web
Extract Consecutive Pairs of Elements from a Vector and Place in a Matrix
Cumulative Sum in a Window (Or Running Window Sum) Based on a Condition in R
How to Let R Use All the Cores of the Computer
Tidyr Separate Only First N Instances
Twitter Sentiment Analysis W R Using German Language Set Sentiws
R: Interactive Plots (Tooltips): Rcharts Dimple Plot: Formatting Axis
Count Consecutive True Values Within Each Block Separately
Rstudio Shiny Not Able to Use Ggvis
How to Control the Canvas Size in Ggplot
Math of Tm::Findassocs How Does This Function Work
R Dplyr Filter Based on Matching Search Term with First Words of Any Work in Select Columns