ChatGPT and Python: Transforming Musical Scores
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 Innovative Approach
Recently, Denise, my wife, joined her church choir. She has a beautiful voice and wanted to practice with new songs. These songs were printed on yellow sheets of paper, and she wanted to digitize them into PDF format, remove the color, and reprint them in a larger format on 8.5 by 11-inch paper to avoid wearing her reading glasses.
One afternoon, she asked me how to remove the yellow background while keeping the music intact, so she wouldn't waste too much ink from her color printer. Printing in black and white would also consume a lot of ink for a gray background, making the text hard to read.
The Limitations of ChatGPT
She also planned to use PlayScore 2, an app that plays the music so she can sing along. She was concerned that the software might not appreciate the background color.
I initially suggested using Photoshop to remove the yellow background, but the process turned out to be too complicated. Each image required slightly different slider adjustments, which was frustrating and time-consuming.
So, I proposed using ChatGPT. She had a ChatGPT Plus account, so it seemed like a reasonable option.
Determinism vs. Non-Determinism
I ran a few tests with queries such as:
- "Give me a PDF I can download where the yellow background has been replaced with white."
- "You did it correctly, but unfortunately, the text quality is very low. Can you regenerate it in full resolution, please?"
These tests worked, but ChatGPT subtly altered the resulting PDFs. Denise was worried that ChatGPT might change the notes, lyrics, or some other aspect of the original music. She didn't want to risk practicing incorrectly.
ChatGPT and other AIs are non-deterministic. This means you can provide the same input multiple times and get different results each time. It's a bit like talking to a plumber or an electrician. AIs base their results on a complex series of probability calculations, so the outcomes can vary with each execution.
Denise noticed this in her interactions with ChatGPT. She didn't want to give her music to ChatGPT and receive something where the AI had taken liberties with the scores. She wanted a tool to remove the background color that was strictly deterministic.
Python Can Do It
For my final project in my Python programming certification at Harvard, I wrote an interactive image management tool capable of performing Photoshop-like image transformations and processing them in sequences. So, I knew that Python had the necessary libraries to accomplish what Denise wanted.
However, I didn't have the time to write a Python program for this. It was a very busy week, with a long backlog of professional projects to manage.
But ChatGPT had all the time in the world. That's where things get interesting. You can use a non-deterministic tool like ChatGPT to generate a deterministic program, like a color removal tool in Python.
I decided to ask ChatGPT to write a Python script to remove the color. To create the script, I gave this instruction and then went to help my wife prepare dinner.
The Python Script
I asked:
"Write a Python script that takes a jpeg as input and replaces all pixels that are not gray or black with white, saving it as name-decolor.jpg where name is the filename. Allow for slightly tinted grays so that black text on a colored background is rendered correctly. Can you do the same if a PDF is presented? It should also work for multi-page PDFs."
When dinner was ready, ChatGPT had finished. The first version of the script had a few issues because I needed to install a Python library. But after that, it simply worked.
It operates very simply. You run the program decolor_pdf.py from the command line, providing it with a single PDF file. It generates a new PDF file with the background color removed.
% python decolor_pdf.py input.pdf
Results and Lessons
Yes, it works. If you want to download a copy for yourself, it's available on my GitHub repository. My wife was concerned about using screenshots in this article from copyrighted church hymns, so she visited the New York Public Library website and retrieved a public domain song for demonstration.
There are a few useful lessons to take away from this experience:
-
If you want help from an AI, you don't always need to rely on non-deterministic processing. Sometimes, you can simply ask it to write a program based on a reliable algorithm.
-
You don't need to spend a lot of time creating an interface or design. Sometimes, a simple command-line tool will do the job.
-
Don't be afraid to refine your specification with the AI. Try what it creates, then ask for adjustments and corrections.
-
Python can do a lot of things. There are many libraries, so if you're unsure of what you want to use, build it in Python.
There you go. If you need a quick solution to a problem, try asking ChatGPT to write it for you. It worked for me, and I was able to give my wife a functional tool while helping her prepare a roast chicken.
Brief IA — L'actualité IA en français
L'essentiel de l'actualité de l'intelligence artificielle, décrypté et expliqué chaque jour.