How can I extract color values from an EPS file?

一曲冷凌霜 提交于 2019-12-19 11:26:08

问题


I'm trying to find all fill and stroke values used in an EPS file. I can parse the file, I just can't figure out how color values are defined in the EPS postscript section. I have converted the file to SVG (using ghostscript) and I can get the hex values, but an EPS to SVG conversion with a gradient produces files 20x the size. Is there a standard format for color values in postscript that I can extract from an EPS file?


回答1:


PostScript is a programming language, not a simple file format, so there is no simple way to determine what is going on in the program.

A gradient may well be defined as a smooth shading in PostScript, which is a high level construct with no equivalent in SVG, so it will be rendered as an image (hence the explosion in size).

You can use the fact that PostScript is a programming language by redefining the basic operations, and using that to get the information you want. For example, to find the colour being used for a stroke you might do :

/OriginalStroke /stroke load def
/stroke {
(Current colour space = ) print currentcolorspace == flush
(current colour = ) print mark currentcolor counttomark -1 1 { -1 roll 20 string cvs print ( ) print} for flush pop
OriginalStroke
} bind def

Of course you will need to be prepared to cope with the rich variety of possible colour spaces in PostScript; Gray, RGB, CMYK, Separation, DeviceN, CIEBasedA, CIEBasedABC, CIEBasedDEF, CIEBasedDEFG, Indexed and Pattern.

Possibly you don';t need to know the original values, I'm guessing this is true because conversion to SVG will, I think, convert all colours to RGB, so perhaps you only want the RGB equivalents. In which case you could simply use:

(current colour in RGB = ) print currentrgbcolor 3 -1 roll == exch == == flush

I don't know how you wold handle a fill with a Pattern colour though :-)

Perhaps if you explained why you want to know this it would be easier to help.




回答2:


the size of the bit image should not really matter if all you do is get a histogram and throw it away:

 pstopnm -stdout file.ps | ppmhist

I assume everyone has netpbm...netpbm.sourceforge.net



来源:https://stackoverflow.com/questions/16680747/how-can-i-extract-color-values-from-an-eps-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!