Part 2: How to automate graphics production with Inkscape

A couple weeks ago I recorded a 15-minute tutorial with supporting materials on how to automate graphics production in Inkscape by building a base template and automatically replacing various text strings in the file from an CSV using the Next Generator Inkscape extension from Maren Hachmann.

Based on popular demand from that tutorial, I have created a more advanced tutorial that expands upon the last one, demonstrating how to automate image replacement and changing colors via the same method. (Which, oddly, also turned out to be roughly 15-minutes long!)

You can watch it below embedded from the Fedora Design Team Linux Rocks PeerTube channel, or on YouTube. (PeerTube is open source so I prefer it!)

As in the last tutorial, I will provide a very high-level summary of the content in the video in case you’d rather skim text and not watch a video.

Conference Talk Card Graphics

The background on this tutorial is continued from the original tutorial: for each Flock / Nest conference, we need a graphic for each talk for the online platform we use to host the virtual conference. There’s usually on the order of 50+ talks for large events like this, and that’s a lot of graphics to produce manually.

With this tutorial, you will learn how to make a template like this in Inkscape:

Graphic template showing a speaker photo in the lower left corner and a bright red background on the track name.

And a CSV file like this:

ConferenceName TalkName PresenterNames TrackNames BackgroundColor1 BackgroundColor2 AccentColor Photo
BestCon The Pandas Are Marching Beefy D. Miracle Exercise 51a2da 294172 e59728 beefy.png
Fedora Nest Why Fedora is the Best Linux Colúr and Badger The Best Things afda51 0d76c4 79db32 colur.png
BambooFest 2022 Bamboo Tastes Better with Fedora Panda Panda Life 9551da 130dc4 a07cbc panda.png
AwesomeCon The Best Talk You Ever Heard Dr. Ver E. Awesome Hyperbole da51aa e1767c db3279 badger.png

And combine them to generate one graphic per row in the CSV, like so, where the background color of the slide, the background color of the track name / speaker headshot background, and the speaker headshot image changes accordingly:

Graphic showing one of the example rows "Why Fedora is the Best Linux" with a green and blue background, a green accent color, and a hot dog picture as the speaker photo to demonstrate the technique.

As we discussed in the previous post – there are so many things you can use this technique for – even creating consistent cover images for your video channel videos 🙂 I need to point out again, that you could even use it to create awesome banners and graphics for Fedora as a member of the Fedora Design Team!! (We’d love to have you 🙂 )

The Inkscape Next Generator Extension

As in the last tutorial, the first step to creating these is to install the Next Generator extension for Inkscape created by Maren Hachmann, if you haven’t already:

  1. Grab the .inx and .py files from the top level of the repo, download them [next_gen.inx] [next_gen.py].
  2. Then go into the Edit > Preferences > System dialog in Inkscape, search for the “User Extensions” directory listing and click the “Open” icon next to it. Drag the .inx and .py files into that folder.
  3. Close all open Inkscape windows, and restart Inkscape. The new extension will be under the “Extensions” menu: Extensions > Export > Next Generator.

Creating the Template

Each header of your CSV file (in my example: ConferenceName, TalkName, PresenterNames) is a variable you can place in an Inkscape file that will serve as your template. Take a look at the example SVG template file for direction. To have the TalkName appear in your template, create a text object in Inkscape and put the following content into it:

%VAR_TalkName%

When you run the extension, the %VAR_TalkName% text will be replaced with the TalkName listed for each row of the CSV. So for the first row, %VAR_TalkName% will be replaced with the text The Pandas Are Marching for the first graphic. For the second graphic, the TalkName will be Why Fedora is the Best Linux. So on and so forth down the TalkName column per each graphic.

Extending the Template for Color Changes

For the color changes, there’s not much you have to do except decide what colors you want to change, come up for field names for them in your CSV, and pick out colors for each row of your CSV. In our example CSV, we have two colors of the background gradient that change (BackgroundColor1 and BackgroundColor2) and an accent color (AccentColor) that is used to color the conference track name background lozenge as well as the outline on the speaker headshot:

BackgroundColor1 BackgroundColor2 AccentColor
51a2da 294172 e59728
afda51 0d76c4 79db32
9551da 130dc4 a07cbc
da51aa e1767c db3279

Tip: changing only certain items of the same color

There is one trick you have to do if you have the same color you want to change in some parts of the image and to stay the same in other parts of the image.

The way color changes work in Next Generator is a simple find & replace type of mechanism. So when you tell Next Generator in Inkscape to replace anything with the color code #ff0000 (which is in the sample template and what I like to call “obnoxious red”) to some other color (let’s say #aaaa00), it will replace every single object in the file that has #ff0000 as a color to the new value, #aaaa00.

If you wanted just the conference track name background’s red to change color, but you wanted to keep the color border around the speaker’s headshot red in all of the graphics, there’s a little trick you can use to achieve this. Simply use the HSV tool in the Fill & Stroke dialog in Inkscape to tune the red item that you didn’t down just one notch, say to #fa0000, so it has a different hex value for its color code. Then, you can have anything with #ff0000 change color according to the values in your CSV, and anything #fa0000 would stay red and be unaffected by the color replacement mechanism.

Now a couple of things to note about color codes (and we review this in the troubleshooting section below):

  • Do not use # in the CSV or the JSON (more on the JSON below) for these color values.
  • Only use the first six “digits” of the hex color code. Inkscape by default includes 8; the last two are the alpha channel / opacity value for the color. (But wait, how do you use different opacity color values here then? You might be able to use an inline stylesheet that changes the fill-opacity value for the items you want transparency on, but I have not tested this yet.)

Extending the Template for Image Changes

First, you’ll want to add “filler” images to your template (do this by linking them, do not embed them when you import them into Inkscape! I don’t make this point in the video and I should have!) We used just one in our template – photo.png.

Then, similarly to how we prepped the CSV for the color changes, for the image changes you’ll need to come up for field names for any images you’d like to be swappable in your CSV, and list out the image filenames you want to use to replace those images for each row of your CSV. In our example CSV, we have just one image with a field name of “Photo”:

Photo
beefy.png
colur.png
panda.png
badger.png

Note that the images as listed in the CSV are just filenames. I recommend placing these files in the same directory as your template SVG file – you won’t have to worry about specifying specific file paths, which will make your template more portable (tar or zip it up and share!)

Building the JSON for the NextGenerator dialog

The final (and trickiest!) bit of getting this all to work is to write some JSON formatted key-value pairs for NextGenerator to understand which colors / images present in the template file map to which field names / column headers in your CSV file, so it knows what goes where.

Here is the example JSON we used:
{"BackgroundColor1":"51a2da","BackgroundColor2":"294172","AccentColor":"ff0000","Photo":"photo.png"}

Where did I come up with those color codes for the JSON? They are all picked from the template.svg file. 51a2da is the lighter blue color in the circular gradient in the background; 294172 is the darker blue towards the bottom of the gradient. ff0000 (aka obnoxious red) is the color border around the speaker headshot and the background lozenge color behind the track name.

Where did the photo.png filename come from? That’s the name of the filler image I used for the headshot placement (if you’re in Inkscape and not sure what the filename of the image you’re using is, right click, select “Image Properties” and it’s the value in the URL field that pops up in the sidebar.)

Running the Generator

Once your template is ready, you simply run the Next Generator extension by loading your CSV into it, selecting which variables (header names) you want to use in each file name, and copy pasting your JSON snippet into the dialog in the “Non-text values to replace” field:

Screenshot showing the JSON text in the NextGenerator dialog

Then hit apply and enjoy!

Troubleshooting Tips

Tips to troubleshoot color and image replacement issues

Some hard-won knowledge on how to troubleshoot color and/or image replacement not working:

  • Image names are just the filename; keep the images in the same directory as your template and you do not need to use the full file path. (This will make your templates more portable since you can then tar or zip up the directory and share it.)
  • Image names and color values and variable names in the spreadsheet do not need any ” or ‘ unless you need to escape a comma (,) character in a text field. But image names and color values and variable names do need quotes always in the JSON.
  • Color values are not preceded by the # character. It won’t work if you add it.
  • By default Inkscape gives you an 8-“digit” hex value for color codes, the last two correspond to the alpha value of the color (e.g. ff0000ff for bright red with no opacity.) You will need to remove the last two digits so you are using the base 6-“digit” hex code for the color values (that correspond to RGB colors) to remove the opacity/alpha values from the color code. Otherwise, the color replacement won’t work.
  • Check that you have all variable names in the JSON spelled and written exactly the same as in the CSV header entries except with ” in the JSON (e.g. BackgroundColor1 in the CSV is “BackgroundColor1” in the JSON)
  • Use the filename for the default image you are replacing in the template. You do not use the ObjectID or any other Inkscape-specific identifier for the image. Also, link the image instead of embedding it.

Tutorial Resources

All of the example files used in this tutorial are available here:
https://gitlab.com/fedora/design/team/tutorials/inkscape-automation

Link to the Next Generator extension:
https://gitlab.com/Moini/nextgenerator

Direct Links to download *.inx and *.py for the extension:

Have fun 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.