[Python-modules-commits] [python-imagesize] 01/01: Import python-imagesize_0.7.1.orig.tar.gz

Dmitry Shachnev mitya57 at moszumanska.debian.org
Sat Apr 30 12:46:17 UTC 2016


This is an automated email from the git hooks/post-receive script.

mitya57 pushed a commit to branch upstream
in repository python-imagesize.

commit 8d862dbeeb968e5f0a9cc744d894aeffb69dcb9b
Author: Dmitry Shachnev <mitya57 at gmail.com>
Date:   Sat Apr 30 15:41:36 2016 +0300

    Import python-imagesize_0.7.1.orig.tar.gz
---
 .gitignore            |   3 +++
 .travis.yml           |  11 +++++++++
 LICENSE.rst           |  19 ++++++++++++++++
 README.rst            |  61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 bench.py              |  34 ++++++++++++++++++++++++++++
 imagesize/__init__.py |   7 ++++++
 imagesize/get.py      |  60 +++++++++++++++++++++++++++++++++++++++++++++++++
 setup.cfg             |   4 ++++
 setup.py              |  39 ++++++++++++++++++++++++++++++++
 test/images/test.gif  | Bin 0 -> 49683 bytes
 test/images/test.jp2  | Bin 0 -> 114521 bytes
 test/images/test.jpg  | Bin 0 -> 200243 bytes
 test/images/test.png  | Bin 0 -> 125608 bytes
 test/test_get.py      |  27 ++++++++++++++++++++++
 14 files changed, 265 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b6e7535
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.pyc
+__pycache__
+.DS_Store
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..6a43ec0
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,11 @@
+language: python
+python:
+  - "2.6"
+  - "2.7"
+  - "3.3"
+  - "3.4"
+  - "3.5"
+  - "pypy"
+install:
+  - pip install nose
+script: nosetests
diff --git a/LICENSE.rst b/LICENSE.rst
new file mode 100644
index 0000000..58a2394
--- /dev/null
+++ b/LICENSE.rst
@@ -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.
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..abf7b4c
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,61 @@
+imagesize
+=============
+
+.. image:: https://travis-ci.org/shibukawa/imagesize_py.svg?branch=master
+    :target: https://travis-ci.org/shibukawa/imagesize_py
+
+This module analyzes jpeg/jpeg2000/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.
+
+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
+
+   - * module
+     * result
+   - * 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
+
+Thank you for feedbacks:
+
+* tk0miya (https://github.com/tk0miya)
+* shimizukawa (https://github.com/shimizukawa)
+* xantares (https://github.com/xantares)
+* Ivan Zakharyaschev (https://github.com/imz)
+
diff --git a/bench.py b/bench.py
new file mode 100644
index 0000000..e5ad9a9
--- /dev/null
+++ b/bench.py
@@ -0,0 +1,34 @@
+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")
+
+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 bench_purepython():
+    imagesize.get(imagepath)
+
+def bench_pil():
+    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()
diff --git a/imagesize/__init__.py b/imagesize/__init__.py
new file mode 100644
index 0000000..6535788
--- /dev/null
+++ b/imagesize/__init__.py
@@ -0,0 +1,7 @@
+import sys
+
+__all__ = ("get",)
+
+
+from .get import get
+
diff --git a/imagesize/get.py b/imagesize/get.py
new file mode 100644
index 0000000..7b5708d
--- /dev/null
+++ b/imagesize/get.py
@@ -0,0 +1,60 @@
+import struct
+
+def get(filepath):
+    """
+    Return (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
+            try:
+                width, height = struct.unpack("<hh", head[6:10])
+            except struct.error:
+                raise ValueError("Invalid GIF file")
+        # 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':
+            try:
+                width, height = struct.unpack(">LL", head[16:24])
+            except struct.error:
+                raise ValueError("Invalid PNG file")
+        # 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
+            try:
+                width, height = struct.unpack(">LL", head[8:16])
+            except struct.error:
+                raise ValueError("Invalid PNG file")
+        # 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 struct.error:
+                raise ValueError("Invalid JPEG file")
+        # handle JPEG2000s
+        elif size >= 12 and head.startswith(b'\x00\x00\x00\x0cjP  \r\n\x87\n'):
+            fhandle.seek(48)
+            try:
+                height, width = struct.unpack('>LL', fhandle.read(8))
+            except struct.error:
+                raise ValueError("Invalid JPEG2000 file")
+    return width, height
+
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..0ccba97
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,4 @@
+
+[wheel]
+universal = 1
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..b24e95b
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+from setuptools import setup
+#from distutils.core import setup
+
+setup(name='imagesize',
+      version='0.7.1',
+      description='Getting image size from png/jpeg/jpeg2000/gif file',
+      long_description='''
+It parses image files' header and return image size.
+
+* PNG
+* JPEG
+* JPEG2000
+* 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'
+     ]
+)
diff --git a/test/images/test.gif b/test/images/test.gif
new file mode 100644
index 0000000..4a2e175
Binary files /dev/null and b/test/images/test.gif differ
diff --git a/test/images/test.jp2 b/test/images/test.jp2
new file mode 100644
index 0000000..13b1104
Binary files /dev/null and b/test/images/test.jp2 differ
diff --git a/test/images/test.jpg b/test/images/test.jpg
new file mode 100644
index 0000000..b621a05
Binary files /dev/null and b/test/images/test.jpg differ
diff --git a/test/images/test.png b/test/images/test.png
new file mode 100644
index 0000000..b870bd8
Binary files /dev/null and b/test/images/test.png differ
diff --git a/test/test_get.py b/test/test_get.py
new file mode 100644
index 0000000..dfebeff
--- /dev/null
+++ b/test/test_get.py
@@ -0,0 +1,27 @@
+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 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_jpeg2000(self):
+        width, height = imagesize.get(os.path.join(imagedir, "test.jp2"))
+        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)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-imagesize.git



More information about the Python-modules-commits mailing list