File Exchange

image thumbnail

ExportPngInkscape(f​ilename,dpi,handle)

version 1.1.0.0 (1.81 KB) by Alessandro Masullo
Save figures in high quality PNG by saving them in SVG and exporting in PNG using Inkscape

0 Downloads

Updated 30 Jun 2016

View License

Matlab function for exporting figures in PNG is terrible. There is no anti-alias (although something better has been tried with the new graphic engine) and even increasing the exported file resolution, images still look horrible.
The best solution that I've found for exporting images in a nice way is saving them in SVG and exporting them to PNG using Inkscape, but this requires a lot of time. For this reason, I coded this very basic routine that takes care of everything: first, the image is saved in a vectorial format (SVG) using "print", then Inkscape is invoked through command line to export the SVG file in a PNG with the desired resolution.
This function uses Matlab internal SVG printer, which has been introduced since R2014a. If you have a previous Matlab version you may want to use the following script from the File exchange to export images to SVG:
http://www.mathworks.com/matlabcentral/fileexchange/7401-scalable-vector-graphics--svg--export-of-figures

Comments and Ratings (2)

@Tien Tran
Apparently, your Matlab can't export in SVG in the first instance.
The documentation says that SVG export was introduced in Matlab 8.3. A possible workaround is to export to EPS instead of SVG. I'll try to see if I get the same results and I'll update the code.
Thank you for letting me know.

Tien Tran

Can you give me more example to export image with this code? I using Matlab 8.3 but can't print image. some error display on screen:
ExportPngInkscape('Hinh_42')
Error using print>LocalPrint (line 291)
Unknown device 'svg' for hardcopy.

Error in print (line 233)
LocalPrint(pj);

Error in ExportPngInkscape (line 47)
print(handle,'_tmp.svg','-dsvg')

Updates

1.1.0.0

Export in PDF rather than SVG, before exporting in PNG

1.1.0.0

Fixed figure transparency

1.0.0.0

Updated description

1.0.0.0

Title updated

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor