{"id":23123539,"url":"https://github.com/devvyyxyz/images-and-audio-converter-with-python","last_synced_at":"2025-04-04T04:24:46.282Z","repository":{"id":243762191,"uuid":"813374488","full_name":"devvyyxyz/images-and-audio-converter-with-python","owner":"devvyyxyz","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-11T01:14:22.000Z","size":10027,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T15:44:18.355Z","etag":null,"topics":["audio","bit","converter","image","images","python","visual"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devvyyxyz.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-11T00:57:54.000Z","updated_at":"2025-01-27T07:47:38.000Z","dependencies_parsed_at":"2024-06-11T03:16:52.702Z","dependency_job_id":null,"html_url":"https://github.com/devvyyxyz/images-and-audio-converter-with-python","commit_stats":null,"previous_names":["devvyyxyz/images-and-audio-converter-with-python"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devvyyxyz%2Fimages-and-audio-converter-with-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devvyyxyz%2Fimages-and-audio-converter-with-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devvyyxyz%2Fimages-and-audio-converter-with-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devvyyxyz%2Fimages-and-audio-converter-with-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devvyyxyz","download_url":"https://codeload.github.com/devvyyxyz/images-and-audio-converter-with-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247119690,"owners_count":20886790,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["audio","bit","converter","image","images","python","visual"],"created_at":"2024-12-17T07:35:10.022Z","updated_at":"2025-04-04T04:24:46.254Z","avatar_url":"https://github.com/devvyyxyz.png","language":"Python","funding_links":["https://www.patreon.com/devvyyxyz"],"categories":[],"sub_categories":[],"readme":"# Image to Audio Converter and Vice Versa\nThis Python script allows you to convert an image to an audio file and also convert an audio file back to an image. It uses the `Pillow` library for image processing, `pydub` for audio processing, `tqdm` for progress bars, and `scipy` for audio analysis.\n\n# Requirements\nBefore running the script, ensure you have the following libraries installed:\n\n- `Pillow`\n- `numpy`\n- `pydub`\n- `tqdm`\n- `scipy`\n- \nYou can install these libraries using `pip`:\n```bash\npip install pillow numpy pydub tqdm scipy\n```\n\n# Usage\n\n1. Clone the repository or download the script.\n2. Run the script using Python.\n\n## Example\n```bash\npython script.py\n```\n\nWhen you run the script, you will be prompted to choose an option:\n```vbnet\nWhat would you like to do?\n1: Convert image to audio\n2: Convert audio to image\nEnter your choice (1 or 2):\n```\n\n## Convert Image to Audio\n1. Choose option `1`.\n2. Enter the path to the input image.\n3. Enter the path to save the output audio file.\n```lua\nEnter your choice (1 or 2): 1\nEnter the path to the input image: input_image.jpg\nEnter the path to save the output audio: output_audio.wav\n```\n\n## Convert Audio to Image\n1. Choose option 2.\n2. Enter the path to the input audio file.\n3. Enter the path to save the output image.\n```lua\nEnter your choice (1 or 2): 2\nEnter the path to the input audio: input_audio.wav\nEnter the path to save the output image: output_image.jpg\n```\n# code\n```py\nfrom PIL import Image\nimport numpy as np\nfrom pydub import AudioSegment\nfrom pydub.generators import Sine\nfrom tqdm import tqdm\nfrom scipy.fft import fft\nimport wave\nimport contextlib\n\ndef image_to_audio(image_path, output_audio_path):\n    # Load the image\n    img = Image.open(image_path).convert('L')  # Convert image to grayscale\n    img = img.resize((100, 100))  # Resize for simplicity\n    pixels = np.array(img)\n\n    # Create an empty audio segment\n    audio = AudioSegment.silent(duration=0)\n\n    # Map pixel brightness to frequency (pitch)\n    max_freq = 2000  # Maximum frequency (Hz)\n    min_freq = 200   # Minimum frequency (Hz)\n    duration_ms = 50 # Duration of each tone (ms)\n\n    # Initialize the progress bar\n    total_pixels = pixels.shape[0] * pixels.shape[1]\n    with tqdm(total=total_pixels, desc=\"Converting image to audio\") as pbar:\n        for row in pixels:\n            for pixel in row:\n                # Normalize pixel value to range between min_freq and max_freq\n                frequency = min_freq + (max_freq - min_freq) * (pixel / 255.0)\n                # Generate a sine wave for the corresponding frequency\n                sine_wave = Sine(frequency).to_audio_segment(duration=duration_ms)\n                # Append the sine wave to the audio segment\n                audio += sine_wave\n                # Update the progress bar\n                pbar.update(1)\n\n    # Export the generated audio\n    audio.export(output_audio_path, format=\"wav\")\n\ndef audio_to_image(audio_path, output_image_path):\n    # Load the audio\n    with contextlib.closing(wave.open(audio_path, 'r')) as f:\n        frames = f.getnframes()\n        rate = f.getframerate()\n        duration = frames / float(rate)\n        audio_data = np.frombuffer(f.readframes(frames), dtype=np.int16)\n\n    # Parameters\n    min_freq = 200   # Minimum frequency (Hz)\n    max_freq = 2000  # Maximum frequency (Hz)\n    duration_ms = 50 # Duration of each tone (ms)\n\n    # Calculate the number of samples per segment\n    segment_length = int(rate * (duration_ms / 1000.0))\n\n    # Calculate the number of segments\n    num_segments = int(duration * 1000 / duration_ms)\n\n    # Initialize an empty list to store pixel values\n    pixel_values = []\n\n    # Initialize the progress bar\n    with tqdm(total=num_segments, desc=\"Converting audio to image\") as pbar:\n        for i in range(num_segments):\n            # Extract the segment of audio data\n            segment = audio_data[i * segment_length:(i + 1) * segment_length]\n            # Perform FFT to find the dominant frequency\n            freqs = np.fft.fftfreq(len(segment))\n            fft_values = np.abs(fft(segment))\n            dominant_freq = abs(freqs[np.argmax(fft_values)])\n\n            # Map the frequency to a pixel value\n            pixel_value = 255 * (dominant_freq - min_freq) / (max_freq - min_freq)\n            pixel_value = int(np.clip(pixel_value, 0, 255))\n\n            # Append the pixel value to the list\n            pixel_values.append(pixel_value)\n\n            # Update the progress bar\n            pbar.update(1)\n\n    # Convert the list of pixel values to a 2D array\n    pixels = np.array(pixel_values).reshape((100, 100))\n\n    # Create an image from the 2D array of pixels\n    img = Image.fromarray(pixels.astype(np.uint8))\n    img.save(output_image_path)\n\nif __name__ == \"__main__\":\n    import sys\n\n    print(\"What would you like to do?\")\n    print(\"1: Convert image to audio\")\n    print(\"2: Convert audio to image\")\n\n    choice = input(\"Enter your choice (1 or 2): \")\n\n    if choice == \"1\":\n        image_path = input(\"Enter the path to the input image: \")\n        output_audio_path = input(\"Enter the path to save the output audio: \")\n        image_to_audio(image_path, output_audio_path)\n    elif choice == \"2\":\n        audio_path = input(\"Enter the path to the input audio: \")\n        output_image_path = input(\"Enter the path to save the output image: \")\n        audio_to_image(audio_path, output_image_path)\n    else:\n        print(\"Invalid choice. Please enter 1 or 2.\")\n```\n## License\n\nThis project is licensed under the MIT - see the [LICENSE](LICENSE) file for details.\n\n### Donate\n\u003ca href=\"https://www.patreon.com/devvyyxyz\" rel=\"noopener nofollow ugc\"\u003e\n\u003cimg src=\"https://wsrv.nl/?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2F%40intergrav%2Fdevins-badges%403%2Fassets%2Fcompact%2Fdonate%2Fpatreon-singular_vector.svg\u0026amp;n=-1\" alt=\"paypal-singular\"\u003e\n\u003c/a\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevvyyxyz%2Fimages-and-audio-converter-with-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevvyyxyz%2Fimages-and-audio-converter-with-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevvyyxyz%2Fimages-and-audio-converter-with-python/lists"}