Bitmap Tracing with CLI Tool Potrace

Posted by Praveen on February 28, 2023 · 2 mins read

Today, I want to share with you a nifty little trick I discovered while working with images in Inkscape. You know how Inkscape has this handy “Trace Bitmap” option? It’s great, but it requires manually loading each image and setting the threshold value. Wouldn’t it be awesome if we could automate the process and batch convert multiple files using a simple bash script? Well, I’ve got some good news for you!

Inkscape actually uses a package called “Potrace” under the hood for bitmap tracing. The only catch is that Potrace is not directly accessible via the command line, but fear not, as I have a workaround for you.

Let’s dive into the steps to achieve this seamless batch conversion of images using Potrace and Inkscape.

Step 1: Install Potrace To get started, we need to have Potrace installed on our system. You can do this easily by running the following command:

$ sudo apt install potrace

Step 2: Convert Image Formats (if needed) Now, Potrace can only recognize bitmap images like PNM or BMP. If your files are in JPG or PNG format, you’ll need to convert them to PNM first. Don’t worry; it’s a breeze with the “convert” command, which comes bundled with the Imagemagick package.

$ mogrify -format pnm *.png

Step 3: Convert Images to PDF Potrace, by default, converts images to EPS format, which won’t allow us to perform batch conversions. So, let’s choose the PDF mode instead and convert our files.

$ potrace *.pnm -b pdf -o filename.pdf

Step 4: Split PDF into Individual Image Files Now comes the fun part! We have a PDF file containing all our traced images. Let’s break it down into separate image files using the “convert” command again.

$ convert filename.pdf filename.png

Voila! You’ll find your individual image files, each appended with the filename followed by the page number.

And that’s it! With these simple steps, you can now efficiently perform batch conversions of bitmap images, saving you time and effort. Whether you’re working on intricate designs, illustrations, or creative projects, this method will undoubtedly enhance your workflow.