Registry / serialization / standard-imghdr

standard-imghdr

JSON →
library3.13.0pypypi✓ verified 21d ago

The `standard-imghdr` library is a redistribution of the `imghdr` module, which was formerly part of the Python standard library. It provides functionality to determine the type of an image file based on its header. Maintained by the `python-deadlib` project, this library serves as a compatibility layer for projects that relied on `imghdr` after its deprecation in Python 3.11 and subsequent removal in Python 3.13. The current version is 3.13.0 and it follows an infrequent release cadence focused on maintaining compatibility.

pip install standard-imghdr
INSTALL
IMPORT
SIG · STANDARD-IMGHDR
S
standard-imghdr
serializationpythonv3.13.0
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.13.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

imghdr
import imghdr
from imghdr import what
The original module and this redistribution are typically imported as a whole, then functions like `what` are accessed via `imghdr.what()`.

This quickstart demonstrates how to use `imghdr.what()` to identify image types from both a filename and a byte stream. It creates temporary dummy JPEG and PNG files, identifies their types, and then cleans up the files.

import imghdr import os # Create a dummy JPEG file for demonstration dummy_jpeg_data = b'\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xFF\xDB\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\x09\x09\x08\x0A\x0C\x14\x0D\x0C\x0B\x0B\x0C\x19\x12\x13\x0F\x14\x1D\x1A\x1F\x1E\x1D\x1A\x1C\x1C\x20\x24\x2E\x27\x20\x22\x2C\x23\x1C\x1C\x28\x37\x29\x2B\x30\x31\x30\x33\x31\x2F\x36\x2E\x37\x39\x3D\x3B\x3A\x32\x38\x37\x3D\x36\x36\x38\x37\x20\x20' dummy_png_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x0cIDATx\xda\xed\xc1\x01\x01\x00\x00\x00\xc2\xa0\xf7Om\x00\x00\x00\x00IEND\xaeB`\x82' # Create temporary files with open('test_image.jpg', 'wb') as f: f.write(dummy_jpeg_data) with open('test_image.png', 'wb') as f: f.write(dummy_png_data) # Determine image type by filename image_type_jpg = imghdr.what('test_image.jpg') print(f"Type of test_image.jpg: {image_type_jpg}") image_type_png = imghdr.what('test_image.png') print(f"Type of test_image.png: {image_type_png}") # Determine image type from a byte stream image_type_bytes = imghdr.what(None, h=dummy_jpeg_data) print(f"Type of image from bytes: {image_type_bytes}") # Clean up temporary files os.remove('test_image.jpg') os.remove('test_image.png')
Debug
Known issues
breakingThe `imghdr` module was deprecated in Python 3.11 and completely removed from the Python standard library in Python 3.13 (PEP 594). Direct `import imghdr` will result in a `ModuleNotFoundError` in Python 3.13 and newer versions unless `standard-imghdr` is installed.
fix
For existing code, install `standard-imghdr` (`pip install standard-imghdr`). For new code, consider using more robust and actively maintained alternatives like `Pillow`, `filetype`, `puremagic`, or `python-magic`.
affects: Python 3.13+
deprecatedThe `standard-imghdr` library is a 'dead battery' redistribution of a removed standard library module. It is not actively developed for new features or significant improvements. The project explicitly states that it is intended for 'minimal compatibility work' only.
fix
Avoid using `standard-imghdr` for new projects or new image handling logic. Migrate to actively maintained third-party libraries (e.g., `Pillow`, `filetype`) that offer more comprehensive and current image detection capabilities.
affects: All versions of `standard-imghdr` (reflects the status of the original `imghdr` module)
gotchaOn some Linux distributions, especially when trying to install system-wide without a virtual environment, `pip install standard-imghdr` might encounter issues or recommend `pipx`, which is not suitable for libraries.
fix
Always use virtual environments (`python -m venv .venv` followed by `source .venv/bin/activate`) when installing Python packages. If a system-wide installation is absolutely necessary and issues arise, a workaround like `pip install standard-imghdr --break-system-packages` might be needed on some systems (e.g., Arch Linux), but this is generally discouraged as it can break system Python packages.
affects: Potentially all versions, depending on Python and OS configuration.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'imghdr'
The `imghdr` module was removed from the Python standard library in Python 3.13 (and deprecated in 3.11), causing code relying on it to fail on newer Python versions.
fix
Install the `standard-imghdr` package using pip, which provides the original `imghdr` module: `pip install standard-imghdr`
ModuleNotFoundError: No module named 'standard_imghdr'
The `standard-imghdr` package is designed to re-provide the original `imghdr` module under its original name; it does not introduce a new module named `standard_imghdr`.
fix
After installing `standard-imghdr` with `pip install standard-imghdr`, continue to import the module using its original name: `import imghdr` or `from imghdr import what`.
ImportError: cannot import name 'what' from 'imghdr' (unknown location)
This error suggests that while the `imghdr` module might be located, the `what` function is not accessible within it, often due to a corrupted `standard-imghdr` installation or conflicting module paths.
fix
Reinstall `standard-imghdr` to ensure a clean and correct installation: `pip uninstall standard-imghdr && pip install standard-imghdr`. Verify your Python environment's module paths if the issue persists.
Upgrade
Version history
3.13.0latest on PyPI · released Oct 30, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
standard-imghdr — pip install standard-imghdr · libregistry