Using Python and Bash I wrote script that lets a label printer print the lyrics of the song I’m currently listening to on Spotify. To understand the inspiration behind this project, we need to look at self checkout stations at the Swiss retail store Migros.These self checkout station have a fatal flaw in my opinion. They will always print a receipt - even if you don’t want it. (UPDATE: Since about November 2023 the Migros checkout stations let you decide if you you want a receipt or not. yay!!!)

Migros checkout station with receipts laying around

So, instead of just throwing these receipts away and generating waste, I looked for a fun way to re-use them.

I realized that the receipts use thermal paper that can be printed on with heat and without any ink. The paper was printed on with black on white. That allows me to print white on black over the existing text on it.

When I finally saw a thermal printer for a decent price on a second-hand online portal, I couldn’t help myself an finally bought one.

CITIZEN CL-S521 label printer stocked with Migros receipts

The printing of text or images itself is very simple. The CITIZEN CL-S521 can be controlled via the cups driver on the CITIZEN website and thus the lp command on Linux:

# Define variables
text="This string will be printed."
font_size=36
font="Hack-Bold"
 
# Define the name of the temporary files used to print
output_image="/tmp/print_$(date +"%s").png"
output_pdf="/tmp/print_$(date +"%s").pdf"

# Create an image with the string $text on it
# The width is 600 pixel and the height is undefined ("-size ${w}x${h}" -> "-size 600x") and will thus be adjusted automatically
magick \
    -size 600x \
    -background black \
    -font ${font} \
    -kerning -0.5 \
    -weight 700 \
    -pointsize ${font_size} \
    -fill white \
    -gravity NorthWest caption:"${text}" \
    "${output_image}"

# Get the height of the generated image
height=$(
    identify ${output_image} | \
        cut -d " " -f 3 | \
        cut -d "x" -f 2
)

# Convert the height to millimeters
height_mm=$(
cat<<EOF | python
output = (76 / 600) * ${height}
print(int(output+1))
EOF
)

# Convert the png to a PDF
convert "${output_image}" \
    "${output_pdf}"

# Print the PDF
lp -d CITIZEN_CL-S521Z \
    -o media=Custom.76x${height_mm}mm \
    "${output_pdf}"

# Delete the temporary files
rm "${output_image}"
rm "${output_pdf}"

I used the Spotify API in a Python script to figure out to which song I’m currently listening to on Spotify and used the (unofficial) spotify lyrics API to synchronize the lyrics. The lyrics that are playing at the moment will then be printed using the lp command and the Bash commands documented above.

Drucker” (“Printer” in German) from Deichkind and Drunken Masters is the perfect song for this experiment

The accuracy of the timing provided by the API is different from song to song. In the video, I adjusted the timing to match the song “Drucker” specifically.