Skip to content

About

This tool converts a given image into a spectrogram representation using experimental methods. and subject to further development and optimization.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Image2Spectrogram 🎵🖼️

Hide an image inside an audio file. Open it in a spectrogram viewer and the picture appears.

License Python


🎉 After a year in beta, Image2Spectrogram has finally graduated and shipped its first stable release.

Overview

Image2Spectrogram is a small command-line tool that converts an image into a .wav file. The audio is synthesized so that its spectrogram (frequency over time) draws the original image.

Each image column becomes a short slice of audio, and each row becomes a sine wave at a specific frequency. The darker a pixel, the louder its tone, so shades of gray come through as well as solid shapes. Played back, it sounds like a burst of tones; viewed in a spectrogram, it reads as a picture.

image (pixels)  ──►  columns = time  ──►  rows = frequency  ──►  output.wav

Features

  • Simple CLI: pass an image, get a WAV. Every parameter is configurable.
  • Grayscale support: each tone's loudness follows its pixel's darkness, so gradients and soft edges show up, not just black and white.
  • Built-in preview: --preview renders the spectrogram of the generated audio to a PNG, so you can check the result without opening Audacity.
  • Clean audio: a Hann window is applied to every column to prevent clicks and pops at column boundaries.
  • High-quality resizing: images are downscaled with Lanczos resampling to preserve detail.
  • Resolution guard: warns you when the chosen height is finer than a spectrogram viewer can resolve.
  • Standard output: 16-bit PCM WAV, 44.1 kHz by default.

Requirements

  • Python 3.9 or newer
  • NumPy, SciPy and Pillow
  • A spectrogram viewer, such as Audacity (optional if you only use --preview)

Installation

git clone https://github.com/SirAtilotty/Image2Spectogram.git
cd Image2Spectogram/Image2Spectrogram

Create a virtual environment and install the dependencies:

python -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate
pip install -r requirements.txt

Arch Linux and other distros with a managed Python: pip install outside a virtual environment fails with externally-managed-environment (PEP 668). Use the venv above, or install the system packages with sudo pacman -S python-numpy python-scipy python-pillow.

Usage

python spectrogram_text.py example.png

This writes output.wav to the current folder. Open it in Audacity, click the track name, choose Spectrogram, and your image shows up.

To check the result without leaving the terminal, add --preview:

python spectrogram_text.py example.png --preview preview.png

preview.png shows the spectrogram of the generated audio, cropped to the encoded frequency range and drawn dark-on-light like your source image.

Examples

# Custom output file
python spectrogram_text.py logo.png -o logo.wav

# Higher resolution (longer audio, finer detail)
python spectrogram_text.py logo.png --width 256 --height 96

# Wider frequency range and a lower tone threshold
python spectrogram_text.py logo.png --freq-start 200 --freq-end 10000 --threshold 0.4

# Generate audio and a preview image in one go
python spectrogram_text.py logo.png -o logo.wav --preview logo_preview.png

Options

Option Default Description
image required Path to the source image.
-o, --output output.wav Output WAV file path.
--width 128 Image width in pixels after resizing. More width means longer audio.
--height 64 Image height in pixels after resizing. More height means finer detail and does not change the duration.
--sample-rate 44100 Audio sample rate in Hz.
--duration-per-column 0.05 Seconds of audio per image column.
--freq-start 300 Lowest frequency in Hz (bottom row of the image).
--freq-end 8000 Highest frequency in Hz (top row of the image).
--threshold 0.5 Pixels with intensity (0 to 1) at or below this stay silent. Lower it to keep lighter grays.
--fft-size 2048 FFT window of your spectrogram viewer. Used for the resolution warning and the preview.
--preview off Save a spectrogram preview PNG to this path.

Run python spectrogram_text.py -h for the full help text.

Preparing your image

  • Dark on light. Dark pixels produce loud tones and appear bright in the spectrogram, so use a dark subject on a white background.
  • High contrast. Thick shapes, silhouettes and bold text survive downscaling best. Soft gradients do carry over, but only above --threshold; lower it if light grays disappear.
  • Match the aspect ratio. The image is resized to --width x --height, so use the same ratio (the default 128x64 is 2:1) to avoid stretching. The source file can be any size at least as large as the target.
  • Keep the height reasonable. Rows closer together than the viewer's frequency resolution blur into each other. With the defaults, about 358 rows is the practical ceiling, and the tool warns you if you go past it.

How it works

  1. The image is converted to grayscale, resized with Lanczos resampling, and inverted so dark pixels carry high intensity.
  2. Columns map to time: each one lasts --duration-per-column seconds.
  3. Rows map linearly to frequency between --freq-start and --freq-end.
  4. For every column, a sine wave is added for each row whose intensity exceeds --threshold, with its amplitude scaled by that intensity.
  5. Each column is multiplied by a Hann window, concatenated, normalized and written as 16-bit PCM.
  6. With --preview, the signal's spectrogram is computed with the same --fft-size, converted to decibels and saved as a PNG.

Total duration is width x duration-per-column, so the default 128 columns produce about 6.4 seconds of audio.

Troubleshooting

Problem Fix
error: the following arguments are required: image Pass the image path: python spectrogram_text.py example.png.
externally-managed-environment Use a virtual environment (see Installation).
Signal is silent The image produced no tones. Check that the subject is dark on a light background, or lower --threshold.
Light gray areas are missing They fall under --threshold. Lower it, for example --threshold 0.2.
Image looks blurry or smeared in Audacity Lower --height, or raise the window size in Audacity under Spectrogram Settings.
Preview looks different from Audacity Set --fft-size to the window size Audacity uses.
Image looks squashed Make the source image match the aspect ratio of --width x --height.

Roadmap

  • Grayscale support
  • Spectrogram preview (--preview)
  • Optional logarithmic frequency scaling
  • Decode an image back out of a WAV file
  • Unit tests and CI

License

Released under the MIT License. See LICENSE for details.

Copyright (c) 2026 Atilla İlhan.

About

This tool converts a given image into a spectrogram representation using experimental methods. and subject to further development and optimization.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages