A tool to download and organize images referenced in markdown files
Project description
Markdown Image Downloader
A Python package 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.
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:
- Scan all markdown files in the
../Readwise/Articlesfolder - Download any images referenced in the markdown files
- Store them in
../Readwise/Articles/_attachments - Update the markdown files to reference the local copies
Features
- Uses custom HTTP headers to avoid download blocks
- Downloads images from URLs referenced in markdown files
- Creates local copies of images in an
_attachmentsdirectory - 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
How It Works
- Scanning: The script scans all
.mdfiles in the specified folder for image references. - 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
- Downloads the image if it's not already in
- Organization: Creates an
_attachmentsfolder to store all images - 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:
 - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markdown_image_downloader-0.1.6.tar.gz.
File metadata
- Download URL: markdown_image_downloader-0.1.6.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b75d8e7f2e784d0cae18258101315337c0c7c53351bf6f7275bffbd245d7212
|
|
| MD5 |
43f745bd347e3a09f55fddb1d069d03c
|
|
| BLAKE2b-256 |
94510f9cc4d23e7535b0485f49d8cf8deb16b839384f657cede3d270e54c7c15
|
File details
Details for the file markdown_image_downloader-0.1.6-py3-none-any.whl.
File metadata
- Download URL: markdown_image_downloader-0.1.6-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a749245810cb818f0075e9c666e16124a5799a4c3c85197284c65a7bf69c3077
|
|
| MD5 |
c809b64722a1d5228d1b0099249577b1
|
|
| BLAKE2b-256 |
8a1f880e5c351516183ea0681906cc121e4956e2ac6f1ee2255ec6608ccbdde9
|