⚠️ This is a fork of scardine/image_size simply to create a distributable package on PyPI. Will be rendered unnecessary if image_size/issues/13 is addressed.
get_image_size
Get image width and height given a file path using minimal dependencies (no need for PIL, libjpeg, libpng, etc).
Why don’t you just use PIL?
PIL is huge and has lots of dependencies, perhaps an overkill if you want just the image dimensions. If you already have PIL installed, then sure, use it instead.
This was written in answer for the question “Get Image size WITHOUT loading image into memory” (using Python) in stackoverflow. The OP said:
@PauloScardine hey thanks Paulo, this is great! It’s always so nice to have code that can be easily deployed without bringing in dependencies, and this fits the bill! As you say, avoiding PIL is worth it for its own sake.
Usage
Right now only for PNG, JPEG, GIF, BMP and TIF. Very untested, fork and send PRs.
Python get_image_size.get_image_size usage:
import get_image_size
try:
width, height = get_image_size.get_image_size('/path/to/image.ext')
except get_image_size.UnknownImageFormat:
width, height = -1, -1
Python get_image_size.get_image_metadata usage:
import get_image_size
try:
img = get_image_size.get_image_metadata('/path/to/image.ext')
width, height = img.width, img.height
print(img._asdict())
except get_image_size.UnknownImageFormat:
width, height = -1, -1
Commandline get-image-size usage:
$ python -m get_image_size --help
$ get-image-size --help
Usage: get-image-size [-v|--verbose] [--json|--json-indent] <path0> [<pathN>]
Print metadata for the given image paths (without image library bindings).
Options:
-h, --help show this help message and exit
--json
--json-indent
-v, --verbose
-q, --quiet
-t, --test
$ get-image-size ./lookmanodeps.png
251 208 22228 PNG ./lookmanodeps.png
$ get-image-size --json-indent ./lookmanodeps.png
{
"path": "./lookmanodeps.png",
"type": "PNG",
"file_size": 22228,
"width": 251,
"height": 208
}
Updates
Over the time people sent updates for “.ico”, “.bmp” and other improvements. It is interesting to have all those algorithms in the same place so people can reimplement them using other languages, so thanks for all the pull requests (you can see the list of contributions in the history).
I added support for BMP file types, respecting different types of DIB headers. Should work on all current bitmap types, tested for old OS/2 and BITMAPCOREHEADER files, too. You might try it and consider it for merging if you find it useful. No longer under 100 LOC though, sorry ;)
License
MIT License
Thanks
Thanks everyone who inspired this, contributed with code or sent bug reports. You know who you are, THANK YOU!
HISTORY
Metadata
Release files for opsdroid-get-image-size 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| opsdroid_get_image_size-0.2.2.tar.gz | 22.8 kB | Without provenance | Details for opsdroid_get_image_size-0.2.2.tar.gz |
Release files / opsdroid_get_image_size-0.2.2.tar.gz
| Download URL | opsdroid_get_image_size-0.2.2.tar.gz |
|---|---|
| Size | 22.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0a9dadbec74267effce8317b151370c7976219c5162d8705c109ecee76179288
|
|
BLAKE2b-256 checksum How to use checksums |
c5981ac0cb475f90dee4d9c11a2ef3fe715438d569f71dc239ff8431f14f28fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
|
Security for opsdroid-get-image-size 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.