Rust-y Adventures with GPT-4: From Raster to Vector

Marc Puig
5 min readApr 9, 2023

--

As a seasoned software developer, I am always eager to explore new technologies and programming languages. That’s why I recently embarked on a journey to learn Rust, one of the most popular new programming languages out there.

To make the learning process more interesting and hands-on, I decided to take on a project: developing a program to convert raster bitmaps to vector files.

And to help me along the way, I enlisted the help of GPT-4.

In this post, I’ll be sharing my journey of learning Rust from scratch and building a practical application with the help of GPT-4.

Zero-ish Rust experience

To begin my journey and kickstart the bitmap-to-vector project, I started by installing Rust on my laptop and familiarizing myself with the basic steps to run a simple “hello world” program. With this initial setup complete, I was eager and excited to dive right into the project.

Let’s go

Open ChatGPT, set the model GPT-4, and ask the first question:

Hey GPT-4, can you tell me how to start with Rust?

The answer:

Suggested steps by GPT-4 to build a bitmap tracer tool.

The answer also includes some initial code:

Initial program structure suggested by GPT-4

Great job! The code appears to be logically structured and capable of converting bitmaps into vector images. Now, let’s copy and paste it into the main.rs file and run the cargo run command. Unfortunately, this led to 8 errors due to missing imports and types. However, not to worry, we can add the necessary imports manually into the Cargo.toml file and request some help from GPT-4 to complete some of the missing types. With these changes, we should be well on our way to a fully functional program.

Suggested Rust structures by GPT-4

Fantastic work! I really appreciate the explanations GPT-4 included alongside the code. They’re incredibly helpful in understanding what I’m trying to achieve. Now that I have all the necessary types defined, I can move on to creating the functions that will bring our project to life.

Suggested function code by GPT-4

Same here,… the comments are really useful… but when compiling the code, new kind of errors are popping up… now related to an incorrect use of arguments. So I ask about the error:

Suggested fixes by GPT-4

The development process continued in this manner for a while, with me requesting new functions code, attempting to build, encountering errors, and then asking GPT-4 for help to fix them before moving on to the next task.

Hallucinations?

However, not everything went smoothly, and not all of the responses from GPT-4 were helpful. Entering hallucinations here. At some point GPT-4 suggests me to use the algorithm Ramer-Douglas-Peucker for polyline simplification at the trace_bitmap function, suggesting to use the crate rdp:

GPT-4 screwing it up

The explanation looks good, but the problem is that the suggested crate is not correct. So I told GPT-4 about the problem and it’s response:

GPT-4 confused

And I got caught in a cycle with no way out. After several attempts, I realized that the only way forward was to find an implementation of the algorithm and incorporate it into the project as plain code.

Happy end

In just a few hours, I had a functional version of the code that was free of errors. This new version could take an input PNG file and convert it into a sleek black and white SVG file. It was a rewarding accomplishment that marked a significant step forward in the project’s development.

From left to right: original, quantized, vectorized.

What about adding colors?

It’s a fair question… and one that I’ve spent some time considering myself. After all, generating vector files in color would take the project to the next level. However, it’s not a small task by any means. I was certainly tempted to try, and I spent several more hours tinkering with the code. The Best image I could obtain was the following one.

8 colors vectorized panda

It’s clear that achieving a high-quality, fully-colored output is going to take some serious effort. While there’s a lot of literature available on vectorization and multiple algorithms that can be used to trace segments, it’s important to remember that this project had a different goal in mind.

Although there’s still a lot of work left to do, I’m happy with the progress I’ve made so far. Additionally, I’m particularly pleased with the valuable experience gained from working with GPT-4 as a development peer.

Conclusions

In conclusion, my experience with using GPT-4 as a development peer for learning Rust and building a bitmap-to-vector conversion program was overall positive. The contextual information and suggestions provided by GPT-4 were incredibly helpful in guiding my development process and ensuring that I was on the right track.

Although the majority of GPT-4’s suggestions were good, it’s important to note that, apart of a strict guidance, the final quality of the program still required human supervision to ensure that it met the necessary standards.

Next steps

It seems to me that this development process could be further automated. In fact, this is precisely what Auto-GPT and BabyAGI projects and similar initiatives are attempting to do. Moving forward, I plan to explore these options as the next steps in my journey towards streamlining and improving the programming process.

Source Code

--

--