-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yoshiki Shibukawa
committed
Feb 1, 2016
0 parents
commit 0354c84
Showing
13 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.pyc | ||
__pycache__ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
The MIT License (MIT) | ||
---------------------------- | ||
|
||
Copyright © 2016 Yoshiki Shibukawa | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software | ||
and associated documentation files (the “Software”), to deal in the Software without restriction, | ||
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial | ||
portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH | ||
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
imagesize | ||
============= | ||
|
||
This module analyzes jpeg/png/gif image header and return image size. | ||
|
||
.. code:: python | ||
import imagesize | ||
width, height = imagesize.get("test.png") | ||
print(width, height) | ||
This module is pure python module (and it use PIL/Pillow for fallback). | ||
|
||
API | ||
----- | ||
|
||
* ``imagesize.get(filepath)`` | ||
|
||
Returns image size(width, height). | ||
|
||
Benchmark | ||
------------ | ||
|
||
It just parses only header, ignores pixel data. So it is much faster than Pillow. | ||
|
||
.. list-table | ||
:header-rows: 1 | ||
- * imagesize(pure python) | ||
* 1.077 seconds per 100000 times | ||
- * Pillow | ||
* 10.569 seconds per 100000 times | ||
I tested on MacBookPro(2014/Core i7) with 125kB PNG files. | ||
|
||
License | ||
----------- | ||
|
||
MIT License | ||
|
||
Thanks | ||
---------- | ||
|
||
I refers the following codes: | ||
|
||
* http://markasread.net/post/17551554979/get-image-size-info-using-pure-python-code | ||
* http://stackoverflow.com/questions/8032642/how-to-obtain-image-size-using-standard-python-class-without-using-external-lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import timeit | ||
import os, sys | ||
sys.path.insert(0, os.path.dirname(__file__)) | ||
import imagesize | ||
|
||
imagepath = os.path.join(os.path.dirname(__file__), "test", "images", "test.png") | ||
|
||
def bench_purepython(): | ||
imagesize.get_by_purepython(imagepath) | ||
|
||
def bench_pil(): | ||
imagesize.get_by_pil(imagepath) | ||
|
||
def bench(): | ||
print("pure python png") | ||
print(timeit.timeit('bench_purepython()', number=100000, setup="from __main__ import bench_purepython")) | ||
print("pil png") | ||
print(timeit.timeit('bench_pil()', number=100000, setup="from __main__ import bench_pil")) | ||
|
||
if __name__ == "__main__": | ||
bench() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import sys | ||
|
||
__all__ = ("get", "get_by_pil", "get_by_purepython") | ||
|
||
if sys.version_info[0] > 2: | ||
from .py3 import get_by_purepython, get_by_pil | ||
else: | ||
from py2 import get_by_purepython, get_by_pil | ||
|
||
def get(filepath): | ||
width, height = get_by_purepython(filepath) | ||
if width == -1 or height == -1: | ||
return get_by_pil(filepath) | ||
return width, height | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import struct | ||
|
||
try: | ||
from PIL import Image | ||
except ImportError: | ||
try: | ||
import Image | ||
except ImportError: | ||
Image = None | ||
|
||
def get_by_pil(filepath): | ||
img = Image.open(filepath) | ||
(width, height) = img.size | ||
return width, height | ||
|
||
def get_by_purepython(filepath): | ||
""" | ||
Return (content_type, width, height) for a given img file content | ||
no requirements | ||
""" | ||
height = -1 | ||
width = -1 | ||
|
||
with open(filepath, 'rb') as fhandle: | ||
head = fhandle.read(24) | ||
size = len(head) | ||
# handle GIFs | ||
if size >= 10 and head[:6] in ('GIF87a', 'GIF89a'): | ||
# Check to see if content_type is correct | ||
width, height = struct.unpack("<hh", head[6:10]) | ||
# see png edition spec bytes are below chunk length then and finally the | ||
elif size >= 24 and head.startswith('\211PNG\r\n\032\n') and head[12:16] == 'IHDR': | ||
width, height = struct.unpack(">LL", head[16:24]) | ||
# Maybe this is for an older PNG version. | ||
elif size >= 16 and head.startswith('\211PNG\r\n\032\n'): | ||
# Check to see if we have the right content type | ||
width, height = struct.unpack(">LL", head[8:16]) | ||
# handle JPEGs | ||
elif size >= 2 and head.startswith('\377\330'): | ||
try: | ||
fhandle.seek(0) # Read 0xff next | ||
size = 2 | ||
ftype = 0 | ||
while not 0xc0 <= ftype <= 0xcf: | ||
fhandle.seek(size, 1) | ||
byte = fhandle.read(1) | ||
while ord(byte) == 0xff: | ||
byte = fhandle.read(1) | ||
ftype = ord(byte) | ||
size = struct.unpack('>H', fhandle.read(2))[0] - 2 | ||
# We are at a SOFn block | ||
fhandle.seek(1, 1) # Skip `precision' byte. | ||
height, width = struct.unpack('>HH', fhandle.read(4)) | ||
except Exception: #IGNORE:W0703 | ||
return | ||
return width, height | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import struct | ||
|
||
def get_by_pil(filepath): | ||
try: | ||
from PIL import Image | ||
except ImportError: | ||
try: | ||
import Image | ||
except ImportError: | ||
Image = None | ||
img = Image.open(filepath) | ||
(width, height) = img.size | ||
return width, height | ||
|
||
def get_by_purepython(filepath): | ||
""" | ||
Return (content_type, width, height) for a given img file content | ||
no requirements | ||
""" | ||
height = -1 | ||
width = -1 | ||
|
||
with open(filepath, 'rb') as fhandle: | ||
head = fhandle.read(24) | ||
size = len(head) | ||
# handle GIFs | ||
if size >= 10 and head[:6] in (b'GIF87a', b'GIF89a'): | ||
# Check to see if content_type is correct | ||
width, height = struct.unpack("<hh", head[6:10]) | ||
# see png edition spec bytes are below chunk length then and finally the | ||
elif size >= 24 and head.startswith(b'\211PNG\r\n\032\n') and head[12:16] == b'IHDR': | ||
width, height = struct.unpack(">LL", head[16:24]) | ||
# Maybe this is for an older PNG version. | ||
elif size >= 16 and head.startswith(b'\211PNG\r\n\032\n'): | ||
# Check to see if we have the right content type | ||
width, height = struct.unpack(">LL", head[8:16]) | ||
# handle JPEGs | ||
elif size >= 2 and head.startswith(b'\377\330'): | ||
try: | ||
fhandle.seek(0) # Read 0xff next | ||
size = 2 | ||
ftype = 0 | ||
while not 0xc0 <= ftype <= 0xcf: | ||
fhandle.seek(size, 1) | ||
byte = fhandle.read(1) | ||
while ord(byte) == 0xff: | ||
byte = fhandle.read(1) | ||
ftype = ord(byte) | ||
size = struct.unpack('>H', fhandle.read(2))[0] - 2 | ||
# We are at a SOFn block | ||
fhandle.seek(1, 1) # Skip `precision' byte. | ||
height, width = struct.unpack('>HH', fhandle.read(4)) | ||
except Exception: #IGNORE:W0703 | ||
return | ||
return width, height | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
[wheel] | ||
universal = 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python | ||
|
||
from setuptools import setup | ||
#from distutils.core import setup | ||
|
||
setup(name='imagesize', | ||
version='0.5.0', | ||
description='Getting image size from png/jpeg/gif file', | ||
long_description=''' | ||
It parses image files' header and return image size. | ||
* PNG | ||
* JPEG | ||
* GIF | ||
This is a pure Python library. | ||
''', | ||
author='Yoshiki Shibukawa', | ||
author_email='yoshiki at shibu.jp', | ||
url='https://github.com/shibukawa/imagesize_py', | ||
license="MIT", | ||
packages=['imagesize'], | ||
package_dir={"imagesize": "imagesize"}, | ||
classifiers = [ | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: MIT License', | ||
'Programming Language :: Python', | ||
'Operating System :: OS Independent', | ||
'Programming Language :: Python :: 2.6', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3.3', | ||
'Programming Language :: Python :: 3.4', | ||
'Programming Language :: Python :: 3.5', | ||
'Programming Language :: Python :: Implementation :: PyPy', | ||
'Topic :: Multimedia :: Graphics' | ||
] | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import unittest | ||
import os, sys | ||
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) | ||
import imagesize | ||
|
||
imagedir = os.path.join(os.path.dirname(__file__), "images") | ||
|
||
class GetByPurePythonTest(unittest.TestCase): | ||
def test_load_png(self): | ||
width, height = imagesize.get_by_purepython(os.path.join(imagedir, "test.png")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
def test_load_jpeg(self): | ||
width, height = imagesize.get_by_purepython(os.path.join(imagedir, "test.jpg")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
def test_load_gif(self): | ||
width, height = imagesize.get_by_purepython(os.path.join(imagedir, "test.gif")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
class GetByPILTest(unittest.TestCase): | ||
def test_load_png(self): | ||
width, height = imagesize.get_by_pil(os.path.join(imagedir, "test.png")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
def test_load_jpeg(self): | ||
width, height = imagesize.get_by_pil(os.path.join(imagedir, "test.jpg")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
def test_load_gif(self): | ||
width, height = imagesize.get_by_pil(os.path.join(imagedir, "test.gif")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
class GetTest(unittest.TestCase): | ||
def test_load_png(self): | ||
width, height = imagesize.get(os.path.join(imagedir, "test.png")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
def test_load_jpeg(self): | ||
width, height = imagesize.get(os.path.join(imagedir, "test.jpg")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) | ||
|
||
def test_load_gif(self): | ||
width, height = imagesize.get(os.path.join(imagedir, "test.gif")) | ||
self.assertEqual(width, 802) | ||
self.assertEqual(height, 670) |