Skip to main content

A tool to download and organize images referenced in markdown files

Project description

Markdown Image Downloader

A Python script that automatically downloads and manages images referenced in markdown files, storing them locally in an _attachments folder. This script is particularly useful for maintaining local copies of images in markdown documentation and ensuring consistent image availability.

Or just for Obsidian's Readwise export, which I made this for.

Previously hosted on GitHub Gist. Moved here to allow for easier maintenance and contributions, if any. Also published to PyPI for convenience.

Features

  • Uses custom HTTP headers to avoid download blocks
  • Downloads images from URLs referenced in markdown files
  • Creates local copies of images in an _attachments directory
  • Automatically updates links in the markdown files with new local image paths
  • Compresses large images to reduce storage space
  • Supports multithreaded concurrent downloads
  • Uses rate limit to prevent server overload and download blocks
  • Progress bar for tracking download status
  • Maintains detailed logging of error operations
  • Sanitizes filenames for cross-platform compatibility
  • Supports for rerunning the script without re-downloading images

Requirements

Python 3.9+

Installation

Install directly from PyPI using pip:

pip install markdown-image-downloader

Usage

Run the package from the command line, providing the folder containing your markdown files as an argument:

markdown-image-downloader <folder_name>

Example

markdown-image-downloader ../Readwise/Articles

This will:

  1. Scan all markdown files in the ../Readwise/Articles folder
  2. Download any images referenced in the markdown files
  3. Store them in ../Readwise/Articles/_attachments
  4. Update the markdown files to reference the local copies

How It Works

  1. Scanning: The script scans all .md files in the specified folder for image references.
  2. Downloading: For each image URL found:
    • Downloads the image if it's not already in _attachments
    • Compresses images larger than 500KB while maintaining quality
    • Generates unique filenames based on content hash
  3. Organization: Creates an _attachments folder to store all images
  4. Updating: Updates markdown files to reference the local copies in _attachments

Features in Detail

Image Compression

  • Automatically compresses large images
  • Maintains reasonable quality through progressive compression
  • Converts RGBA images to RGB with white background

Filename Handling

  • Preserves original filenames
  • Sanitizes filenames for cross-platform compatibility

Concurrent Processing

  • Uses ThreadPoolExecutor for parallel downloads
  • Includes progress bar for tracking downloads
  • Implements rate limiting to prevent server overload

Error Handling

  • Comprehensive logging of all operations
  • Graceful handling of download failures
  • Skips already processed images

Logging

The script creates detailed logs in a logs directory:

  • Location: ./logs/image_downloader.log
  • Includes timestamps, operation details, and error messages
  • New log file created for each run

Limitations

  • Only processes image links in markdown format: ![alt](url)
  • Requires internet connection for downloading external images
  • May be rate-limited or just straight denied by some servers
  • SVG files are downloaded but not compressed

Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.

License

This project is available under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_image_downloader-0.1.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file markdown_image_downloader-0.1.3.tar.gz.

File metadata

File hashes

Hashes for markdown_image_downloader-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4965b1fbe3d7e85266096c04bb3fa9fa8a2c6c47d02cf9284346d634e3b73a96
MD5 d1d868705452404ff8a14d898552de40
BLAKE2b-256 b959ea2dafb5f784ac41d7af2b7bdd72862ba297360d72ab4976bb1bf3f5233a

See more details on using hashes here.

File details

Details for the file markdown_image_downloader-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_image_downloader-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e92e942cb431f3cde591792f21636e65d500442be3fcb3173a4f337ca768a8b7
MD5 d405c476c6765baf58b0e2a83b2ea222
BLAKE2b-256 313231ef16e5a7aba77e4827f034468567faad56366aaf57d79da70fd1368b80

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page