Elras
Elras

Reputation: 19

How to insert vector graphics (SVG) to a raster image such as JPG/TIF programmatically (Python/JS/C++)?

I need a programmatic way to embed a clipping-path (saved as SVG file) to another image such as JPG/TIF/PSD. Using a tool such as Photoshop, this can be done easily and the path will be inserted in the image 8BIM profile, but it seems there is no way to do it programmatically. ImageMagick allows you to get a vector image for example by using the following command:

identify -format "%[8BIM:1999,2998:#1]" test.jpg > test.svg

But it seems not possible to do the reverse operation and add a vector image. Can anyone suggest any libraries which allow this operation?

Upvotes: 1

Views: 574

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207853

It's a bit more code than I feel like writing for the moment, but it should be possible to put an 8BIM into a JPEG using the following information.

  • The anatomy of a JPEG is described here and here.

  • You can use PIL or OpenCV to encode a JPEG into a memory buffer and then locate and modify/add segments (such as an 8BIM) using code like this. Or you could just read() in an existing JPEG that you want to modify. To insert a segment, just write the first few segments to disk, then write your new one followed by the remaining segments from the existing file that you read at the start.

  • You can construct an 8BIM segment to insert using this answer.

  • You can use exiftool -v -v -v to see where an 8BIM appears in a JPEG created by Photoshop and then put yours in a similar place. You can also, obviously, equally use exiftool to see where/how your own attempt has landed.

Upvotes: 1

Related Questions