About

Inkscape is a powerful open-source vector graphics editor which supports the inclusion of raster images either through file references (links) or through direct embedding of the image data in the Inkscape SVG file. Referencing images as links keeps SVG files small and ensures that changes to image placement and transformations specified in the SVG file remain separate from the underlying image data. However, embedding images may be required as a final step in some production work-flows.

This java-based extension for Inkscape facilitates image embedding by:

  • Automatically identifying all linked images
  • Cropping image data that lies outside the images' clipping frame
  • Optionally applying jpeg compression
  • Optionally resampling images above a maximum resolution
  • Writing the cropped and possibly compressed image data directly in the SVG file

By cropping image data that lies outside the clipping frame, applying jpeg compression, or resampling to lower resolution, the resulting file size can be reduced significantly. If preserving image quality is a priority jpeg compression and resampling can be explicitly avoided.

The extension uses ImageJ to load image data and the Apache Commons Codec library to encode the data for embedding.

This extension was developed to help produce publication quality figures for scientific journals. Comments and suggestions are welcome via Github.

Developed by Benjamin Nanes

Installing the Inkscape Extension

Requirements

  • Inkscape 0.48 or newer
  • Java SE8 or newer

Option 1: Build from source

Get the code and build the project using Maven: $ mvn package. Then unpack target/svg-embed-and-crop-<ver>-distr.zip as described below.

Option 2: Download compiled app

svg-embed-and-crop-1.8-distr.zip

Installation

Unpack the entire contents of the archive to the Inkscape extension directory.

For Windows, this is probably something like:
C:\Users\<your user name>\AppData\Roaming\inkscape\extensions\
Note that the AppData directory may be hidden in the file manager. If this is the case, it can be accessed by manually entering the path in the address bar.

For Mac or Linux, this is probably something like:
~/.config/inkscape/extensions/

Run Inkscape. If it was already running, you will have to restart it. The plugin can be run from the menu:
Extensions > Images > Embed and crop images

Known Issues

  • Only clipping planes created from rectangles are currently supported.

  • This extension uses the javaw command to run Java without opening a terminal window. However, this command may be absent in some Java runtimes, particularly on Linux. A workaround is to replace all instances of javaw with java in svg-embed-and-crop.py.

  • In Inkscape versions >= 1.0 and < 1.2, non-ASCII characters in text elements may be disrupted on some systems. Use older or newer Inkscape versions to avoid this issue. More information.

  • PDFs saved from Inkscape may not include jpeg compression, even if images are embedded in the SVG as jpegs. Additional information is available here. A workaround is to embed images without compression, export a PDF, then compress images in the PDF using The PDF Shrinker.

  • On some 64bit Windows systems, Inkscape's Python interpreter fails to recognize the JRE, even if it is correctly registered with the system. A workaround for this issue is to edit svg-embed-and-crop.py to include an absolute path to the JRE.

Release Notes

Version 1.8

  • Fix bug affecting resolution calculation when down-sampling images

Version 1.7

  • Update to JavaSE 8
  • Independently define target and maximum image resolutions
  • Allow selection of Jpeg or PNG image output depending on file size
  • Add support for processing embedded images (command line only)
  • Compatibility fixes for Unix systems

version 1.6

  • Optionally resample images to a maximum resolution to further decrease file size.
  • Improve compatibility with Inkscape versions >= 1.0.

version 1.5

  • Fix resolution of certain relative image paths.
  • Allow output files to be created in new directories.
  • Update initiation script for compatibility with Inkscape 1.0.

version 1.4

  • Fixed bug affecting rotation and skew transformations.

version 1.3

  • Fixed bug causing Java to exit when calling the extension from other Java programs.

version 1.2

  • Fixed bug which resulted in non-default compression quality parameters being ignored.

version 1.1

  • Check for the JRE in some common locations. Partial workaround for problem finding system-registered JRE from 32bit Python interpreter on 64bit Windows.

Running from the Command Line

It is possible to run the app directly from the command line without running Inkscape:

java -classpath <install path> edu.emory.cellbio.svg.EmbedAndCrop    
   [<input>] [-o <output> | -s]
   [-t <type> [-q <quality>]] 
   [-r [<target res.> [<max res.>]]] 
   [-e <size>]
  • <install path>
    Location of the app's jar files, ending with /*. Ex.: /usr/some-dir/svg-embed-and-crop/*

  • <input>
    Path to the input SVG file. If missing, the user will be presented with a file open dialog box.

  • -o <output>
    Path to save the output SVG file with embedded images

  • -s
    Present the user with a file save dialog to specify the output file Note: If neither -o nor -s is specified, the output is sent to the standard output stream

  • -t <type>
    Specify the type of image for encoding. Supported options are png, jpeg, or mix. The latter option indicates that for each image, the format resulting in the smaller file size should be used. If this flag is not set, the user will be presented with a selection dialog.

  • -q <quality>
    Quality parameter for jpeg compression. Default value is 0.85.

  • -r <target res.> <max res.>
    Images with resolution above <max res.> should be downsampled to <target res.>. Resolution is specified in pixels per mm. If this flag is not set, no resampling will be done. If this flag is set, but no target resolution is provided, the default value is 11.811, approximately equal to 300dpi. If only the target resolution is provided, the max resolution defaults to 4/3 of the target resolution.

  • -e <size>
    Minimum size at which already embedded images will be processed. Set to -1 to skip processing of all embedded images (default behavior). Format using common units (20KB, 1MB, etc.)

Examples:

java -classpath /path/to/jars/* edu.emory.cellbio.svg.EmbedAndCrop -s -t jpeg -q 0.95
java -classpath /path/to/jars/* edu.emory.cellbio.svg.EmbedAndCrop input.svg -o output.svg