How to change background color of an eps file while converting it to jpeg or png - image-manipulation

I am converting eps (Encapsulated PostScript) files to jpeg files with ghostscript. A sample command I use is:
gswin32.exe -sDEVICE=jpeg -dJPEGQ=100 -dNOPAUSE -dBATCH -dSAFER -r600x600 -dGraphicsAlphaBits=4 -dUseCIEColor -dEPSCrop -sOutputFile=”a.jpeg” b.eps
The input eps files come with white backgrounds (I only have their clipping path). What I need to do is change this white background to another color in the output images, or it would be even better if I could make them transparent (output file format would be png). How can I do this?

never tried it myself but you should be able to convert your eps file into png by setting:
-sDEVICE=pngalpha
also the pngalpha device has a -dBackgroundColor option:
-dBackgroundColor=16#RRGGBB (RGB color, default white = 16#ffffff) For
the pngalpha device only, set the
suggested background color in the PNG
bKGD chunk. When a program reading a
PNG file does not support alpha
transparency, the PNG library converts
the image using either a background
color if supplied by the program or
the bKGD chunk. One common web browser
has this problem, so when using on a web page you
would need to use
-dBackgroundColor=16#CCCC00 when creating alpha transparent PNG images
for use on the page.
more details here: Details of Ghostscript output devices see section 3.1. PNG file format

After you've obtained your (white background) images from Ghostscript, you could use ImageMagick's convert or GraphicMagick's gm convert commands to change the white to transparent background:
convert -background transparent my.png my_transp.png

Related

Output high-resolution figure from Matlab with full picture

I want to output a high-resolution figure from Matlab.
I tried to "save as pic.eps". But when I insert this picture in latex, it seems that the picture is not "real eps" in the sense that I can tell that the resolution is not enough.
I also tried to "save as pic.pdf". The resolution meets my demand, but the pic is cropped, as it is too large.
Then I used
print -depsc -tiff -r300 -painters pic.eps
This gives me the high-resolution full picture, but some of the dashed lines in the original picture disappeared. I have tried many other cases and still couldn't find the right way to solve my problem. Any suggestions are highly appreciated
I'd try to save eps in vector format, or use a vector only format such as .svg.
For example,
print -depsc2 -painters test.eps
These are the vector graphics supported formats according to the current documentation:
Option Vector Graphics Format File Extension
'-dpdf' Full page Portable Document Format (PDF) color .pdf
'-deps' Encapsulated PostScript (EPS) Level 3 black and white .eps
'-depsc' Encapsulated PostScript (EPS) Level 3 color .eps
'-deps2' Encapsulated PostScript (EPS) Level 2 black and white .eps
'-depsc2' Encapsulated PostScript (EPS) Level 2 color .eps
'-dmeta' Enhanced Metafile (Windows® only) .emf
'-dsvg' SVG (Scalable Vector Graphics) .svg
'-dps' Full-page PostScript (PS) Level 3 black and white .ps
'-dpsc' Full-page PostScript (PS) Level 3 color .ps
'-dps2' Full-page PostScript (PS) Level 2 black and white .ps
'-dpsc2' Full-page PostScript (PS) Level 2 color .ps
you'll also have to use the '-painters' renderer when saving vector graphics files. If you save to a vector graphics file and if the figure RendererMode property is set to 'auto', then print automatically attempts to use the Painters renderer. If you want to ensure that your output format is a true vector graphics file, then specify the Painters renderer. For example:
print('-painters','-deps','myVectorFile')
this command in question may come from the following link
After removing -tiff, my issue of missing one line was fixed (even though I don't understand what is '-tiff' for)

Load PNG RGBA channels like interleaved JPG RGB channels

Can a PNG file's data be loaded in a certain order, say beginning with one RGBA channel, then the rest?
Perhaps similar to how JPG files can, sometimes using custom codecs, store their data as "scans" or "slices", that are loaded progressively, sequentially or interleaved, at first displaying black and white, followed by red, green, and finally blue.
It seems very unlikely, but can PNG files have a way of behaving like this?
References:
https://cloudinary.com/blog/progressive_jpegs_and_green_martians
Besides standard/progressive, the 3rd kind of JPEG compression: load by channel?
https://graphicdesign.stackexchange.com/a/55570/97317
PNG's equivalent to JPEG's progressive display is interlacing. Rather than interlacing lines, it interlaces pixels using a pattern the PNG standard calls ADAM7.

PIL: converting an image with mode "I" to "RGB" results in a fully white image

The image at the end of this question is a PNG with mode I, which stands for Indexed, as far as I can tell.
I'm trying to create a thumbnail out of it, and save it as JPG with PIL.
However, is I leave the mode alone, PIL won't let me resize it with error unable to generate thumbnail: cannot write mode I as JPEG.
If I convert it to RGB, the result will be a fully white image.
Is there a way to fix this?
https://www.dropbox.com/s/2d1edk2iu4ixk25/NGC281.png
The input image is a 16-bit grayscale PNG, and it appears PIL has a problem with this. Manually converting it to an 8-bit image before further processing makes it work again.
The problem may originate inside PIL itself. The PyPNG homepage asserts
..PIL only has internal representations (PIL mode) for 1-bit and 8-bit channel values. This makes me wonder if PIL can read PNG files with bit depth 2 or 4 (greyscale or palette), and also bit depth 16 (which PNG supports for greyscale and RGB images).
Then again, that page is from 2009. It could be worth tracking down where PIL is maintained from, and report this as a bug (? Or possibly a feature request?).

Cant export matlab figure using custom font

I'm trying to export my plot with Tahoma font :
However I always end up with the following figure :
Which is not the font I chose !! can someone tell me why is this happening ?
Also, the legend's font do not change either.
For export figures in MATLAB, I highly recommend you to try export_fig. WYSIWYG!
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
Figure/axes reproduced as it appears on screen
Cropped borders (optional)
Embedded fonts (pdf only)
Improved line and grid line styles
Anti-aliased graphics (bitmap formats)
Render images at native resolution (optional for bitmap formats)
Transparent background supported (pdf, eps, png)
Semi-transparent patch objects supported (png only)
RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
Variable image compression, including lossless (pdf, eps, jpg)
Optionally append to file (pdf, tiff)
Vector formats: pdf, eps
Bitmap formats: png, tiff, jpg, bmp, export to workspace
This function is especially suited to exporting figures for use in publications and presentations, because of the high quality and portability of media produced.
I think you cannot embed fonts to MATLAB figures normally, you can just use Type1 fonts.
Check this MATLAB function, by Oliver Woodford, maybe it can help: [Link]

Matlab: How to save plots of patches / fill() automatically in the code?

quick question: I'm creating "random" polygons using either the patch() or the fill() function in Matlab. This works quite good and it is plotted correctly.
However, I need to at least save a few hundres polygons as images to my hard drive for working with them later - so I'm looking for a way to directly save the image in my function rather than saving each polygon myself using the file-menu.
Is there any way to do this?
Thanks in advance!
You can indeed use the print function, but I would not use the jpeg device. JPEG is never the right format for plots (you will get a lot of artifacts near all your lines).
If you need a bitmap image, try the png or tiff device. If you don't need a bitmap, use the appropriate vector image format: fig is the native MATLAB format (which allows you to edit the plot afterwards), so this is the best one if you stick with MATLAB for all your operations. For exporting to other software, I would recommend pdf (works almost anywhere), epsc (EPS with color, great for LaTeX or inkscape), wmf/emf (Windows Metafile, so Windows only, but great for including the images in MS Office). Or you could of course use any of the other formats mentioned in the print documentation.
Sometimes it's a pain in the neck to get the format of your image all right (especially with PDF output). Just take a look at the different properties of your figure and more specifically the PaperSize, PaperUnits and PaperPosition.
The easiest way, and I guess the best solution, is to save as a .fig file. You can do this by using saveas:
h = figure;
% your plot commands here
saveas(h,'mFile.fig');
Afterwards, you can reload the image with the openfig function:
openfig('mFile.fig');
Have to add this answer. This function is helping a lot.
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
   - Figure/axes reproduced as it appears on screen
   - Cropped/padded borders (optional)
   - Embedded fonts (pdf only)
   - Improved line and grid line styles
   - Anti-aliased graphics (bitmap formats)
   - Render images at native resolution (optional for bitmap formats)
   - Transparent background supported (pdf, eps, png)
   - Semi-transparent patch objects supported (png only)
   - RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
   - Variable image compression, including lossless (pdf, eps, jpg)
   - Optionally append to file (pdf, tiff)
   - Vector formats: pdf, eps
   - Bitmap formats: png, tiff, jpg, bmp, export to workspace