ChatGPT and Python: A Duo for a Custom PDF Editor

Le brief IA que les pros lisent chaque soir
Les 7 actus IA du jour, décryptées en 5 min. Gratuit.
Inclus dès l'inscription : notre sélection des meilleurs guides & comparatifs IA.
Choisis ton rythme
Gratuit · Pas de spam · Désabonnement en 1 clic
An Unexpected Request
Denise, my wife, recently joined her church choir and encountered an unexpected technical challenge. She needed to practice new songs provided in booklets printed on yellow paper. Her goal was to digitize these booklets into PDF format, remove the background color, and reprint them in a larger format on 8.5 by 11-inch paper. This would allow her to read without reading glasses while saving ink.
One afternoon, she asked me how to remove the yellow background while keeping the music intact. Printing in black and white was not a viable solution, as it would consume a lot of ink for a gray background, making it difficult to read. Additionally, she was using the PlayScore 2 app to play the music and was concerned that the colored background might interfere with the software.
The Limitations of Photoshop
My first suggestion was to use Photoshop to eliminate the yellow background. However, this solution proved too complex. Each image required specific adjustments, which was not only frustrating but also time-consuming.
The ChatGPT Alternative
I then suggested trying ChatGPT, as Denise had a ChatGPT Plus account. I ran a few tests by asking:
- "Give me a PDF where the yellow background is replaced with white."
- "That was correct, but the text quality was low. Can you regenerate it in high resolution?"
Although these attempts worked, ChatGPT slightly altered the PDFs. Denise feared that the AI might change the notes or lyrics, thus compromising her musical practice.
Understanding Non-Determinism
AIs like ChatGPT are non-deterministic, meaning that the same instructions can produce different results each time. Denise had already experienced this and did not want to risk receiving modified files. She needed a deterministic tool to remove the background color.
Python to the Rescue
During my Python programming certification at Harvard, I had developed an image management tool capable of transformations similar to Photoshop. I knew that Python could meet her needs. However, I lacked the time to code a program. But ChatGPT could be used to generate a Python script that would address her specific requirements.
Designing the Script
I asked ChatGPT to write a Python script to remove the color. While I was preparing dinner with Denise, I gave this instruction:
"Write a Python script that takes a JPEG and transforms all non-gray or non-black pixels to white, saving the file as name-decolor.jpg. Allow for slightly tinted grays so that black text on a colored background is rendered correctly as black text. Ensure it also works for PDFs, even if they have multiple pages."
When dinner was ready, ChatGPT had completed the task. The first version required the installation of a Python library, but after that, the script worked perfectly.
How It Works
The program is simple to use. You just need to run decolor_pdf.py from the command line with a PDF file as input. The program generates a new PDF without background color.
% python decolor_pdf.py input.pdf
Conclusive Results
The script worked as intended. To avoid using copyrighted hymns, Denise downloaded a public domain song from the New York Public Library for the demonstration. The script is available on my GitHub repository for those who wish to try it.
Lessons Learned
This experience revealed several lessons:
- AI can be used to create deterministic solutions, avoiding the uncertainties of non-deterministic processing.
- A command-line tool can suffice without requiring a complex interface.
- It is possible to refine specifications with AI to achieve precise results.
- Python offers many libraries to perform a variety of tasks.
In conclusion, asking ChatGPT to create a custom tool was an effective solution, allowing Denise to practice her music hassle-free while we prepared a delicious dinner together.
Brief IA — L'actualité IA en français
L'essentiel de l'actualité de l'intelligence artificielle, décrypté et expliqué chaque jour.