Last active
June 8, 2024 18:32
-
-
Save bemxio/b0a9fd5084affab0986630d617a320e1 to your computer and use it in GitHub Desktop.
A CLI tool for encoding/decoding images from/to MP3 files, made as an experiment to see how MP3 compression would affect image data. Requires `pydub` and `Pillow` to be installed via `pip`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <https://unlicense.org> | |
""" | |
import pathlib | |
import argparse | |
import pydub | |
from PIL import Image | |
def encode_image(input_path: pathlib.Path, output_path: pathlib.Path) -> None: | |
image = Image.open(input_path) | |
image = image.convert("RGB") | |
width, height = image.size | |
data = bytes([value for pixel in image.getdata() for value in pixel]) | |
audio = pydub.AudioSegment(data, frame_rate=22050, sample_width=1, channels=1) | |
audio.export( | |
output_path or input_path.with_suffix(".mp3"), | |
format="mp3", | |
tags={"width": width, "height": height} | |
) | |
def decode_image(input_path: pathlib.Path, output_path: pathlib.Path) -> None: | |
audio = pydub.AudioSegment.from_file(input_path) | |
audio = audio.set_sample_width(1) | |
metadata = pydub.utils.mediainfo(input_path) | |
width = int(metadata["TAG"]["width"]) | |
height = int(metadata["TAG"]["height"]) | |
data = audio.raw_data | |
image = Image.frombytes("RGB", (width, height), data) | |
image.save(output_path or input_path.with_suffix(".png")) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Encode images into MP3 files and vice versa.") | |
parser.add_argument("input_path", type=pathlib.Path, help="Path to the input file.") | |
parser.add_argument("--output_path", "-o", type=pathlib.Path, help="Path to the output file. Defaults to the input file with a different extension.") | |
args = parser.parse_args() | |
if not args.input_path.exists(): | |
raise FileNotFoundError(f"input path `{args.input_path}` does not exist") | |
if args.input_path.suffix == ".mp3": | |
decode_image(args.input_path, args.output_path) | |
else: | |
encode_image(args.input_path, args.output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment