diff --git a/.travis.yml b/.travis.yml index 6f14035e8..a75c80873 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,6 @@ language: python notifications: irc: "chat.freenode.net#pil" -env: MAX_CONCURRENCY=4 - # Run slow PyPy* first, to give them a headstart and reduce waiting time. # Run latest 3.x and 2.x next, to get quick compatibility results. # Then run the remainder. @@ -22,9 +20,7 @@ install: - "travis_retry sudo apt-get -qq install libfreetype6-dev liblcms2-dev python-qt4 ghostscript libffi-dev libjpeg-turbo-progs cmake imagemagick" - "travis_retry pip install cffi" - "travis_retry pip install coverage nose" - - # Pyroma installation is slow on Py3, so just do it for Py2. - - if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then travis_retry pip install pyroma; fi + - "travis_retry pip install pyroma" - if [ "$TRAVIS_PYTHON_VERSION" == "2.6" ]; then travis_retry pip install unittest2; fi @@ -56,7 +52,6 @@ after_success: - travis_retry pip install coveralls-merge - coveralls-merge coverage.c.json - - travis_retry pip install pep8 pyflakes - pep8 --statistics --count PIL/*.py - pep8 --statistics --count Tests/*.py @@ -64,10 +59,53 @@ after_success: - pyflakes PIL/*.py | tee >(wc -l) - pyflakes Tests/*.py | tee >(wc -l) - # Coverage and quality reports on just the latest diff. # (Installation is very slow on Py3, so just do it for Py2.) - if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then Scripts/diffcover-install.sh; fi - if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then Scripts/diffcover-run.sh; fi + + # after_all + - | + if [ "$TRAVIS_REPO_SLUG" = "python-pillow/Pillow" ] && [ "$TRAVIS_BRANCH" = "master" ] && [ "$TRAVIS_PULL_REQUEST" = "false" ]; then + curl -Lo travis_after_all.py https://raw.github.com/dmakhno/travis_after_all/master/travis_after_all.py + python travis_after_all.py + export $(cat .to_export_back) + if [ "$BUILD_LEADER" = "YES" ]; then + if [ "$BUILD_AGGREGATE_STATUS" = "others_succeeded" ]; then + echo "All jobs succeded! Triggering OS X build..." + # Trigger an OS X build at the pillow-wheels repo + ./build_children.sh + else + echo "Some jobs failed" + fi + fi + fi + +after_failure: + - | + if [ "$TRAVIS_REPO_SLUG" = "python-pillow/Pillow" ] && [ "$TRAVIS_BRANCH" = "master" ] && [ "$TRAVIS_PULL_REQUEST" = "false" ]; then + curl -Lo travis_after_all.py https://raw.github.com/dmakhno/travis_after_all/master/travis_after_all.py + python travis_after_all.py + export $(cat .to_export_back) + if [ "$BUILD_LEADER" = "YES" ]; then + if [ "$BUILD_AGGREGATE_STATUS" = "others_failed" ]; then + echo "All jobs failed" + else + echo "Some jobs failed" + fi + fi + fi + +after_script: + - | + if [ "$TRAVIS_REPO_SLUG" = "python-pillow/Pillow" ] && [ "$TRAVIS_BRANCH" = "master" ] && [ "$TRAVIS_PULL_REQUEST" = "false" ]; then + echo leader=$BUILD_LEADER status=$BUILD_AGGREGATE_STATUS + fi + matrix: fast_finish: true + +env: + global: + # travis encrypt AUTH_TOKEN= + secure: "Vzm7aG1Qv0SDQcqiPzZMedNLn5ZmpL7IzF0DYnqcD+/l+zmKU22SnJBcX0uVXumo+r7eZfpsShpqfcdsZvMlvmQnwz+Y6AGKQru9tCKZbTMnuRjWKKXekC+tr8Xt9CKvRVtte5PyXW31paxUI3/e+fQGBwoFjEEC+6EpEOjeRfE=" diff --git a/CHANGES.rst b/CHANGES.rst index 679eaa73b..518a4244f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,14 +1,194 @@ Changelog (Pillow) ================== -2.7.0 (unreleased) +2.9.0 (Unreleased) ------------------ +- Provide n_frames attribute to multi-frame formats #1261 + [anntzer, radarhere] + +- Add duration and loop set to GifImagePlugin #1172 + [radarhere] + +- Ico files are little endian #1232 + [wiredfool] + +- Upgrade olefile from 0.30 to 0.42b #1226 + [radarhere, decalage2] + +- Setting transparency value to 0 when the tRNS contains only null byte(s) #1239 + [juztin] + +- Separated out feature checking from selftest #1233 + [radarhere] + +- Style/health fixes + [radarhere] + +- Update WebP from 0.4.1 to 0.4.3 #1235 + [radarhere] + +- Release GIL during image load (decode) #1224 + [lkesteloot] + +- Added icns save #1185 + [radarhere] + +- Fix putdata memory leak #1196 + [benoit-pierre] + +- Keep user-specified ordering of icon sizes #1193 + [karimbahgat] + +- Tiff: allow writing floating point tag values #1113 + [bpedersen2] + +2.8.2 (2015-06-06) +------------------ + +- Bug fix: Fixed Tiff handling of bad EXIF data + [radarhere] + +2.8.1 (2015-04-02) +------------------ + +- Bug fix: Catch struct.error on invalid JPEG, fixes #1163 + [wiredfool, hugovk] + +2.8.0 (2015-04-01) +------------------ + +- Fix 32-bit BMP loading (RGBA or RGBX) + [artscoop] + +- Fix UnboundLocalError in ImageFile #1131 + [davarisg] + +- Re-enable test image caching + [hugovk, homm] + +- Fix: Cannot identify EPS images, fixes #1104 + [hugovk] + +- Configure setuptools to run nosetests, fixes #729 + [aclark4life] + +- Style/health fixes + [radarhere, hugovk] + +- Add support for HTTP response objects to Image.open() + [mfitzp] + +- Improve reference docs for PIL.ImageDraw.Draw.pieslice() #1145 + [audreyr] + +- Added copy method font_variant() and accessible properties to truetype() #1123 + [radarhere] + +- Fix ImagingEffectNoise #1128 + [hugovk] + +- Remove unreachable code + [hugovk] + +- Let Python do the endian stuff + tests #1121 + [amoibos, radarhere] + +- Fix webp decode memory leak #1114 + [benoit-pierre] + +- Fast path for opaque pixels in RGBa unpacker #1088 + [bgilbert] + +- Enable basic support for 'RGBa' raw encoding/decoding #1096 + [immerrr] + +- Fix pickling L mode images with no palette, #1095 + [hugovk] + +- iPython display hook #1091 + [wiredfool] + +- Adjust buffer size when quality=keep, fixes #148 (again) + [wiredfool] + +- Fix for corrupted bitmaps embedded in truetype fonts. #1072 + [jackyyf, wiredfool] + +2.7.0 (2015-01-01) +------------------ + +- Split Sane into a separate repo: https://github.com/python-pillow/Sane + [hugovk] + +- Look for OSX and Linux fonts in common places. #1054 + [charleslaw] + +- Fix CVE-2014-9601, potential PNG decompression DOS #1060 + [wiredfool] + +- Use underscores, not spaces, in TIFF tag kwargs. #1044, #1058 + [anntzer, hugovk] + +- Update PSDraw for Python3, add tests. #1055 + [hugovk] + +- Use Bicubic filtering by default for thumbnails. Don't use Jpeg Draft mode for thumbnails. #1029 + [homm] + +- Fix MSVC compiler error: Use Py_ssize_t instead of ssize_t #1051 + [cgohlke] + +- Fix compiler error: MSVC needs variables defined at the start of the block #1048 + [cgohlke] + +- The GIF Palette optimization algorithm is only applicable to mode='P' or 'L' #993 + [moriyoshi] + +- Use PySide as an alternative to PyQt4/5. + [holg] + +- Replace affine-based im.resize implementation with convolution-based im.stretch #997 + [homm] + +- Replace Gaussian Blur implementation with iterated fast box blur. #961 Note: Radius parameter is interpreted differently than before. + [homm] + +- Better docs explaining import _imaging failure #1016, build #1017, mode #1018, PyAccess, PixelAccess objects #1019 Image.quantize #1020 and Image.save #1021 + [wiredfool] + +- Fix for saving TIFF image into an io.BytesIO buffer #1011 + [mfergie] + +- Fix antialias compilation on debug versions of Python #1010 + [wiredfool] + +- Fix for Image.putdata segfault #1009 + [wiredfool] + +- Ico save, additional tests #1007 + [exherb] + +- Use PyQt4 if it has already been imported, otherwise prefer PyQt5. #1003 + [AurelienBallier] + +- Speedup resample implementation up to 2.5 times. #977 + [homm] + +- Speed up rotation by using cache aware loops, added transpose to rotations. #994 + [homm] + +- Fix Bicubic interpolation #970 + [homm] + +- Support for 4-bit greyscale TIFF images #980 + [hugovk] + - Updated manifest #957 [wiredfool] - Fix PyPy 2.4 regression #952 - [wiredfool] + [wiredfool] - Webp Metadata Skip Test comments #954 [wiredfool] @@ -16,6 +196,15 @@ Changelog (Pillow) - Fixes for things rpmlint complains about #942 [manisandro] +2.6.2 (2015-01-01) +------------------ + +- Fix CVE-2014-9601, potential PNG decompression DOS #1060 + [wiredfool] + +- Fix Regression in PyPy 2.4 in streamio #958 + [wiredfool] + 2.6.1 (2014-10-11) ------------------ @@ -23,7 +212,7 @@ Changelog (Pillow) [wiredfool] - Fix manifest to include all test files. - [aclark] + [aclark4life] 2.6.0 (2014-10-01) ------------------ @@ -193,7 +382,7 @@ Changelog (Pillow) [wirefool] - Top level flake8 fixes #741 - [aclark] + [aclark4life] - Remove obsolete Animated Raster Graphics (ARG) support [hugovk] @@ -322,7 +511,7 @@ Changelog (Pillow) [larsmans] - Avoid conflicting _expand functions in PIL & MINGW, fixes #538 - [aclark] + [aclark4life] - Merge from Philippe Lagadec’s OleFileIO_PL fork [vadmium] @@ -737,13 +926,13 @@ Changelog (Pillow) [blueyed] - Package cleanup and additional documentation - [aclark] + [aclark4life] 1.7.4 (2011-07-21) ------------------ - Fix brown bag release - [aclark] + [aclark4life] 1.7.3 (2011-07-20) ------------------ @@ -755,19 +944,19 @@ Changelog (Pillow) ------------------ - Bug fix: Python 2.4 compat - [aclark] + [aclark4life] 1.7.1 (2011-05-31) ------------------ - More multi-arch support - [SteveM, regebro, barry, aclark] + [SteveM, regebro, barry, aclark4life] 1.7.0 (2011-05-27) ------------------ - Add support for multi-arch library directory /usr/lib/x86_64-linux-gnu - [aclark] + [aclark4life] 1.6 (12/01/2010) ---------------- @@ -776,28 +965,28 @@ Changelog (Pillow) [elro] - Doc fixes - [aclark] + [aclark4life] 1.5 (11/28/2010) ---------------- - Module and package fixes - [aclark] + [aclark4life] 1.4 (11/28/2010) ---------------- - Doc fixes - [aclark] + [aclark4life] 1.3 (11/28/2010) ---------------- - Add support for /lib64 and /usr/lib64 library directories on Linux - [aclark] + [aclark4life] - Doc fixes - [aclark] + [aclark4life] 1.2 (08/02/2010) ---------------- @@ -806,26 +995,29 @@ Changelog (Pillow) [jezdez] - Doc fixes - [aclark] + [aclark4life] 1.1 (07/31/2010) ---------------- - Removed setuptools_hg requirement - [aclark] + [aclark4life] - Doc fixes - [aclark] + [aclark4life] 1.0 (07/30/2010) ---------------- - Remove support for ``import Image``, etc. from the standard namespace. ``from PIL import Image`` etc. now required. - Forked PIL based on `Hanno Schlichting's re-packaging `_ - [aclark] + [aclark4life] .. Note:: What follows is the original PIL 1.1.7 CHANGES +0.2b5 - 1.1.7 (1995-2010) +------------------------- + :: -*- coding: utf-8 -*- @@ -1685,7 +1877,7 @@ Changelog (Pillow) (1.1.2c1 and 1.1.2 final released) + Adapted to Python 2.1. Among other things, all uses of the - "regex" module has been repleased with "re". + "regex" module have been replaced with "re". + Fixed attribute error when reading large PNG files (this bug was introduced in maintenance code released after the 1.1.1 @@ -2309,7 +2501,7 @@ Changelog (Pillow) the default value is 75. JPEG smooth smooth dithered images. value - is strengh (1-100). default is + is strength (1-100). default is off (0). PNG optimize minimize output file at the diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 30c375a17..5a49ab2d7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,25 +1,29 @@ -# Contributing +# Contributing to Pillow -## Fixes, Features and Changes +Bug fixes, feature additions, tests, documentation and more can be contributed via [issues](https://github.com/python-pillow/Pillow/issues) and/or [pull requests](https://github.com/python-pillow/Pillow/issues). All contributions are welcome. -Send a pull request. We'll generally want documentation and [tests](Tests/README.rst) for new features. Tests or documentation on their own are also welcomed. Feel free to ask questions as an [issue](https://github.com/python-pillow/Pillow/issues/new) or on IRC (irc.freenode.net, #pil) +## Bug fixes, feature additions, etc. -- Fork the repo -- Make a branch -- Add your changes + Tests -- Run the test suite. Try to run on both Python 2.x and 3.x, or you'll get tripped up. You can enable [Travis CI on your repo](https://travis-ci.org/profile/) to catch test failures prior to the pull request, and [Coveralls](https://coveralls.io/repos/new) to see if the changed code is covered by tests. -- Push to your fork, and make a pull request. +Please send a pull request to the master branch. Please include [documentation](http://pillow.readthedocs.org) and [tests](Tests/README.rst) for new features. Tests or documentation without bug fixes or feature additions are welcome too. Feel free to ask questions [via issues](https://github.com/python-pillow/Pillow/issues/new) or irc://irc.freenode.net#pil -A few guidelines: -- Try to keep any code commits clean and separate from reformatting commits. -- All new code is going to need tests. -- Try to follow PEP8. +- Fork the Pillow repository. +- Create a branch from master. +- Develop bug fixes, features, tests, etc. +- Run the test suite on both Python 2.x and 3.x. You can enable [Travis CI on your repo](https://travis-ci.org/profile/) to catch test failures prior to the pull request, and [Coveralls](https://coveralls.io/repos/new) to see if the changed code is covered by tests. +- Create a pull request to pull the changes from your branch to the Pillow master. -## Bugs +### Guidelines -When reporting bugs, please include example code that reproduces the issue, and if possible a problem image. The best reproductions are self-contained scripts that pull in as few dependencies as possible. An entire Django stack is harder to handle. +- Separate code commits from reformatting commits. +- Provide tests for any newly added code. +- Follow PEP8. + +## Reporting Issues + +When reporting issues, please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. The best reproductions are self-contained scripts with minimal dependencies. + +### Provide details -Let us know: - What did you do? - What did you expect to happen? - What actually happened? diff --git a/docs/LICENSE b/LICENSE similarity index 100% rename from docs/LICENSE rename to LICENSE diff --git a/MANIFEST.in b/MANIFEST.in index 292421671..4fb47c637 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,21 +2,17 @@ include *.c include *.h include *.md include *.py +include *.sh include *.rst include *.txt include *.yaml include .coveragerc include .gitattributes include .travis.yml +include LICENSE include Makefile include tox.ini recursive-include PIL *.md -recursive-include Sane *.c -recursive-include Sane *.py -recursive-include Sane *.rst -recursive-include Sane *.txt -recursive-include Sane CHANGES -recursive-include Sane README.rst recursive-include Scripts *.py recursive-include Scripts *.rst recursive-include Scripts *.sh @@ -64,6 +60,7 @@ recursive-include Tests *.ttf recursive-include Tests *.txt recursive-include Tests *.webp recursive-include Tests *.xpm +recursive-include Tests *.msp recursive-include Tk *.c recursive-include Tk *.rst recursive-include depends *.rst diff --git a/Makefile b/Makefile index 98e0c647a..4d96c497d 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,5 @@ -.PHONY: pre clean install test inplace coverage test-dep help docs livedocs - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " clean remove build products" - @echo " install make and install" - @echo " test run tests on installed pillow" - @echo " inplace make inplace extension" - @echo " coverage run coverage test (in progress)" - @echo " docs make html docs" - @echo " docserver run an http server on the docs directory" - @echo " test-dep install coveraget and test dependencies" - -pre: - virtualenv . - bin/pip install -r requirements.txt - bin/python setup.py develop - bin/python selftest.py - bin/nosetests Tests/test_*.py - bin/python setup.py install - bin/python test-installed.py - check-manifest - pyroma . - viewdoc +# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html +.PHONY: clean coverage doc docserve help inplace install install-req release-test sdist test upload upload-test clean: python setup.py clean @@ -30,32 +7,70 @@ clean: rm -r build || true find . -name __pycache__ | xargs rm -r || true -install: - python setup.py install - python selftest.py --installed - -test: - python test-installed.py - -inplace: clean - python setup.py build_ext --inplace - coverage: -# requires nose-cov coverage erase coverage run --parallel-mode --include=PIL/* selftest.py nosetests --with-cov --cov='PIL/' --cov-report=html Tests/test_*.py -# doesn't combine properly before report, -# writing report instead of displaying invalid report +# Doesn't combine properly before report, writing report instead of displaying invalid report. rm -r htmlcov || true coverage combine coverage report -test-dep: - pip install coveralls nose nose-cov pep8 pyflakes - -docs: +doc: $(MAKE) -C docs html -docserver: - cd docs/_build/html && python -mSimpleHTTPServer 2> /dev/null& \ No newline at end of file +docserve: + cd docs/_build/html && python -mSimpleHTTPServer 2> /dev/null& + +help: + @echo "Welcome to Pillow development. Please use \`make ' where is one of" + @echo " clean remove build products" + @echo " coverage run coverage test (in progress)" + @echo " doc make html docs" + @echo " docserve run an http server on the docs directory" + @echo " html to make standalone HTML files" + @echo " inplace make inplace extension" + @echo " install make and install" + @echo " install-req install documentation and test dependencies" + @echo " release-test run code and package tests before release" + @echo " test run tests on installed pillow" + @echo " upload build and upload sdists to PyPI" + @echo " upload-test build and upload sdists to test.pythonpackages.com" + +inplace: clean + python setup.py build_ext --inplace + +install: + python setup.py install + python selftest.py --installed + +install-req: + pip install -r requirements.txt + +release-test: + $(MAKE) install-req + python setup.py develop + python selftest.py + nosetests Tests/test_*.py + python setup.py install + python test-installed.py + check-manifest + pyroma . + viewdoc + +sdist: + python setup.py sdist --format=gztar,zip + +test: + python test-installed.py + +# https://docs.python.org/2/distutils/packageindex.html#the-pypirc-file +upload-test: +# [test] +# username: +# password: +# repository = http://test.pythonpackages.com + python setup.py sdist --format=gztar,zip upload -r test + +upload: + python setup.py sdist --format=gztar,zip upload diff --git a/PIL/BdfFontFile.py b/PIL/BdfFontFile.py index c52812450..0c1614e0f 100644 --- a/PIL/BdfFontFile.py +++ b/PIL/BdfFontFile.py @@ -69,8 +69,8 @@ def bdf_char(f): bitmap.append(s[:-1]) bitmap = b"".join(bitmap) - [x, y, l, d] = [int(s) for s in props["BBX"].split()] - [dx, dy] = [int(s) for s in props["DWIDTH"].split()] + [x, y, l, d] = [int(p) for p in props["BBX"].split()] + [dx, dy] = [int(p) for p in props["DWIDTH"].split()] bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y) diff --git a/PIL/BmpImagePlugin.py b/PIL/BmpImagePlugin.py index 917d43b9c..30ca10971 100644 --- a/PIL/BmpImagePlugin.py +++ b/PIL/BmpImagePlugin.py @@ -30,6 +30,7 @@ __version__ = "0.7" from PIL import Image, ImageFile, ImagePalette, _binary import math + i8 = _binary.i8 i16 = _binary.i16le i32 = _binary.i32le @@ -48,7 +49,7 @@ BIT2MODE = { 8: ("P", "P"), 16: ("RGB", "BGR;15"), 24: ("RGB", "BGR"), - 32: ("RGB", "BGRX") + 32: ("RGB", "BGRX"), } @@ -56,131 +57,146 @@ def _accept(prefix): return prefix[:2] == b"BM" -## +# ============================================================================== # Image plugin for the Windows BMP format. - +# ============================================================================== class BmpImageFile(ImageFile.ImageFile): + """ Image plugin for the Windows Bitmap format (BMP) """ - format = "BMP" + # -------------------------------------------------------------- Description format_description = "Windows Bitmap" + format = "BMP" + # --------------------------------------------------- BMP Compression values + COMPRESSIONS = {'RAW': 0, 'RLE8': 1, 'RLE4': 2, 'BITFIELDS': 3, 'JPEG': 4, 'PNG': 5} + RAW, RLE8, RLE4, BITFIELDS, JPEG, PNG = 0, 1, 2, 3, 4, 5 def _bitmap(self, header=0, offset=0): + """ Read relevant info about the BMP """ + read, seek = self.fp.read, self.fp.seek if header: - self.fp.seek(header) - - read = self.fp.read - - # CORE/INFO - s = read(4) - s = s + ImageFile._safe_read(self.fp, i32(s)-4) - - if len(s) == 12: - - # OS/2 1.0 CORE - bits = i16(s[10:]) - self.size = i16(s[4:]), i16(s[6:]) - compression = 0 - lutsize = 3 - colors = 0 - direction = -1 - - elif len(s) in [40, 64, 108, 124]: - - # WIN 3.1 or OS/2 2.0 INFO - bits = i16(s[14:]) - self.size = i32(s[4:]), i32(s[8:]) - compression = i32(s[16:]) - pxperm = (i32(s[24:]), i32(s[28:])) # Pixels per meter - lutsize = 4 - colors = i32(s[32:]) - direction = -1 - if i8(s[11]) == 0xff: - # upside-down storage - self.size = self.size[0], 2**32 - self.size[1] - direction = 0 - - self.info["dpi"] = tuple(map(lambda x: math.ceil(x / 39.3701), - pxperm)) - + seek(header) + file_info = dict() + file_info['header_size'] = i32(read(4)) # read bmp header size @offset 14 (this is part of the header size) + file_info['direction'] = -1 + # --------------------- If requested, read header at a specific position + header_data = ImageFile._safe_read(self.fp, file_info['header_size'] - 4) # read the rest of the bmp header, without its size + # --------------------------------------------------- IBM OS/2 Bitmap v1 + # ------ This format has different offsets because of width/height types + if file_info['header_size'] == 12: + file_info['width'] = i16(header_data[0:2]) + file_info['height'] = i16(header_data[2:4]) + file_info['planes'] = i16(header_data[4:6]) + file_info['bits'] = i16(header_data[6:8]) + file_info['compression'] = self.RAW + file_info['palette_padding'] = 3 + # ---------------------------------------------- Windows Bitmap v2 to v5 + elif file_info['header_size'] in (40, 64, 108, 124): # v3, OS/2 v2, v4, v5 + if file_info['header_size'] >= 40: # v3 and OS/2 + file_info['y_flip'] = i8(header_data[7]) == 0xff + file_info['direction'] = 1 if file_info['y_flip'] else -1 + file_info['width'] = i32(header_data[0:4]) + file_info['height'] = i32(header_data[4:8]) if not file_info['y_flip'] else 2**32 - i32(header_data[4:8]) + file_info['planes'] = i16(header_data[8:10]) + file_info['bits'] = i16(header_data[10:12]) + file_info['compression'] = i32(header_data[12:16]) + file_info['data_size'] = i32(header_data[16:20]) # byte size of pixel data + file_info['pixels_per_meter'] = (i32(header_data[20:24]), i32(header_data[24:28])) + file_info['colors'] = i32(header_data[28:32]) + file_info['palette_padding'] = 4 + self.info["dpi"] = tuple(map(lambda x: math.ceil(x / 39.3701), file_info['pixels_per_meter'])) + if file_info['compression'] == self.BITFIELDS: + if len(header_data) >= 52: + for idx, mask in enumerate(['r_mask', 'g_mask', 'b_mask', 'a_mask']): + file_info[mask] = i32(header_data[36+idx*4:40+idx*4]) + else: + for mask in ['r_mask', 'g_mask', 'b_mask', 'a_mask']: + file_info[mask] = i32(read(4)) + file_info['rgb_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask']) + file_info['rgba_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask'], file_info['a_mask']) else: - raise IOError("Unsupported BMP header type (%d)" % len(s)) - - if (self.size[0]*self.size[1]) > 2**31: - # Prevent DOS for > 2gb images + raise IOError("Unsupported BMP header type (%d)" % file_info['header_size']) + # ------------------ Special case : header is reported 40, which + # ---------------------- is shorter than real size for bpp >= 16 + self.size = file_info['width'], file_info['height'] + # -------- If color count was not found in the header, compute from bits + file_info['colors'] = file_info['colors'] if file_info.get('colors', 0) else (1 << file_info['bits']) + # -------------------------------- Check abnormal values for DOS attacks + if file_info['width'] * file_info['height'] > 2**31: raise IOError("Unsupported BMP Size: (%dx%d)" % self.size) - - if not colors: - colors = 1 << bits - - # MODE - try: - self.mode, rawmode = BIT2MODE[bits] - except KeyError: - raise IOError("Unsupported BMP pixel depth (%d)" % bits) - - if compression == 3: - # BI_BITFIELDS compression - mask = i32(read(4)), i32(read(4)), i32(read(4)) - if bits == 32 and mask == (0xff0000, 0x00ff00, 0x0000ff): - rawmode = "BGRX" - elif bits == 16 and mask == (0x00f800, 0x0007e0, 0x00001f): - rawmode = "BGR;16" - elif bits == 16 and mask == (0x007c00, 0x0003e0, 0x00001f): - rawmode = "BGR;15" - else: - # print bits, map(hex, mask) - raise IOError("Unsupported BMP bitfields layout") - elif compression != 0: - raise IOError("Unsupported BMP compression (%d)" % compression) - - # LUT - if self.mode == "P": - palette = [] - greyscale = 1 - if colors == 2: - indices = (0, 255) - elif colors > 2**16 or colors <= 0: # We're reading a i32. - raise IOError("Unsupported BMP Palette size (%d)" % colors) - else: - indices = list(range(colors)) - for i in indices: - rgb = read(lutsize)[:3] - if rgb != o8(i)*3: - greyscale = 0 - palette.append(rgb) - if greyscale: - if colors == 2: - self.mode = rawmode = "1" + # ----------------------- Check bit depth for unusual unsupported values + self.mode, raw_mode = BIT2MODE.get(file_info['bits'], (None, None)) + if self.mode is None: + raise IOError("Unsupported BMP pixel depth (%d)" % file_info['bits']) + # ----------------- Process BMP with Bitfields compression (not palette) + if file_info['compression'] == self.BITFIELDS: + SUPPORTED = { + 32: [(0xff0000, 0xff00, 0xff, 0x0), (0xff0000, 0xff00, 0xff, 0xff000000), (0x0, 0x0, 0x0, 0x0)], + 24: [(0xff0000, 0xff00, 0xff)], + 16: [(0xf800, 0x7e0, 0x1f), (0x7c00, 0x3e0, 0x1f)]} + MASK_MODES = { + (32, (0xff0000, 0xff00, 0xff, 0x0)): "BGRX", (32, (0xff0000, 0xff00, 0xff, 0xff000000)): "BGRA", (32, (0x0, 0x0, 0x0, 0x0)): "BGRA", + (24, (0xff0000, 0xff00, 0xff)): "BGR", + (16, (0xf800, 0x7e0, 0x1f)): "BGR;16", (16, (0x7c00, 0x3e0, 0x1f)): "BGR;15"} + if file_info['bits'] in SUPPORTED: + if file_info['bits'] == 32 and file_info['rgba_mask'] in SUPPORTED[file_info['bits']]: + raw_mode = MASK_MODES[(file_info['bits'], file_info['rgba_mask'])] + self.mode = "RGBA" if raw_mode in ("BGRA",) else self.mode + elif file_info['bits'] in (24, 16) and file_info['rgb_mask'] in SUPPORTED[file_info['bits']]: + raw_mode = MASK_MODES[(file_info['bits'], file_info['rgb_mask'])] else: - self.mode = rawmode = "L" + raise IOError("Unsupported BMP bitfields layout") else: - self.mode = "P" - self.palette = ImagePalette.raw( - "BGR", b"".join(palette) - ) + raise IOError("Unsupported BMP bitfields layout") + elif file_info['compression'] == self.RAW: + if file_info['bits'] == 32 and header == 22: # 32-bit .cur offset + raw_mode, self.mode = "BGRA", "RGBA" + else: + raise IOError("Unsupported BMP compression (%d)" % file_info['compression']) + # ---------------- Once the header is processed, process the palette/LUT + if self.mode == "P": # Paletted for 1, 4 and 8 bit images + # ----------------------------------------------------- 1-bit images + if not (0 < file_info['colors'] <= 65536): + raise IOError("Unsupported BMP Palette size (%d)" % file_info['colors']) + else: + padding = file_info['palette_padding'] + palette = read(padding * file_info['colors']) + greyscale = True + indices = (0, 255) if file_info['colors'] == 2 else list(range(file_info['colors'])) + # ------------------ Check if greyscale and ignore palette if so + for ind, val in enumerate(indices): + rgb = palette[ind*padding:ind*padding + 3] + if rgb != o8(val) * 3: + greyscale = False + # -------- If all colors are grey, white or black, ditch palette + if greyscale: + self.mode = "1" if file_info['colors'] == 2 else "L" + raw_mode = self.mode + else: + self.mode = "P" + self.palette = ImagePalette.raw("BGRX" if padding == 4 else "BGR", palette) - if not offset: - offset = self.fp.tell() - - self.tile = [("raw", - (0, 0) + self.size, - offset, - (rawmode, ((self.size[0]*bits+31) >> 3) & (~3), - direction))] - - self.info["compression"] = compression + # ----------------------------- Finally set the tile data for the plugin + self.info['compression'] = file_info['compression'] + self.tile = [('raw', (0, 0, file_info['width'], file_info['height']), offset or self.fp.tell(), + (raw_mode, ((file_info['width'] * file_info['bits'] + 31) >> 3) & (~3), file_info['direction']) + )] def _open(self): - - # HEAD - s = self.fp.read(14) - if s[:2] != b"BM": + """ Open file, check magic number and read header """ + # read 14 bytes: magic number, filesize, reserved, header final offset + head_data = self.fp.read(14) + # choke if the file does not have the required magic bytes + if head_data[0:2] != b"BM": raise SyntaxError("Not a BMP file") - offset = i32(s[10:]) - + # read the start position of the BMP image data (u32) + offset = i32(head_data[10:14]) + # load bitmap information (offset=raster info) self._bitmap(offset=offset) +# ============================================================================== +# Image plugin for the DIB format (BMP alias) +# ============================================================================== class DibImageFile(BmpImageFile): format = "DIB" @@ -198,6 +214,7 @@ SAVE = { "L": ("L", 8, 256), "P": ("P", 8, 256), "RGB": ("BGR", 24, 0), + "RGBA": ("BGRA", 32, 0), } diff --git a/PIL/ContainerIO.py b/PIL/ContainerIO.py index dcedcd6dd..262f2afb9 100644 --- a/PIL/ContainerIO.py +++ b/PIL/ContainerIO.py @@ -19,7 +19,7 @@ # file (for example a TAR file). -class ContainerIO: +class ContainerIO(object): ## # Create file object. diff --git a/PIL/DcxImagePlugin.py b/PIL/DcxImagePlugin.py index 0940b3935..978c90e80 100644 --- a/PIL/DcxImagePlugin.py +++ b/PIL/DcxImagePlugin.py @@ -62,6 +62,10 @@ class DcxImageFile(PcxImageFile): self.__fp = self.fp self.seek(0) + @property + def n_frames(self): + return len(self._offset) + def seek(self, frame): if frame >= len(self._offset): raise EOFError("attempt to seek outside DCX directory") diff --git a/PIL/EpsImagePlugin.py b/PIL/EpsImagePlugin.py index d077ca1ab..7b1f4c1ca 100644 --- a/PIL/EpsImagePlugin.py +++ b/PIL/EpsImagePlugin.py @@ -157,7 +157,7 @@ def Ghostscript(tile, size, fp, scale=1): return im -class PSFile: +class PSFile(object): """ Wrapper for bytesio object that treats either CR or LF as end of line. """ @@ -248,7 +248,7 @@ class EpsImageFile(ImageFile.ImageFile): # Note: The DSC spec says that BoundingBox # fields should be integers, but some drivers # put floating point values there anyway. - box = [int(float(s)) for s in v.split()] + box = [int(float(i)) for i in v.split()] self.size = box[2] - box[0], box[3] - box[1] self.tile = [("eps", (0, 0) + self.size, offset, (length, box))] @@ -275,20 +275,20 @@ class EpsImageFile(ImageFile.ImageFile): s = fp.readline().strip('\r\n') - if s[0] != "%": + if s[:1] != "%": break # # Scan for an "ImageData" descriptor - while s[0] == "%": + while s[:1] == "%": if len(s) > 255: raise SyntaxError("not an EPS file") if s[:11] == "%ImageData:": # Encoded bitmapped image. - [x, y, bi, mo, z3, z4, en, id] = s[11:].split(None, 7) + x, y, bi, mo = s[11:].split(None, 7)[:4] if int(bi) != 8: break @@ -365,7 +365,7 @@ def _save(im, fp, filename, eps=1): else: raise ValueError("image mode is not supported") - class NoCloseStream: + class NoCloseStream(object): def __init__(self, fp): self.fp = fp diff --git a/PIL/FitsStubImagePlugin.py b/PIL/FitsStubImagePlugin.py index 0b851ae59..7aefff212 100644 --- a/PIL/FitsStubImagePlugin.py +++ b/PIL/FitsStubImagePlugin.py @@ -18,6 +18,7 @@ _handler = None # # @param handler Handler object. + def register_handler(handler): global _handler _handler = handler @@ -25,9 +26,11 @@ def register_handler(handler): # -------------------------------------------------------------------- # Image adapter + def _accept(prefix): return prefix[:6] == b"SIMPLE" + class FITSStubImageFile(ImageFile.StubImageFile): format = "FITS" diff --git a/PIL/FliImagePlugin.py b/PIL/FliImagePlugin.py index 4ecaccdc4..0660ddeb6 100644 --- a/PIL/FliImagePlugin.py +++ b/PIL/FliImagePlugin.py @@ -86,9 +86,10 @@ class FliImageFile(ImageFile.ImageFile): self.palette = ImagePalette.raw("RGB", b"".join(palette)) # set things up to decode first frame - self.frame = -1 + self.__frame = -1 self.__fp = self.fp - + self.__rewind = self.fp.tell() + self._n_frames = None self.seek(0) def _palette(self, palette, shift): @@ -109,11 +110,35 @@ class FliImageFile(ImageFile.ImageFile): palette[i] = (r, g, b) i += 1 - def seek(self, frame): + @property + def n_frames(self): + if self._n_frames is None: + current = self.tell() + try: + while True: + self.seek(self.tell() + 1) + except EOFError: + self._n_frames = self.tell() + 1 + self.seek(current) + return self._n_frames - if frame != self.frame + 1: + def seek(self, frame): + if frame == self.__frame: + return + if frame < self.__frame: + self._seek(0) + for f in range(self.__frame + 1, frame + 1): + self._seek(f) + + def _seek(self, frame): + if frame == 0: + self.__frame = -1 + self.__fp.seek(self.__rewind) + self.__offset = 128 + + if frame != self.__frame + 1: raise ValueError("cannot seek to frame %d" % frame) - self.frame = frame + self.__frame = frame # move to next frame self.fp = self.__fp @@ -128,11 +153,10 @@ class FliImageFile(ImageFile.ImageFile): self.decodermaxblock = framesize self.tile = [("fli", (0, 0)+self.size, self.__offset, None)] - self.__offset = self.__offset + framesize + self.__offset += framesize def tell(self): - - return self.frame + return self.__frame # # registry diff --git a/PIL/FontFile.py b/PIL/FontFile.py index 26ddff0ac..db8e6bec1 100644 --- a/PIL/FontFile.py +++ b/PIL/FontFile.py @@ -17,11 +17,6 @@ import os from PIL import Image, _binary -try: - import zlib -except ImportError: - zlib = None - WIDTH = 800 @@ -36,7 +31,7 @@ def puti16(fp, values): ## # Base class for raster font file handlers. -class FontFile: +class FontFile(object): bitmap = None @@ -83,7 +78,8 @@ class FontFile: glyph = self[i] if glyph: d, dst, src, im = glyph - xx, yy = src[2] - src[0], src[3] - src[1] + xx = src[2] - src[0] + # yy = src[3] - src[1] x0, y0 = x, y x = x + xx if x > WIDTH: diff --git a/PIL/FpxImagePlugin.py b/PIL/FpxImagePlugin.py index 7e1d09dc8..9d338d9da 100644 --- a/PIL/FpxImagePlugin.py +++ b/PIL/FpxImagePlugin.py @@ -20,7 +20,7 @@ __version__ = "0.1" from PIL import Image, ImageFile -from PIL.OleFileIO import * +from PIL.OleFileIO import i8, i32, MAGIC, OleFileIO # we map from colour field tuples to (mode, rawmode) descriptors @@ -130,15 +130,15 @@ class FpxImageFile(ImageFile.ImageFile): fp = self.ole.openstream(stream) # skip prefix - p = fp.read(28) + fp.read(28) # header stream s = fp.read(36) size = i32(s, 4), i32(s, 8) - tilecount = i32(s, 12) + # tilecount = i32(s, 12) tilesize = i32(s, 16), i32(s, 20) - channels = i32(s, 24) + # channels = i32(s, 24) offset = i32(s, 28) length = i32(s, 32) diff --git a/PIL/GbrImagePlugin.py b/PIL/GbrImagePlugin.py index 0e686326c..e1580e718 100644 --- a/PIL/GbrImagePlugin.py +++ b/PIL/GbrImagePlugin.py @@ -39,8 +39,8 @@ class GbrImageFile(ImageFile.ImageFile): width = i32(self.fp.read(4)) height = i32(self.fp.read(4)) - bytes = i32(self.fp.read(4)) - if width <= 0 or height <= 0 or bytes != 1: + color_depth = i32(self.fp.read(4)) + if width <= 0 or height <= 0 or color_depth != 1: raise SyntaxError("not a GIMP brush") comment = self.fp.read(header_size - 20)[:-1] diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 0b154f417..150773b67 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -24,13 +24,11 @@ # See the README file for information on usage and redistribution. # +from PIL import Image, ImageFile, ImagePalette, _binary __version__ = "0.9" -from PIL import Image, ImageFile, ImagePalette, _binary - - # -------------------------------------------------------------------- # Helpers @@ -89,9 +87,30 @@ class GifImageFile(ImageFile.ImageFile): self.__fp = self.fp # FIXME: hack self.__rewind = self.fp.tell() - self.seek(0) # get ready to read first frame + self._n_frames = None + self._seek(0) # get ready to read first frame + + @property + def n_frames(self): + if self._n_frames is None: + current = self.tell() + try: + while True: + self.seek(self.tell() + 1) + except EOFError: + self._n_frames = self.tell() + 1 + self.seek(current) + return self._n_frames def seek(self, frame): + if frame == self.__frame: + return + if frame < self.__frame: + self._seek(0) + for f in range(self.__frame + 1, frame + 1): + self._seek(f) + + def _seek(self, frame): if frame == 0: # rewind @@ -271,7 +290,7 @@ def _save(im, fp, filename): pass # write uncompressed file if im.mode in RAWMODE: - imOut = im + im_out = im else: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) @@ -279,9 +298,9 @@ def _save(im, fp, filename): palette_size = 256 if im.palette: palette_size = len(im.palette.getdata()[1]) // 3 - imOut = im.convert("P", palette=1, colors=palette_size) + im_out = im.convert("P", palette=1, colors=palette_size) else: - imOut = im.convert("L") + im_out = im.convert("L") # header try: @@ -290,63 +309,16 @@ def _save(im, fp, filename): palette = None im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) - header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo) + header, used_palette_colors = getheader(im_out, palette, im.encoderinfo) for s in header: fp.write(s) - flags = 0 - - try: - interlace = im.encoderinfo["interlace"] - except KeyError: - interlace = 1 - - # workaround for @PIL153 - if min(im.size) < 16: - interlace = 0 - - if interlace: - flags = flags | 64 - - try: - transparency = im.encoderinfo["transparency"] - except KeyError: - pass - else: - transparency = int(transparency) - # optimize the block away if transparent color is not used - transparentColorExists = True - # adjust the transparency index after optimize - if usedPaletteColors is not None and len(usedPaletteColors) < 256: - for i in range(len(usedPaletteColors)): - if usedPaletteColors[i] == transparency: - transparency = i - transparentColorExists = True - break - else: - transparentColorExists = False - - # transparency extension block - if transparentColorExists: - fp.write(b"!" + - o8(249) + # extension intro - o8(4) + # length - o8(1) + # transparency info present - o16(0) + # duration - o8(transparency) # transparency index - + o8(0)) - # local image header - fp.write(b"," + - o16(0) + o16(0) + # bounding box - o16(im.size[0]) + # size - o16(im.size[1]) + - o8(flags) + # flags - o8(8)) # bits + get_local_header(fp, im) - imOut.encoderconfig = (8, interlace) - ImageFile._save(imOut, fp, [("gif", (0, 0)+im.size, 0, - RAWMODE[imOut.mode])]) + im_out.encoderconfig = (8, get_interlace(im)) + ImageFile._save(im_out, fp, [("gif", (0, 0)+im.size, 0, + RAWMODE[im_out.mode])]) fp.write(b"\0") # end of image data @@ -358,6 +330,85 @@ def _save(im, fp, filename): pass +def get_interlace(im): + try: + interlace = im.encoderinfo["interlace"] + except KeyError: + interlace = 1 + + # workaround for @PIL153 + if min(im.size) < 16: + interlace = 0 + + return interlace + + +def get_local_header(fp, im, offset=(0, 0)): + transparent_color_exists = False + try: + transparency = im.encoderinfo["transparency"] + except KeyError: + pass + else: + transparency = int(transparency) + # optimize the block away if transparent color is not used + transparent_color_exists = True + + if _get_optimize(im, im.encoderinfo): + used_palette_colors = _get_used_palette_colors(im) + + # adjust the transparency index after optimize + if len(used_palette_colors) < 256: + for i in range(len(used_palette_colors)): + if used_palette_colors[i] == transparency: + transparency = i + transparent_color_exists = True + break + else: + transparent_color_exists = False + + if "duration" in im.encoderinfo: + duration = int(im.encoderinfo["duration"] / 10) + else: + duration = 0 + if transparent_color_exists or duration != 0: + transparency_flag = 1 if transparent_color_exists else 0 + if not transparent_color_exists: + transparency = 0 + + fp.write(b"!" + + o8(249) + # extension intro + o8(4) + # length + o8(transparency_flag) + # transparency info present + o16(duration) + # duration + o8(transparency) + # transparency index + o8(0)) + + if "loop" in im.encoderinfo: + number_of_loops = im.encoderinfo["loop"] + fp.write(b"!" + + o8(255) + # extension intro + o8(11) + + b"NETSCAPE2.0" + + o8(3) + + o8(1) + + o16(number_of_loops) + # number of loops + o8(0)) + + flags = 0 + + if get_interlace(im): + flags = flags | 64 + + fp.write(b"," + + o16(offset[0]) + # offset + o16(offset[1]) + + o16(im.size[0]) + # size + o16(im.size[1]) + + o8(flags) + # flags + o8(8)) # bits + + def _save_netpbm(im, fp, filename): # @@ -407,11 +458,26 @@ def _save_netpbm(im, fp, filename): # -------------------------------------------------------------------- # GIF utilities +def _get_optimize(im, info): + return im.mode in ("P", "L") and info and info.get("optimize", 0) + + +def _get_used_palette_colors(im): + used_palette_colors = [] + + # check which colors are used + i = 0 + for count in im.histogram(): + if count: + used_palette_colors.append(i) + i += 1 + + return used_palette_colors + + def getheader(im, palette=None, info=None): """Return a list of strings representing a GIF header""" - optimize = info and info.get("optimize", 0) - # Header Block # http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp header = [ @@ -422,74 +488,68 @@ def getheader(im, palette=None, info=None): if im.mode == "P": if palette and isinstance(palette, bytes): - sourcePalette = palette[:768] + source_palette = palette[:768] else: - sourcePalette = im.im.getpalette("RGB")[:768] + source_palette = im.im.getpalette("RGB")[:768] else: # L-mode if palette and isinstance(palette, bytes): - sourcePalette = palette[:768] + source_palette = palette[:768] else: - sourcePalette = bytearray([i//3 for i in range(768)]) + source_palette = bytearray([i//3 for i in range(768)]) - usedPaletteColors = paletteBytes = None + used_palette_colors = palette_bytes = None - if optimize: - usedPaletteColors = [] - - # check which colors are used - i = 0 - for count in im.histogram(): - if count: - usedPaletteColors.append(i) - i += 1 + if _get_optimize(im, info): + used_palette_colors = _get_used_palette_colors(im) # create the new palette if not every color is used - if len(usedPaletteColors) < 256: - paletteBytes = b"" - newPositions = {} + if len(used_palette_colors) < 256: + palette_bytes = b"" + new_positions = {} i = 0 # pick only the used colors from the palette - for oldPosition in usedPaletteColors: - paletteBytes += sourcePalette[oldPosition*3:oldPosition*3+3] - newPositions[oldPosition] = i + for oldPosition in used_palette_colors: + palette_bytes += source_palette[oldPosition*3:oldPosition*3+3] + new_positions[oldPosition] = i i += 1 # replace the palette color id of all pixel with the new id - imageBytes = bytearray(im.tobytes()) - for i in range(len(imageBytes)): - imageBytes[i] = newPositions[imageBytes[i]] - im.frombytes(bytes(imageBytes)) - newPaletteBytes = (paletteBytes + - (768 - len(paletteBytes)) * b'\x00') - im.putpalette(newPaletteBytes) - im.palette = ImagePalette.ImagePalette("RGB", palette=paletteBytes, - size=len(paletteBytes)) + image_bytes = bytearray(im.tobytes()) + for i in range(len(image_bytes)): + image_bytes[i] = new_positions[image_bytes[i]] + im.frombytes(bytes(image_bytes)) + new_palette_bytes = (palette_bytes + + (768 - len(palette_bytes)) * b'\x00') + im.putpalette(new_palette_bytes) + im.palette = ImagePalette.ImagePalette("RGB", + palette=palette_bytes, + size=len(palette_bytes)) - if not paletteBytes: - paletteBytes = sourcePalette + if not palette_bytes: + palette_bytes = source_palette # Logical Screen Descriptor # calculate the palette size for the header import math - colorTableSize = int(math.ceil(math.log(len(paletteBytes)//3, 2)))-1 - if colorTableSize < 0: - colorTableSize = 0 + color_table_size = int(math.ceil(math.log(len(palette_bytes)//3, 2)))-1 + if color_table_size < 0: + color_table_size = 0 # size of global color table + global color table flag - header.append(o8(colorTableSize + 128)) + header.append(o8(color_table_size + 128)) # background + reserved/aspect header.append(o8(0) + o8(0)) # end of Logical Screen Descriptor # add the missing amount of bytes # the palette has to be 2< 0: - paletteBytes += o8(0) * 3 * actualTargetSizeDiff + actual_target_size_diff = (2 << color_table_size) - len(palette_bytes)//3 + if actual_target_size_diff > 0: + palette_bytes += o8(0) * 3 * actual_target_size_diff # Header + Logical Screen Descriptor + Global Color Table - header.append(paletteBytes) - return header, usedPaletteColors + header.append(palette_bytes) + return header, used_palette_colors def getdata(im, offset=(0, 0), **params): @@ -497,7 +557,7 @@ def getdata(im, offset=(0, 0), **params): The first string is a local image header, the rest contains encoded image data.""" - class collector: + class Collector(object): data = [] def write(self, data): @@ -505,19 +565,13 @@ def getdata(im, offset=(0, 0), **params): im.load() # make sure raster data is available - fp = collector() + fp = Collector() try: im.encoderinfo = params # local image header - fp.write(b"," + - o16(offset[0]) + # offset - o16(offset[1]) + - o16(im.size[0]) + # size - o16(im.size[1]) + - o8(0) + # flags - o8(8)) # bits + get_local_header(fp, im, offset) ImageFile._save(im, fp, [("gif", (0, 0)+im.size, 0, RAWMODE[im.mode])]) diff --git a/PIL/GimpGradientFile.py b/PIL/GimpGradientFile.py index 696f425f1..45af573bb 100644 --- a/PIL/GimpGradientFile.py +++ b/PIL/GimpGradientFile.py @@ -58,7 +58,7 @@ def sphere_decreasing(middle, pos): SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing] -class GradientFile: +class GradientFile(object): gradient = None diff --git a/PIL/GimpPaletteFile.py b/PIL/GimpPaletteFile.py index a066c80cc..4bf3ca36a 100644 --- a/PIL/GimpPaletteFile.py +++ b/PIL/GimpPaletteFile.py @@ -21,7 +21,7 @@ from PIL._binary import o8 ## # File handler for GIMP's palette format. -class GimpPaletteFile: +class GimpPaletteFile(object): rawmode = "RGB" diff --git a/PIL/IcnsImagePlugin.py b/PIL/IcnsImagePlugin.py index e88b84985..e0b130e81 100644 --- a/PIL/IcnsImagePlugin.py +++ b/PIL/IcnsImagePlugin.py @@ -17,7 +17,11 @@ from PIL import Image, ImageFile, PngImagePlugin, _binary import io +import os +import shutil import struct +import sys +import tempfile enable_jpeg2k = hasattr(Image.core, 'jp2klib_version') if enable_jpeg2k: @@ -90,7 +94,7 @@ def read_32(fobj, start_length, size): def read_mk(fobj, start_length, size): # Alpha masks seem to be uncompressed - (start, length) = start_length + start = start_length[0] fobj.seek(start) pixel_size = (size[0] * size[2], size[1] * size[2]) sizesq = pixel_size[0] * pixel_size[1] @@ -126,7 +130,7 @@ def read_png_or_jpeg2000(fobj, start_length, size): raise ValueError('Unsupported icon subimage format') -class IcnsFile: +class IcnsFile(object): SIZES = { (512, 512, 2): [ @@ -247,7 +251,7 @@ class IcnsFile: class IcnsImageFile(ImageFile.ImageFile): """ - PIL read-only image support for Mac OS .icns files. + PIL image support for Mac OS .icns files. Chooses the best resolution, but will possibly load a different size image if you mutate the size attribute before calling 'load'. @@ -293,12 +297,64 @@ class IcnsImageFile(ImageFile.ImageFile): self.tile = () self.load_end() + +def _save(im, fp, filename): + """ + Saves the image as a series of PNG files, + that are then converted to a .icns file + using the OS X command line utility 'iconutil'. + + OS X only. + """ + try: + fp.flush() + except: + pass + + # create the temporary set of pngs + iconset = tempfile.mkdtemp('.iconset') + last_w = None + last_im = None + for w in [16, 32, 128, 256, 512]: + prefix = 'icon_{}x{}'.format(w, w) + + if last_w == w: + im_scaled = last_im + else: + im_scaled = im.resize((w, w), Image.LANCZOS) + im_scaled.save(os.path.join(iconset, prefix+'.png')) + + im_scaled = im.resize((w*2, w*2), Image.LANCZOS) + im_scaled.save(os.path.join(iconset, prefix+'@2x.png')) + last_im = im_scaled + + # iconutil -c icns -o {} {} + from subprocess import Popen, PIPE, CalledProcessError + + convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset] + stderr = tempfile.TemporaryFile() + convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=stderr) + + convert_proc.stdout.close() + + retcode = convert_proc.wait() + + # remove the temporary files + shutil.rmtree(iconset) + + if retcode: + raise CalledProcessError(retcode, convert_cmd) + Image.register_open("ICNS", IcnsImageFile, lambda x: x[:4] == b'icns') Image.register_extension("ICNS", '.icns') +if sys.platform == 'darwin': + Image.register_save("ICNS", _save) + + Image.register_mime("ICNS", "image/icns") + + if __name__ == '__main__': - import os - import sys imf = IcnsImageFile(open(sys.argv[1], 'rb')) for size in imf.info['sizes']: imf.size = size diff --git a/PIL/IcoImagePlugin.py b/PIL/IcoImagePlugin.py index 2dc46ea18..778edaf9a 100644 --- a/PIL/IcoImagePlugin.py +++ b/PIL/IcoImagePlugin.py @@ -24,6 +24,9 @@ __version__ = "0.1" +import struct +from io import BytesIO + from PIL import Image, ImageFile, BmpImagePlugin, PngImagePlugin, _binary from math import log, ceil @@ -37,11 +40,46 @@ i32 = _binary.i32le _MAGIC = b"\0\0\1\0" +def _save(im, fp, filename): + fp.write(_MAGIC) # (2+2) + sizes = im.encoderinfo.get("sizes", + [(16, 16), (24, 24), (32, 32), (48, 48), + (64, 64), (128, 128), (255, 255)]) + width, height = im.size + filter(lambda x: False if (x[0] > width or x[1] > height or + x[0] > 255 or x[1] > 255) else True, sizes) + fp.write(struct.pack("= self.info[FRAMES]: @@ -313,7 +317,7 @@ SAVE = { def _save(im, fp, filename, check=0): try: - type, rawmode = SAVE[im.mode] + image_type, rawmode = SAVE[im.mode] except KeyError: raise ValueError("Cannot save %s images as IM" % im.mode) @@ -325,7 +329,7 @@ def _save(im, fp, filename, check=0): if check: return check - fp.write(("Image type: %s image\r\n" % type).encode('ascii')) + fp.write(("Image type: %s image\r\n" % image_type).encode('ascii')) if filename: fp.write(("Name: %s\r\n" % filename).encode('ascii')) fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode('ascii')) diff --git a/PIL/Image.py b/PIL/Image.py index ed948e048..1c95bfca0 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -35,7 +35,7 @@ class DecompressionBombWarning(RuntimeWarning): pass -class _imaging_not_installed: +class _imaging_not_installed(object): # module placeholder def __getattr__(self, id): raise ImportError("The _imaging C module is not installed") @@ -55,10 +55,11 @@ except ImportError: pass try: - # If the _imaging C module is not present, you can still use - # the "open" function to identify files, but you cannot load - # them. Note that other modules should not refer to _imaging - # directly; import Image and use the Image.core variable instead. + # If the _imaging C module is not present, Pillow will not load. + # Note that other modules should not refer to _imaging directly; + # import Image and use the Image.core variable instead. + # Also note that Image.core is not a publicly documented interface, + # and should be considered private and subject to change. from PIL import _imaging as core if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None): raise ImportError("The _imaging extension was built for another " @@ -91,6 +92,7 @@ except ImportError as v: RuntimeWarning ) # Fail here anyway. Don't let people run with a mostly broken Pillow. + # see docs/porting-pil-to-pillow.rst raise try: @@ -107,6 +109,8 @@ from PIL._util import deferred_error import os import sys +import io +import struct # type stuff import collections @@ -150,6 +154,7 @@ FLIP_TOP_BOTTOM = 1 ROTATE_90 = 2 ROTATE_180 = 3 ROTATE_270 = 4 +TRANSPOSE = 5 # transforms AFFINE = 0 @@ -159,11 +164,10 @@ QUAD = 3 MESH = 4 # resampling filters -NONE = 0 -NEAREST = 0 -ANTIALIAS = 1 # 3-lobed lanczos -LINEAR = BILINEAR = 2 -CUBIC = BICUBIC = 3 +NEAREST = NONE = 0 +LANCZOS = ANTIALIAS = 1 +BILINEAR = LINEAR = 2 +BICUBIC = CUBIC = 3 # dithers NONE = 0 @@ -383,7 +387,7 @@ def init(): for plugin in _plugins: try: if DEBUG: - print ("Importing %s" % plugin) + print("Importing %s" % plugin) __import__("PIL.%s" % plugin, globals(), locals(), []) except ImportError: if DEBUG: @@ -439,7 +443,7 @@ def coerce_e(value): return value if isinstance(value, _E) else _E(value) -class _E: +class _E(object): def __init__(self, data): self.data = data @@ -474,7 +478,7 @@ def _getscaleoffset(expr): # -------------------------------------------------------------------- # Implementation wrapper -class Image: +class Image(object): """ This class represents an image object. To create :py:class:`~PIL.Image.Image` objects, use the appropriate factory @@ -542,7 +546,7 @@ class Image: self.fp.close() except Exception as msg: if DEBUG: - print ("Error closing: %s" % msg) + print("Error closing: %s" % msg) # Instead of simply setting to None, we're setting up a # deferred error that will better explain that the core image @@ -596,6 +600,16 @@ class Image: id(self) ) + def _repr_png_(self): + """ iPython display hook support + + :returns: png version of the image as bytes + """ + from io import BytesIO + b = BytesIO() + self.save(b, 'PNG') + return b.getvalue() + def __getattr__(self, name): if name == "__array_interface__": # numpy array interface support @@ -623,7 +637,7 @@ class Image: self.mode = mode self.size = size self.im = core.new(mode, size) - if mode in ("L", "P"): + if mode in ("L", "P") and palette: self.putpalette(palette) self.frombytes(data) @@ -742,6 +756,7 @@ class Image: associated with the image. :returns: An image access object. + :rtype: :ref:`PixelAccess` or :py:class:`PIL.PyAccess` """ if self.im and self.palette and self.palette.dirty: # realize palette @@ -801,7 +816,7 @@ class Image: use other thresholds, use the :py:meth:`~PIL.Image.Image.point` method. - :param mode: The requested mode. + :param mode: The requested mode. See: :ref:`concept-modes`. :param matrix: An optional conversion matrix. If given, this should be 4- or 16-tuple containing floating point values. :param dither: Dithering method, used when converting from @@ -875,16 +890,15 @@ class Image: elif self.mode == 'P' and mode == 'RGBA': t = self.info['transparency'] delete_trns = True - + if isinstance(t, bytes): self.im.putpalettealphas(t) elif isinstance(t, int): - self.im.putpalettealpha(t,0) + self.im.putpalettealpha(t, 0) else: raise ValueError("Transparency for P mode should" + " be bytes or int") - if mode == "P" and palette == ADAPTIVE: im = self.im.quantize(colors) new = self._new(im) @@ -936,14 +950,19 @@ class Image: return new_im def quantize(self, colors=256, method=None, kmeans=0, palette=None): + """ + Convert the image to 'P' mode with the specified number + of colors. - # methods: - # 0 = median cut - # 1 = maximum coverage - # 2 = fast octree + :param colors: The desired number of colors, <= 256 + :param method: 0 = median cut + 1 = maximum coverage + 2 = fast octree + :param kmeans: Integer + :param palette: Quantize to the :py:class:`PIL.ImagingPalette` palette. + :returns: A new image - # NOTE: this functionality will be moved to the extended - # quantizer interface in a later version of PIL. + """ self.load() @@ -1269,11 +1288,11 @@ class Image: images (in the latter case, the alpha band is used as mask). Where the mask is 255, the given image is copied as is. Where the mask is 0, the current value is preserved. Intermediate - values can be used for transparency effects. + values will mix the two images together, including their alpha + channels if they have them. - Note that if you paste an "RGBA" image, the alpha band is - ignored. You can work around this by using the same image as - both source image and mask. + See :py:meth:`~PIL.Image.Image.alpha_composite` if you want to + combine images with respect to their alpha channels. :param im: Source image or pixel value (integer or tuple). :param box: An optional 4-tuple giving the region to paste into. @@ -1514,21 +1533,20 @@ class Image: (width, height). :param resample: An optional resampling filter. This can be one of :py:attr:`PIL.Image.NEAREST` (use nearest neighbour), - :py:attr:`PIL.Image.BILINEAR` (linear interpolation in a 2x2 - environment), :py:attr:`PIL.Image.BICUBIC` (cubic spline - interpolation in a 4x4 environment), or - :py:attr:`PIL.Image.ANTIALIAS` (a high-quality downsampling filter). + :py:attr:`PIL.Image.BILINEAR` (linear interpolation), + :py:attr:`PIL.Image.BICUBIC` (cubic spline interpolation), or + :py:attr:`PIL.Image.LANCZOS` (a high-quality downsampling filter). If omitted, or if the image has mode "1" or "P", it is set :py:attr:`PIL.Image.NEAREST`. :returns: An :py:class:`~PIL.Image.Image` object. """ - if resample not in (NEAREST, BILINEAR, BICUBIC, ANTIALIAS): + if resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS): raise ValueError("unknown resampling filter") self.load() - size=tuple(size) + size = tuple(size) if self.size == size: return self._new(self.im) @@ -1538,16 +1556,7 @@ class Image: if self.mode == 'RGBA': return self.convert('RGBa').resize(size, resample).convert('RGBA') - if resample == ANTIALIAS: - # requires stretch support (imToolkit & PIL 1.1.3) - try: - im = self.im.stretch(size, resample) - except AttributeError: - raise ValueError("unsupported resampling filter") - else: - im = self.im.resize(size, resample) - - return self._new(im) + return self._new(self.im.resize(size, resample)) def rotate(self, angle, resample=NEAREST, expand=0): """ @@ -1618,15 +1627,16 @@ class Image: Keyword options can be used to provide additional instructions to the writer. If a writer doesn't recognise an option, it is - silently ignored. The available options are described later in - this handbook. + silently ignored. The available options are described in the + :doc:`image format documentation + <../handbook/image-file-formats>` for each writer. You can use a file object instead of a filename. In this case, you must always specify the format. The file object must - implement the **seek**, **tell**, and **write** + implement the ``seek``, ``tell``, and ``write`` methods, and be opened in binary mode. - :param file: File name or file object. + :param fp: File name or file object. :param format: Optional format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this @@ -1753,7 +1763,7 @@ class Image: """ return 0 - def thumbnail(self, size, resample=ANTIALIAS): + def thumbnail(self, size, resample=BICUBIC): """ Make this image into a thumbnail. This method modifies the image to contain a thumbnail version of itself, no larger than @@ -1762,12 +1772,7 @@ class Image: :py:meth:`~PIL.Image.Image.draft` method to configure the file reader (where applicable), and finally resizes the image. - Note that the bilinear and bicubic filters in the current - version of PIL are not well-suited for thumbnail generation. - You should use :py:attr:`PIL.Image.ANTIALIAS` unless speed is much more - important than quality. - - Also note that this function modifies the :py:class:`~PIL.Image.Image` + Note that this function modifies the :py:class:`~PIL.Image.Image` object in place. If you need to use the full resolution image as well, apply this method to a :py:meth:`~PIL.Image.Image.copy` of the original image. @@ -1775,10 +1780,9 @@ class Image: :param size: Requested size. :param resample: Optional resampling filter. This can be one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BILINEAR`, - :py:attr:`PIL.Image.BICUBIC`, or :py:attr:`PIL.Image.ANTIALIAS` - (best quality). If omitted, it defaults to - :py:attr:`PIL.Image.ANTIALIAS`. (was :py:attr:`PIL.Image.NEAREST` - prior to version 2.5.0) + :py:attr:`PIL.Image.BICUBIC`, or :py:attr:`PIL.Image.LANCZOS`. + If omitted, it defaults to :py:attr:`PIL.Image.BICUBIC`. + (was :py:attr:`PIL.Image.NEAREST` prior to version 2.5.0) :returns: None """ @@ -1797,14 +1801,7 @@ class Image: self.draft(None, size) - self.load() - - try: - im = self.resize(size, resample) - except ValueError: - if resample != ANTIALIAS: - raise - im = self.resize(size, NEAREST) # fallback + im = self.resize(size, resample) self.im = im.im self.mode = im.mode @@ -1813,7 +1810,7 @@ class Image: self.readonly = 0 self.pyaccess = None - # FIXME: the different tranform methods need further explanation + # FIXME: the different transform methods need further explanation # instead of bloating the method docs, add a separate chapter. def transform(self, size, method, data=None, resample=NEAREST, fill=1): """ @@ -1920,13 +1917,13 @@ class Image: :param method: One of :py:attr:`PIL.Image.FLIP_LEFT_RIGHT`, :py:attr:`PIL.Image.FLIP_TOP_BOTTOM`, :py:attr:`PIL.Image.ROTATE_90`, - :py:attr:`PIL.Image.ROTATE_180`, or :py:attr:`PIL.Image.ROTATE_270`. + :py:attr:`PIL.Image.ROTATE_180`, :py:attr:`PIL.Image.ROTATE_270` or + :py:attr:`PIL.Image.TRANSPOSE`. :returns: Returns a flipped or rotated copy of this image. """ self.load() - im = self.im.transpose(method) - return self._new(im) + return self._new(self.im.transpose(method)) def effect_spread(self, distance): """ @@ -1978,12 +1975,12 @@ class _ImageCrop(Image): # -------------------------------------------------------------------- # Abstract handlers. -class ImagePointHandler: +class ImagePointHandler(object): # used as a mixin by point transforms (for use with im.point) pass -class ImageTransformHandler: +class ImageTransformHandler(object): # used as a mixin by geometry transforms (for use with im.transform) pass @@ -2004,7 +2001,8 @@ def new(mode, size, color=0): """ Creates a new image with the given mode and size. - :param mode: The mode to use for the new image. + :param mode: The mode to use for the new image. See: + :ref:`concept-modes`. :param size: A 2-tuple, containing (width, height) in pixels. :param color: What color to use for the image. Default is black. If given, this should be a single integer or floating point value @@ -2037,14 +2035,14 @@ def frombytes(mode, size, data, decoder_name="raw", *args): You can also use any pixel decoder supported by PIL. For more information on available decoders, see the section - **Writing Your Own File Decoder**. + :ref:`Writing Your Own File Decoder `. Note that this function decodes pixel data only, not entire images. If you have an entire image in a string, wrap it in a :py:class:`~io.BytesIO` object, and use :py:func:`~PIL.Image.open` to load it. - :param mode: The image mode. + :param mode: The image mode. See: :ref:`concept-modes`. :param size: The image size. :param data: A byte buffer containing raw data for the given mode. :param decoder_name: What decoder to use. @@ -2096,7 +2094,7 @@ def frombuffer(mode, size, data, decoder_name="raw", *args): issues a warning if you do this; to disable the warning, you should provide the full set of parameters. See below for details. - :param mode: The image mode. + :param mode: The image mode. See: :ref:`concept-modes`. :param size: The image size. :param data: A bytes or other buffer object containing raw data for the given mode. @@ -2147,7 +2145,8 @@ def fromarray(obj, mode=None): :param obj: Object with array interface :param mode: Mode to use (will be determined from type if None) - :returns: An image memory. + See: :ref:`concept-modes`. + :returns: An image object. .. versionadded:: 1.1.6 """ @@ -2250,6 +2249,11 @@ def open(fp, mode="r"): else: filename = "" + try: + fp.seek(0) + except (AttributeError, io.UnsupportedOperation): + fp = io.BytesIO(fp.read()) + prefix = fp.read(16) preinit() @@ -2262,7 +2266,7 @@ def open(fp, mode="r"): im = factory(fp, filename) _decompression_bomb_check(im.size) return im - except (SyntaxError, IndexError, TypeError): + except (SyntaxError, IndexError, TypeError, struct.error): # import traceback # traceback.print_exc() pass @@ -2277,7 +2281,7 @@ def open(fp, mode="r"): im = factory(fp, filename) _decompression_bomb_check(im.size) return im - except (SyntaxError, IndexError, TypeError): + except (SyntaxError, IndexError, TypeError, struct.error): # import traceback # traceback.print_exc() pass @@ -2364,7 +2368,8 @@ def merge(mode, bands): """ Merge a set of single band images into a new multiband image. - :param mode: The mode to use for the output image. + :param mode: The mode to use for the output image. See: + :ref:`concept-modes`. :param bands: A sequence containing one single-band image for each band in the output image. All bands must have the same size. diff --git a/PIL/ImageCms.py b/PIL/ImageCms.py index ed219f7ba..ebf127df3 100644 --- a/PIL/ImageCms.py +++ b/PIL/ImageCms.py @@ -64,7 +64,7 @@ pyCMS 0.0.2 alpha Jan 6, 2002 - Added try/except statements arount type() checks of + Added try/except statements around type() checks of potential CObjects... Python won't let you use type() on them, and raises a TypeError (stupid, if you ask me!) @@ -123,8 +123,8 @@ FLAGS = { "NOTCACHE": 64, # Inhibit 1-pixel cache "NOTPRECALC": 256, "NULLTRANSFORM": 512, # Don't transform anyway - "HIGHRESPRECALC": 1024, # Use more memory to give better accurancy - "LOWRESPRECALC": 2048, # Use less memory to minimize resouces + "HIGHRESPRECALC": 1024, # Use more memory to give better accuracy + "LOWRESPRECALC": 2048, # Use less memory to minimize resources "WHITEBLACKCOMPENSATION": 8192, "BLACKPOINTCOMPENSATION": 8192, "GAMUTCHECK": 4096, # Out of Gamut alarm @@ -147,7 +147,7 @@ for flag in FLAGS.values(): ## # Profile. -class ImageCmsProfile: +class ImageCmsProfile(object): def __init__(self, profile): """ @@ -573,7 +573,7 @@ def applyTransform(im, transform, inPlace=0): This function applies a pre-calculated transform (from ImageCms.buildTransform() or ImageCms.buildTransformFromOpenProfiles()) to an image. The transform can be used for multiple images, saving - considerable calcuation time if doing the same conversion multiple times. + considerable calculation time if doing the same conversion multiple times. If you want to modify im in-place instead of receiving a new image as the return value, set inPlace to TRUE. This can only be done if @@ -858,7 +858,7 @@ def getDefaultIntent(profile): If an error occurs while trying to obtain the default intent, a PyCMSError is raised. - Use this function to determine the default (and usually best optomized) + Use this function to determine the default (and usually best optimized) rendering intent for this profile. Most profiles support multiple rendering intents, but are intended mostly for one type of conversion. If you wish to use a different intent than returned, use @@ -914,7 +914,7 @@ def isIntentSupported(profile, intent, direction): see the pyCMS documentation for details on rendering intents and what they do. - :param direction: Integer specifing if the profile is to be used for input, + :param direction: Integer specifying if the profile is to be used for input, output, or proof INPUT = 0 (or use ImageCms.DIRECTION_INPUT) diff --git a/PIL/ImageDraw.py b/PIL/ImageDraw.py index a2a75d1c6..1fc5b4d61 100644 --- a/PIL/ImageDraw.py +++ b/PIL/ImageDraw.py @@ -47,7 +47,7 @@ except ImportError: # Application code should use the Draw factory, instead of # directly. -class ImageDraw: +class ImageDraw(object): ## # Create a drawing instance. diff --git a/PIL/ImageDraw2.py b/PIL/ImageDraw2.py index c967a200f..62ee11630 100644 --- a/PIL/ImageDraw2.py +++ b/PIL/ImageDraw2.py @@ -19,25 +19,25 @@ from PIL import Image, ImageColor, ImageDraw, ImageFont, ImagePath -class Pen: +class Pen(object): def __init__(self, color, width=1, opacity=255): self.color = ImageColor.getrgb(color) self.width = width -class Brush: +class Brush(object): def __init__(self, color, opacity=255): self.color = ImageColor.getrgb(color) -class Font: +class Font(object): def __init__(self, color, file, size=12): # FIXME: add support for bitmap fonts self.color = ImageColor.getrgb(color) self.font = ImageFont.truetype(file, size) -class Draw: +class Draw(object): def __init__(self, image, size=None, color=None): if not hasattr(image, "im"): diff --git a/PIL/ImageEnhance.py b/PIL/ImageEnhance.py index a196d5b09..56b5c0199 100644 --- a/PIL/ImageEnhance.py +++ b/PIL/ImageEnhance.py @@ -21,7 +21,7 @@ from PIL import Image, ImageFilter, ImageStat -class _Enhance: +class _Enhance(object): def enhance(self, factor): """ @@ -53,6 +53,7 @@ class Color(_Enhance): self.degenerate = image.convert(self.intermediate_mode).convert(image.mode) + class Contrast(_Enhance): """Adjust image contrast. @@ -72,7 +73,7 @@ class Contrast(_Enhance): class Brightness(_Enhance): """Adjust image brightness. - This class can be used to control the brighntess of an image. An + This class can be used to control the brightness of an image. An enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the original image. """ diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index 82691af92..b1d261166 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -196,6 +196,9 @@ class ImageFile(Image.Image): except AttributeError: prefix = b"" + # Buffer length read; assign a default value + t = 0 + for d, e, o, a in self.tile: d = Image._getdecoder(self.mode, d, a, self.decoderconfig) seek(o) @@ -308,7 +311,7 @@ class StubImageFile(ImageFile): ) -class Parser: +class Parser(object): """ Incremental image parser. This class implements the standard feed/close consumer interface. @@ -319,6 +322,7 @@ class Parser: image = None data = None decoder = None + offset = 0 finished = 0 def reset(self): diff --git a/PIL/ImageFilter.py b/PIL/ImageFilter.py index 1e0154d12..baa168aa7 100644 --- a/PIL/ImageFilter.py +++ b/PIL/ImageFilter.py @@ -15,7 +15,7 @@ # See the README file for information on usage and redistribution. # -from functools import reduce +import functools class Filter(object): @@ -43,7 +43,7 @@ class Kernel(Filter): def __init__(self, size, kernel, scale=None, offset=0): if scale is None: # default scale is sum of kernel - scale = reduce(lambda a, b: a+b, kernel) + scale = functools.reduce(lambda a, b: a+b, kernel) if size[0] * size[1] != len(kernel): raise ValueError("not enough coefficients in kernel") self.filterargs = size, scale, offset, kernel @@ -162,8 +162,13 @@ class UnsharpMask(Filter): See Wikipedia's entry on `digital unsharp masking`_ for an explanation of the parameters. - .. _digital unsharp masking: - https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking + :param radius: Blur Radius + :param percent: Unsharp strength, in percent + :param threshold: Threshold controls the minimum brightness change that + will be sharpened + + .. _digital unsharp masking: https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking + """ name = "UnsharpMask" diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index bd52ea938..c6e7cb4d9 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -25,8 +25,6 @@ # See the README file for information on usage and redistribution. # -from __future__ import print_function - from PIL import Image from PIL._util import isDirectory, isPath import os @@ -38,7 +36,7 @@ except ImportError: warnings = None -class _imagingft_not_installed: +class _imagingft_not_installed(object): # module placeholder def __getattr__(self, id): raise ImportError("The _imagingft C module is not installed") @@ -64,7 +62,7 @@ except ImportError: # -------------------------------------------------------------------- -class ImageFont: +class ImageFont(object): "PIL font wrapper" def _load_pilfont(self, filename): @@ -120,7 +118,7 @@ class ImageFont: # Wrapper for FreeType fonts. Application code should use the # truetype factory function to create font objects. -class FreeTypeFont: +class FreeTypeFont(object): "FreeType font wrapper (requires _imagingft service)" def __init__(self, font=None, size=10, index=0, encoding="", file=None): @@ -133,6 +131,11 @@ class FreeTypeFont: DeprecationWarning) font = file + self.path = font + self.size = size + self.index = index + self.encoding = encoding + if isPath(font): self.font = core.getfont(font, size, index, encoding) else: @@ -162,6 +165,22 @@ class FreeTypeFont: self.font.render(text, im.id, mode == "1") return im, offset + def font_variant(self, font=None, size=None, index=None, encoding=None): + """ + Create a copy of this FreeTypeFont object, + using any specified arguments to override the settings. + + Parameters are identical to the parameters used to initialize this + object, minus the deprecated 'file' argument. + + :return: A FreeTypeFont object. + """ + return FreeTypeFont(font=self.path if font is None else font, + size=self.size if size is None else size, + index=self.index if index is None else index, + encoding=self.encoding if encoding is None else + encoding) + ## # Wrapper that creates a transposed font from any existing font # object. @@ -172,7 +191,7 @@ class FreeTypeFont: # Image.ROTATE_90, Image.ROTATE_180, or Image.ROTATE_270. -class TransposedFont: +class TransposedFont(object): "Wrapper for writing rotated or mirrored text" def __init__(self, font, orientation=None): @@ -214,7 +233,7 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): This function requires the _imagingft service. - :param filename: A truetype font file. Under Windows, if the file + :param font: A truetype font file. Under Windows, if the file is not found in this filename, the loader also looks in Windows :file:`fonts/` directory. :param size: The requested size, in points. @@ -224,6 +243,7 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): Symbol), "ADOB" (Adobe Standard), "ADBE" (Adobe Expert), and "armn" (Apple Roman). See the FreeType documentation for more information. + :param filename: Deprecated. Please use font instead. :return: A font object. :exception IOError: If the file could not be read. """ @@ -239,14 +259,44 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): try: return FreeTypeFont(font, size, index, encoding) except IOError: + ttf_filename = os.path.basename(font) + + dirs = [] if sys.platform == "win32": # check the windows font repository # NOTE: must use uppercase WINDIR, to work around bugs in # 1.5.2's os.environ.get() windir = os.environ.get("WINDIR") if windir: - filename = os.path.join(windir, "fonts", font) - return FreeTypeFont(filename, size, index, encoding) + dirs.append(os.path.join(windir, "fonts")) + elif sys.platform in ('linux', 'linux2'): + lindirs = os.environ.get("XDG_DATA_DIRS", "") + if not lindirs: + # According to the freedesktop spec, XDG_DATA_DIRS should + # default to /usr/share + lindirs = '/usr/share' + dirs += [os.path.join(lindir, "fonts") for lindir in lindirs.split(":")] + elif sys.platform == 'darwin': + dirs += ['/Library/Fonts', '/System/Library/Fonts', + os.path.expanduser('~/Library/Fonts')] + + ext = os.path.splitext(ttf_filename)[1] + first_font_with_a_different_extension = None + for directory in dirs: + for walkroot, walkdir, walkfilenames in os.walk(directory): + for walkfilename in walkfilenames: + if ext and walkfilename == ttf_filename: + fontpath = os.path.join(walkroot, walkfilename) + return FreeTypeFont(fontpath, size, index, encoding) + elif not ext and os.path.splitext(walkfilename)[0] == ttf_filename: + fontpath = os.path.join(walkroot, walkfilename) + if os.path.splitext(fontpath)[1] == '.ttf': + return FreeTypeFont(fontpath, size, index, encoding) + if not ext and first_font_with_a_different_extension is None: + first_font_with_a_different_extension = fontpath + if first_font_with_a_different_extension: + return FreeTypeFont(first_font_with_a_different_extension, size, + index, encoding) raise @@ -259,15 +309,15 @@ def load_path(filename): :return: A font object. :exception IOError: If the file could not be read. """ - for dir in sys.path: - if isDirectory(dir): + for directory in sys.path: + if isDirectory(directory): if not isinstance(filename, str): if bytes is str: filename = filename.encode("utf-8") else: filename = filename.decode("utf-8") try: - return load(os.path.join(dir, filename)) + return load(os.path.join(directory, filename)) except IOError: pass raise IOError("cannot find font file") diff --git a/PIL/ImageMath.py b/PIL/ImageMath.py index 4dcc5125c..f92d5001f 100644 --- a/PIL/ImageMath.py +++ b/PIL/ImageMath.py @@ -31,7 +31,7 @@ def _isconstant(v): return isinstance(v, int) or isinstance(v, float) -class _Operand: +class _Operand(object): # wraps an image operand, providing standard operators def __init__(self, im): diff --git a/PIL/ImageMode.py b/PIL/ImageMode.py index 295069108..d8960017b 100644 --- a/PIL/ImageMode.py +++ b/PIL/ImageMode.py @@ -20,7 +20,7 @@ _modes = {} ## # Wrapper for mode strings. -class ModeDescriptor: +class ModeDescriptor(object): def __init__(self, mode, bands, basemode, basetype): self.mode = mode diff --git a/PIL/ImageMorph.py b/PIL/ImageMorph.py index 996eacb7d..6f92e9e67 100644 --- a/PIL/ImageMorph.py +++ b/PIL/ImageMorph.py @@ -12,7 +12,7 @@ import re LUT_SIZE = 1 << 9 -class LutBuilder: +class LutBuilder(object): """A class for building a MorphLut from a descriptive language The input patterns is a list of a strings sequences like these:: @@ -176,7 +176,7 @@ class LutBuilder: return self.lut -class MorphOp: +class MorphOp(object): """A class for binary morphological operators""" def __init__(self, diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index b3afd9e95..f317645b2 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -20,7 +20,7 @@ from PIL import Image from PIL._util import isStringType import operator -from functools import reduce +import functools # @@ -213,7 +213,7 @@ def equalize(image, mask=None): if len(histo) <= 1: lut.extend(list(range(256))) else: - step = (reduce(operator.add, histo) - histo[-1]) // 255 + step = (functools.reduce(operator.add, histo) - histo[-1]) // 255 if not step: lut.extend(list(range(256))) else: @@ -233,7 +233,6 @@ def expand(image, border=0, fill=0): :param fill: Pixel fill value (a color value). Default is 0 (black). :return: An image. """ - "Add border to image" left, top, right, bottom = _border(border) width = left + image.size[0] + right height = top + image.size[1] + bottom @@ -441,3 +440,22 @@ def unsharp_mask(im, radius=None, percent=None, threshold=None): return im.im.unsharp_mask(radius, percent, threshold) usm = unsharp_mask + + +def box_blur(image, radius): + """ + Blur the image by setting each pixel to the average value of the pixels + in a square box extending radius pixels in each direction. + Supports float radius of arbitrary size. Uses an optimized implementation + which runs in linear time relative to the size of the image + for any radius value. + + :param image: The image to blur. + :param radius: Size of the box in one direction. Radius 0 does not blur, + returns an identical image. Radius 1 takes 1 pixel + in each direction, i.e. 9 pixels in total. + :return: An image. + """ + image.load() + + return image._new(image.im.box_blur(radius)) diff --git a/PIL/ImagePalette.py b/PIL/ImagePalette.py index 62f8814f1..b2f51dd06 100644 --- a/PIL/ImagePalette.py +++ b/PIL/ImagePalette.py @@ -21,7 +21,7 @@ import warnings from PIL import ImageColor -class ImagePalette: +class ImagePalette(object): "Color palette for palette mapped images" def __init__(self, mode="RGB", palette=None, size=0): @@ -225,8 +225,8 @@ def load(filename): p = PaletteFile.PaletteFile(fp) lut = p.getpalette() except (SyntaxError, ValueError): - import traceback - traceback.print_exc() + #import traceback + #traceback.print_exc() pass if not lut: diff --git a/PIL/ImagePath.py b/PIL/ImagePath.py index 656d5ce61..f23d01430 100644 --- a/PIL/ImagePath.py +++ b/PIL/ImagePath.py @@ -20,7 +20,7 @@ from PIL import Image # the Python class below is overridden by the C implementation. -class Path: +class Path(object): def __init__(self, xy): pass diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index 88df3e564..1723e3226 100644 --- a/PIL/ImageQt.py +++ b/PIL/ImageQt.py @@ -18,16 +18,24 @@ from PIL import Image from PIL._util import isPath +import sys -try: - from PyQt5.QtGui import QImage, qRgba -except: +if 'PyQt4.QtGui' not in sys.modules: + try: + from PyQt5.QtGui import QImage, qRgba + except: + try: + from PyQt4.QtGui import QImage, qRgba + except: + from PySide.QtGui import QImage, qRgba + +else: #PyQt4 is used from PyQt4.QtGui import QImage, qRgba - ## # (Internal) Turns an RGB color into a Qt compatible color integer. + def rgb(r, g, b, a=255): # use qRgb to pack the colors, and then turn the resulting long # into a negative integer with the same bitpattern. diff --git a/PIL/ImageSequence.py b/PIL/ImageSequence.py index dd01e2902..256bcbedb 100644 --- a/PIL/ImageSequence.py +++ b/PIL/ImageSequence.py @@ -16,7 +16,7 @@ ## -class Iterator: +class Iterator(object): """ This class implements an iterator object that can be used to loop over an image sequence. diff --git a/PIL/ImageShow.py b/PIL/ImageShow.py index 9527dbf97..51417c30b 100644 --- a/PIL/ImageShow.py +++ b/PIL/ImageShow.py @@ -56,7 +56,7 @@ def show(image, title=None, **options): ## # Base class for viewers. -class Viewer: +class Viewer(object): # main api diff --git a/PIL/ImageStat.py b/PIL/ImageStat.py index 7e023c673..f3c138b3a 100644 --- a/PIL/ImageStat.py +++ b/PIL/ImageStat.py @@ -23,10 +23,10 @@ import math import operator -from functools import reduce +import functools -class Stat: +class Stat(object): def __init__(self, image_or_list, mask=None): try: @@ -71,7 +71,7 @@ class Stat: v = [] for i in range(0, len(self.h), 256): - v.append(reduce(operator.add, self.h[i:i+256])) + v.append(functools.reduce(operator.add, self.h[i:i+256])) return v def _getsum(self): @@ -79,10 +79,10 @@ class Stat: v = [] for i in range(0, len(self.h), 256): - sum = 0.0 + layerSum = 0.0 for j in range(256): - sum += j * self.h[i + j] - v.append(sum) + layerSum += j * self.h[i + j] + v.append(layerSum) return v def _getsum2(self): diff --git a/PIL/ImageTk.py b/PIL/ImageTk.py index 5fb5ecff3..68d388e74 100644 --- a/PIL/ImageTk.py +++ b/PIL/ImageTk.py @@ -56,7 +56,7 @@ def _pilbitmap_check(): # -------------------------------------------------------------------- # PhotoImage -class PhotoImage: +class PhotoImage(object): """ A Tkinter-compatible photo image. This can be used everywhere Tkinter expects an image object. If the image is an RGBA @@ -190,7 +190,7 @@ class PhotoImage: # BitmapImage -class BitmapImage: +class BitmapImage(object): """ A Tkinter-compatible bitmap image. This can be used everywhere Tkinter diff --git a/PIL/ImageWin.py b/PIL/ImageWin.py index 300d118c9..bcb54bc3e 100644 --- a/PIL/ImageWin.py +++ b/PIL/ImageWin.py @@ -21,7 +21,7 @@ import warnings from PIL import Image -class HDC: +class HDC(object): """ Wraps an HDC integer. The resulting object can be passed to the :py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose` @@ -34,7 +34,7 @@ class HDC: return self.dc -class HWND: +class HWND(object): """ Wraps an HWND integer. The resulting object can be passed to the :py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose` @@ -47,7 +47,7 @@ class HWND: return self.wnd -class Dib: +class Dib(object): """ A Windows bitmap with the given mode and size. The mode can be one of "1", "L", "P", or "RGB". @@ -206,7 +206,7 @@ class Dib: ## # Create a Window with the given title size. -class Window: +class Window(object): def __init__(self, title="PIL", width=None, height=None): self.hwnd = Image.core.createwindow( diff --git a/PIL/IptcImagePlugin.py b/PIL/IptcImagePlugin.py index dc8607591..47c7e1936 100644 --- a/PIL/IptcImagePlugin.py +++ b/PIL/IptcImagePlugin.py @@ -222,7 +222,7 @@ def getiptcinfo(im): offset += 2 # resource name (usually empty) name_len = i8(app[offset]) - name = app[offset+1:offset+1+name_len] + # name = app[offset+1:offset+1+name_len] offset = 1 + offset + name_len if offset & 1: offset += 1 @@ -251,7 +251,7 @@ def getiptcinfo(im): return None # no properties # create an IptcImagePlugin object without initializing it - class FakeImage: + class FakeImage(object): pass im = FakeImage() im.__class__ = IptcImageFile diff --git a/PIL/Jpeg2KImagePlugin.py b/PIL/Jpeg2KImagePlugin.py index 446699fdb..ed3e1530a 100644 --- a/PIL/Jpeg2KImagePlugin.py +++ b/PIL/Jpeg2KImagePlugin.py @@ -12,14 +12,13 @@ # # See the README file for information on usage and redistribution. # - -__version__ = "0.1" - from PIL import Image, ImageFile import struct import os import io +__version__ = "0.1" + def _parse_codestream(fp): """Parse the JPEG 2000 codestream to extract the size and component @@ -208,8 +207,8 @@ class Jpeg2KImageFile(ImageFile.ImageFile): def _accept(prefix): - return (prefix[:4] == b'\xff\x4f\xff\x51' - or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a') + return (prefix[:4] == b'\xff\x4f\xff\x51' or + prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a') # ------------------------------------------------------------ diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index a3b603458..5cae90073 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -4,7 +4,7 @@ # # JPEG (JFIF) file handling # -# See "Digital Compression and Coding of Continous-Tone Still Images, +# See "Digital Compression and Coding of Continuous-Tone Still Images, # Part 1, Requirements and Guidelines" (CCITT T.81 / ISO 10918-1) # # History: @@ -355,7 +355,7 @@ class JpegImageFile(ImageFile.ImageFile): scale = s self.tile = [(d, e, o, a)] - self.decoderconfig = (scale, 1) + self.decoderconfig = (scale, 0) return self @@ -454,13 +454,13 @@ def _getmp(self): data = self.info["mp"] except KeyError: return None - file = io.BytesIO(data) - head = file.read(8) + file_contents = io.BytesIO(data) + head = file_contents.read(8) endianness = '>' if head[:4] == b'\x4d\x4d\x00\x2a' else '<' mp = {} # process dictionary info = TiffImagePlugin.ImageFileDirectory(head) - info.load(file) + info.load(file_contents) for key, value in info.items(): mp[key] = _fixup(value) # it's an error not to have a number of images @@ -684,7 +684,8 @@ def _save(im, fp, filename): # https://github.com/jdriscoll/django-imagekit/issues/50 bufsize = 0 if "optimize" in info or "progressive" in info or "progression" in info: - if quality >= 95: + # keep sets quality to 0, but the actual value may be high. + if quality >= 95 or quality == 0: bufsize = 2 * im.size[0] * im.size[1] else: bufsize = im.size[0] * im.size[1] @@ -703,7 +704,7 @@ def _save_cjpeg(im, fp, filename): tempfile = im._dump() subprocess.check_call(["cjpeg", "-outfile", filename, tempfile]) try: - os.unlink(file) + os.unlink(tempfile) except: pass diff --git a/PIL/JpegPresets.py b/PIL/JpegPresets.py index 6ca46d0cd..67af9ac9a 100644 --- a/PIL/JpegPresets.py +++ b/PIL/JpegPresets.py @@ -67,7 +67,7 @@ Libjpeg ref.: http://www.jpegcameras.com/libjpeg/libjpeg-3.html """ presets = { - 'web_low': {'subsampling': 2, # "4:1:1" + 'web_low': {'subsampling': 2, # "4:1:1" 'quantization': [ [20, 16, 25, 39, 50, 46, 62, 68, 16, 18, 23, 38, 38, 53, 65, 68, @@ -86,7 +86,7 @@ presets = { 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68] ]}, - 'web_medium': {'subsampling': 2, # "4:1:1" + 'web_medium': {'subsampling': 2, # "4:1:1" 'quantization': [ [16, 11, 11, 16, 23, 27, 31, 30, 11, 12, 12, 15, 20, 23, 23, 30, @@ -105,7 +105,7 @@ presets = { 38, 35, 46, 53, 64, 64, 64, 64, 48, 43, 53, 64, 64, 64, 64, 64] ]}, - 'web_high': {'subsampling': 0, # "4:4:4" + 'web_high': {'subsampling': 0, # "4:4:4" 'quantization': [ [ 6, 4, 4, 6, 9, 11, 12, 16, 4, 5, 5, 6, 8, 10, 12, 12, @@ -124,7 +124,7 @@ presets = { 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31] ]}, - 'web_very_high': {'subsampling': 0, # "4:4:4" + 'web_very_high': {'subsampling': 0, # "4:4:4" 'quantization': [ [ 2, 2, 2, 2, 3, 4, 5, 6, 2, 2, 2, 2, 3, 4, 5, 6, @@ -143,7 +143,7 @@ presets = { 15, 12, 12, 12, 12, 12, 12, 12, 15, 12, 12, 12, 12, 12, 12, 12] ]}, - 'web_maximum': {'subsampling': 0, # "4:4:4" + 'web_maximum': {'subsampling': 0, # "4:4:4" 'quantization': [ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -162,7 +162,7 @@ presets = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] ]}, - 'low': {'subsampling': 2, # "4:1:1" + 'low': {'subsampling': 2, # "4:1:1" 'quantization': [ [18, 14, 14, 21, 30, 35, 34, 17, 14, 16, 16, 19, 26, 23, 12, 12, @@ -181,7 +181,7 @@ presets = { 17, 12, 12, 12, 12, 12, 12, 12, 17, 12, 12, 12, 12, 12, 12, 12] ]}, - 'medium': {'subsampling': 2, # "4:1:1" + 'medium': {'subsampling': 2, # "4:1:1" 'quantization': [ [12, 8, 8, 12, 17, 21, 24, 17, 8, 9, 9, 11, 15, 19, 12, 12, @@ -200,7 +200,7 @@ presets = { 17, 12, 12, 12, 12, 12, 12, 12, 17, 12, 12, 12, 12, 12, 12, 12] ]}, - 'high': {'subsampling': 0, # "4:4:4" + 'high': {'subsampling': 0, # "4:4:4" 'quantization': [ [ 6, 4, 4, 6, 9, 11, 12, 16, 4, 5, 5, 6, 8, 10, 12, 12, @@ -219,7 +219,7 @@ presets = { 17, 12, 12, 12, 12, 12, 12, 12, 17, 12, 12, 12, 12, 12, 12, 12] ]}, - 'maximum': {'subsampling': 0, # "4:4:4" + 'maximum': {'subsampling': 0, # "4:4:4" 'quantization': [ [ 2, 2, 2, 2, 3, 4, 5, 6, 2, 2, 2, 2, 3, 4, 5, 6, diff --git a/PIL/MicImagePlugin.py b/PIL/MicImagePlugin.py index cdfaf3eda..aa41bf359 100644 --- a/PIL/MicImagePlugin.py +++ b/PIL/MicImagePlugin.py @@ -21,7 +21,7 @@ __version__ = "0.1" from PIL import Image, TiffImagePlugin -from PIL.OleFileIO import * +from PIL.OleFileIO import MAGIC, OleFileIO # @@ -54,9 +54,9 @@ class MicImageFile(TiffImagePlugin.TiffImageFile): # best way to identify MIC files, but what the... ;-) self.images = [] - for file in self.ole.listdir(): - if file[1:] and file[0][-4:] == ".ACI" and file[1] == "Image": - self.images.append(file) + for path in self.ole.listdir(): + if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image": + self.images.append(path) # if we didn't find any images, this is probably not # an MIC file. @@ -71,6 +71,10 @@ class MicImageFile(TiffImagePlugin.TiffImageFile): self.seek(0) + @property + def n_frames(self): + return len(self.images) + def seek(self, frame): try: diff --git a/PIL/MpegImagePlugin.py b/PIL/MpegImagePlugin.py index 9aca58f16..ff7c0dce4 100644 --- a/PIL/MpegImagePlugin.py +++ b/PIL/MpegImagePlugin.py @@ -22,7 +22,7 @@ from PIL._binary import i8 # # Bitstream parser -class BitStream: +class BitStream(object): def __init__(self, fp): self.fp = fp diff --git a/PIL/MpoImagePlugin.py b/PIL/MpoImagePlugin.py index c345fd327..9d21728b9 100644 --- a/PIL/MpoImagePlugin.py +++ b/PIL/MpoImagePlugin.py @@ -62,6 +62,10 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile): def load_seek(self, pos): self.__fp.seek(pos) + @property + def n_frames(self): + return self.__framecount + def seek(self, frame): if frame < 0 or frame >= self.__framecount: raise EOFError("no more images in MPO file") diff --git a/PIL/MspImagePlugin.py b/PIL/MspImagePlugin.py index 4753be7cd..1e974d53f 100644 --- a/PIL/MspImagePlugin.py +++ b/PIL/MspImagePlugin.py @@ -49,10 +49,10 @@ class MspImageFile(ImageFile.ImageFile): raise SyntaxError("not an MSP file") # Header checksum - sum = 0 + checksum = 0 for i in range(0, 32, 2): - sum = sum ^ i16(s[i:i+2]) - if sum != 0: + checksum = checksum ^ i16(s[i:i+2]) + if checksum != 0: raise SyntaxError("bad MSP checksum") self.mode = "1" @@ -83,10 +83,10 @@ def _save(im, fp, filename): header[6], header[7] = 1, 1 header[8], header[9] = im.size - sum = 0 + checksum = 0 for h in header: - sum = sum ^ h - header[12] = sum # FIXME: is this the right field? + checksum = checksum ^ h + header[12] = checksum # FIXME: is this the right field? # header for h in header: diff --git a/PIL/OleFileIO.py b/PIL/OleFileIO.py index c1cc5d5b6..4cf106d97 100755 --- a/PIL/OleFileIO.py +++ b/PIL/OleFileIO.py @@ -1,47 +1,70 @@ #!/usr/bin/env python -## OleFileIO_PL: -## Module to read Microsoft OLE2 files (also called Structured Storage or -## Microsoft Compound Document File Format), such as Microsoft Office -## documents, Image Composer and FlashPix files, Outlook messages, ... -## This version is compatible with Python 2.6+ and 3.x -## version 0.30 2014-02-04 Philippe Lagadec - http://www.decalage.info - -## Project website: http://www.decalage.info/python/olefileio - -## Improved version of the OleFileIO module from PIL library v1.1.6 -## See: http://www.pythonware.com/products/pil/index.htm - -## The Python Imaging Library (PIL) is - -## Copyright (c) 1997-2005 by Secret Labs AB -## Copyright (c) 1995-2005 by Fredrik Lundh - -## OleFileIO_PL changes are Copyright (c) 2005-2014 by Philippe Lagadec - -## See source code and LICENSE.txt for information on usage and redistribution. - -## WARNING: THIS IS (STILL) WORK IN PROGRESS. +# olefile (formerly OleFileIO_PL) version 0.42 2015-01-25 +# +# Module to read/write Microsoft OLE2 files (also called Structured Storage or +# Microsoft Compound Document File Format), such as Microsoft Office 97-2003 +# documents, Image Composer and FlashPix files, Outlook messages, ... +# This version is compatible with Python 2.6+ and 3.x +# +# Project website: http://www.decalage.info/olefile +# +# olefile is copyright (c) 2005-2015 Philippe Lagadec (http://www.decalage.info) +# +# olefile is based on the OleFileIO module from the PIL library v1.1.6 +# See: http://www.pythonware.com/products/pil/index.htm +# +# The Python Imaging Library (PIL) is +# Copyright (c) 1997-2005 by Secret Labs AB +# Copyright (c) 1995-2005 by Fredrik Lundh +# +# See source code and LICENSE.txt for information on usage and redistribution. -# Starting with OleFileIO_PL v0.30, only Python 2.6+ and 3.x is supported +# Since OleFileIO_PL v0.30, only Python 2.6+ and 3.x is supported # This import enables print() as a function rather than a keyword # (main requirement to be compatible with Python 3.x) # The comment on the line below should be printed on Python 2.5 or older: -from __future__ import print_function # This version of OleFileIO_PL requires Python 2.6+ or 3.x. +from __future__ import print_function # This version of olefile requires Python 2.6+ or 3.x. -__author__ = "Philippe Lagadec, Fredrik Lundh (Secret Labs AB)" -__date__ = "2014-02-04" -__version__ = '0.30' +__author__ = "Philippe Lagadec" +__date__ = "2015-01-25" +__version__ = '0.42b' #--- LICENSE ------------------------------------------------------------------ -# OleFileIO_PL is an improved version of the OleFileIO module from the -# Python Imaging Library (PIL). - -# OleFileIO_PL changes are Copyright (c) 2005-2014 by Philippe Lagadec +# olefile (formerly OleFileIO_PL) is copyright (c) 2005-2015 Philippe Lagadec +# (http://www.decalage.info) # +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# ---------- +# PIL License: +# +# olefile is based on source code from the OleFileIO module of the Python +# Imaging Library (PIL) published by Fredrik Lundh under the following license: + # The Python Imaging Library (PIL) is # Copyright (c) 1997-2005 by Secret Labs AB # Copyright (c) 1995-2005 by Fredrik Lundh @@ -67,7 +90,7 @@ __version__ = '0.30' # PERFORMANCE OF THIS SOFTWARE. #----------------------------------------------------------------------------- -# CHANGELOG: (only OleFileIO_PL changes compared to PIL 1.1.6) +# CHANGELOG: (only olefile/OleFileIO_PL changes compared to PIL 1.1.6) # 2005-05-11 v0.10 PL: - a few fixes for Python 2.4 compatibility # (all changes flagged with [PL]) # 2006-02-22 v0.11 PL: - a few fixes for some Office 2003 documents which raise @@ -142,10 +165,29 @@ __version__ = '0.30' # 2014-02-04 v0.30 PL: - upgraded code to support Python 3.x by Martin Panter # - several fixes for Python 2.6 (xrange, MAGIC) # - reused i32 from Pillow's _binary +# 2014-07-18 v0.31 - preliminary support for 4K sectors +# 2014-07-27 v0.31 PL: - a few improvements in OleFileIO.open (header parsing) +# - Fixed loadfat for large files with 4K sectors (issue #3) +# 2014-07-30 v0.32 PL: - added write_sect to write sectors to disk +# - added write_mode option to OleFileIO.__init__ and open +# 2014-07-31 PL: - fixed padding in write_sect for Python 3, added checks +# - added write_stream to write a stream to disk +# 2014-09-26 v0.40 PL: - renamed OleFileIO_PL to olefile +# 2014-11-09 NE: - added support for Jython (Niko Ehrenfeuchter) +# 2014-11-13 v0.41 PL: - improved isOleFile and OleFileIO.open to support OLE +# data in a string buffer and file-like objects. +# 2014-11-21 PL: - updated comments according to Pillow's commits +# 2015-01-24 v0.42 PL: - changed the default path name encoding from Latin-1 +# to UTF-8 on Python 2.x (Unicode on Python 3.x) +# - added path_encoding option to override the default +# - fixed a bug in _list when a storage is empty #----------------------------------------------------------------------------- # TODO (for version 1.0): -# + isOleFile should accept file-like objects like open +# + get rid of print statements, to simplify Python 2.x and 3.x support +# + add is_stream and is_storage +# + remove leading and trailing slashes where a path is used +# + add functions path_list2str and path_str2list # + fix how all the methods handle unicode str and/or bytes as arguments # + add path attrib to _OleDirEntry, set it once and for all in init or # append_kids (then listdir/_list can be simplified) @@ -177,30 +219,16 @@ __version__ = '0.30' # - move all debug code (and maybe dump methods) to a separate module, with # a class which inherits OleFileIO ? # - fix docstrings to follow epydoc format -# - add support for 4K sectors ? # - add support for big endian byte order ? # - create a simple OLE explorer with wxPython # FUTURE EVOLUTIONS to add write support: -# 1) add ability to write a stream back on disk from BytesIO (same size, no -# change in FAT/MiniFAT). -# 2) rename a stream/storage if it doesn't change the RB tree -# 3) use rbtree module to update the red-black tree + any rename -# 4) remove a stream/storage: free sectors in FAT/MiniFAT -# 5) allocate new sectors in FAT/MiniFAT -# 6) create new storage/stream -#----------------------------------------------------------------------------- +# see issue #6 on Bitbucket: +# https://bitbucket.org/decalage/olefileio_pl/issue/6/improve-olefileio_pl-to-write-ole-files + +#----------------------------------------------------------------------------- +# NOTES from PIL 1.1.6: -# -# THIS IS WORK IN PROGRESS -# -# The Python Imaging Library -# $Id$ -# -# stuff to deal with OLE2 Structured Storage files. this module is -# used by PIL to read Image Composer and FlashPix files, but can also -# be used to read other files of this type. -# # History: # 1997-01-20 fl Created # 1997-01-22 fl Fixed 64-bit portability quirk @@ -222,22 +250,22 @@ __version__ = '0.30' # "If this document and functionality of the Software conflict, # the actual functionality of the Software represents the correct # functionality" -- Microsoft, in the OLE format specification -# -# Copyright (c) Secret Labs AB 1997. -# Copyright (c) Fredrik Lundh 1997. -# -# See the README file for information on usage and redistribution. -# #------------------------------------------------------------------------------ import io import sys -import struct, array, os.path, datetime +import struct +import array +import os.path +import datetime -#[PL] Define explicitly the public API to avoid private objects in pydoc: -__all__ = ['OleFileIO', 'isOleFile', 'MAGIC'] +#=== COMPATIBILITY WORKAROUNDS ================================================ + +# [PL] Define explicitly the public API to avoid private objects in pydoc: +#TODO: add more +# __all__ = ['OleFileIO', 'isOleFile', 'MAGIC'] # For Python 3.x, need to redefine long as int: if str is not bytes: @@ -251,48 +279,73 @@ except: # no xrange, for Python 3 it was renamed as range: iterrange = range -#[PL] workaround to fix an issue with array item size on 64 bits systems: +# [PL] workaround to fix an issue with array item size on 64 bits systems: if array.array('L').itemsize == 4: # on 32 bits platforms, long integers in an array are 32 bits: UINT32 = 'L' elif array.array('I').itemsize == 4: # on 64 bits platforms, integers in an array are 32 bits: UINT32 = 'I' +elif array.array('i').itemsize == 4: + # On 64 bit Jython, signed integers ('i') are the only way to store our 32 + # bit values in an array in a *somewhat* reasonable way, as the otherwise + # perfectly suited 'H' (unsigned int, 32 bits) results in a completely + # unusable behaviour. This is most likely caused by the fact that Java + # doesn't have unsigned values, and thus Jython's "array" implementation, + # which is based on "jarray", doesn't have them either. + # NOTE: to trick Jython into converting the values it would normally + # interpret as "signed" into "unsigned", a binary-and operation with + # 0xFFFFFFFF can be used. This way it is possible to use the same comparing + # operations on all platforms / implementations. The corresponding code + # lines are flagged with a 'JYTHON-WORKAROUND' tag below. + UINT32 = 'i' else: raise ValueError('Need to fix a bug with 32 bit arrays, please contact author...') -#[PL] These workarounds were inspired from the Path module +# [PL] These workarounds were inspired from the Path module # (see http://www.jorendorff.com/articles/python/path/) -#TODO: test with old Python versions - -# Pre-2.3 workaround for basestring. try: basestring except NameError: - try: - # is Unicode supported (Python >2.0 or >1.6 ?) - basestring = (str, unicode) - except NameError: - basestring = str + basestring = str -#[PL] Experimental setting: if True, OLE filenames will be kept in Unicode +# [PL] Experimental setting: if True, OLE filenames will be kept in Unicode # if False (default PIL behaviour), all filenames are converted to Latin-1. -KEEP_UNICODE_NAMES = False +KEEP_UNICODE_NAMES = True -#[PL] DEBUG display mode: False by default, use set_debug_mode() or "-d" on +if sys.version_info[0] < 3: + # On Python 2.x, the default encoding for path names is UTF-8: + DEFAULT_PATH_ENCODING = 'utf-8' +else: + # On Python 3.x, the default encoding for path names is Unicode (None): + DEFAULT_PATH_ENCODING = None + + +#=== DEBUGGING =============================================================== + +#TODO: replace this by proper logging + +# [PL] DEBUG display mode: False by default, use set_debug_mode() or "-d" on # command line to change it. DEBUG_MODE = False + + def debug_print(msg): print(msg) + + def debug_pass(msg): pass + + debug = debug_pass + def set_debug_mode(debug_mode): """ Set debug mode on or off, to control display of debugging messages. - mode: True or False + :param mode: True or False """ global DEBUG_MODE, debug DEBUG_MODE = debug_mode @@ -301,41 +354,45 @@ def set_debug_mode(debug_mode): else: debug = debug_pass + +#=== CONSTANTS =============================================================== + +# magic bytes that should be at the beginning of every OLE file: MAGIC = b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1' -#[PL]: added constants for Sector IDs (from AAF specifications) -MAXREGSECT = 0xFFFFFFFA; # maximum SECT -DIFSECT = 0xFFFFFFFC; # (-4) denotes a DIFAT sector in a FAT -FATSECT = 0xFFFFFFFD; # (-3) denotes a FAT sector in a FAT -ENDOFCHAIN = 0xFFFFFFFE; # (-2) end of a virtual stream chain -FREESECT = 0xFFFFFFFF; # (-1) unallocated sector +# [PL]: added constants for Sector IDs (from AAF specifications) +MAXREGSECT = 0xFFFFFFFA # (-6) maximum SECT +DIFSECT = 0xFFFFFFFC # (-4) denotes a DIFAT sector in a FAT +FATSECT = 0xFFFFFFFD # (-3) denotes a FAT sector in a FAT +ENDOFCHAIN = 0xFFFFFFFE # (-2) end of a virtual stream chain +FREESECT = 0xFFFFFFFF # (-1) unallocated sector -#[PL]: added constants for Directory Entry IDs (from AAF specifications) -MAXREGSID = 0xFFFFFFFA; # maximum directory entry ID -NOSTREAM = 0xFFFFFFFF; # (-1) unallocated directory entry +# [PL]: added constants for Directory Entry IDs (from AAF specifications) +MAXREGSID = 0xFFFFFFFA # (-6) maximum directory entry ID +NOSTREAM = 0xFFFFFFFF # (-1) unallocated directory entry -#[PL] object types in storage (from AAF specifications) -STGTY_EMPTY = 0 # empty directory entry (according to OpenOffice.org doc) -STGTY_STORAGE = 1 # element is a storage object -STGTY_STREAM = 2 # element is a stream object -STGTY_LOCKBYTES = 3 # element is an ILockBytes object -STGTY_PROPERTY = 4 # element is an IPropertyStorage object -STGTY_ROOT = 5 # element is a root storage +# [PL] object types in storage (from AAF specifications) +STGTY_EMPTY = 0 # empty directory entry (according to OpenOffice.org doc) +STGTY_STORAGE = 1 # element is a storage object +STGTY_STREAM = 2 # element is a stream object +STGTY_LOCKBYTES = 3 # element is an ILockBytes object +STGTY_PROPERTY = 4 # element is an IPropertyStorage object +STGTY_ROOT = 5 # element is a root storage # # -------------------------------------------------------------------- # property types -VT_EMPTY=0; VT_NULL=1; VT_I2=2; VT_I4=3; VT_R4=4; VT_R8=5; VT_CY=6; -VT_DATE=7; VT_BSTR=8; VT_DISPATCH=9; VT_ERROR=10; VT_BOOL=11; -VT_VARIANT=12; VT_UNKNOWN=13; VT_DECIMAL=14; VT_I1=16; VT_UI1=17; -VT_UI2=18; VT_UI4=19; VT_I8=20; VT_UI8=21; VT_INT=22; VT_UINT=23; -VT_VOID=24; VT_HRESULT=25; VT_PTR=26; VT_SAFEARRAY=27; VT_CARRAY=28; -VT_USERDEFINED=29; VT_LPSTR=30; VT_LPWSTR=31; VT_FILETIME=64; -VT_BLOB=65; VT_STREAM=66; VT_STORAGE=67; VT_STREAMED_OBJECT=68; -VT_STORED_OBJECT=69; VT_BLOB_OBJECT=70; VT_CF=71; VT_CLSID=72; -VT_VECTOR=0x1000; +VT_EMPTY = 0; VT_NULL = 1; VT_I2 = 2; VT_I4 = 3; VT_R4 = 4; VT_R8 = 5; VT_CY = 6; +VT_DATE = 7; VT_BSTR = 8; VT_DISPATCH = 9; VT_ERROR = 10; VT_BOOL = 11; +VT_VARIANT = 12; VT_UNKNOWN = 13; VT_DECIMAL = 14; VT_I1 = 16; VT_UI1 = 17; +VT_UI2 = 18; VT_UI4 = 19; VT_I8 = 20; VT_UI8 = 21; VT_INT = 22; VT_UINT = 23; +VT_VOID = 24; VT_HRESULT = 25; VT_PTR = 26; VT_SAFEARRAY = 27; VT_CARRAY = 28; +VT_USERDEFINED = 29; VT_LPSTR = 30; VT_LPWSTR = 31; VT_FILETIME = 64; +VT_BLOB = 65; VT_STREAM = 66; VT_STORAGE = 67; VT_STREAMED_OBJECT = 68; +VT_STORED_OBJECT = 69; VT_BLOB_OBJECT = 70; VT_CF = 71; VT_CLSID = 72; +VT_VECTOR = 0x1000; # map property id to name (for debugging purposes) @@ -351,31 +408,53 @@ for keyword, var in list(vars().items()): WORD_CLSID = "00020900-0000-0000-C000-000000000046" #TODO: check Excel, PPT, ... -#[PL]: Defect levels to classify parsing errors - see OleFileIO._raise_defect() -DEFECT_UNSURE = 10 # a case which looks weird, but not sure it's a defect -DEFECT_POTENTIAL = 20 # a potential defect -DEFECT_INCORRECT = 30 # an error according to specifications, but parsing - # can go on -DEFECT_FATAL = 40 # an error which cannot be ignored, parsing is - # impossible +# [PL]: Defect levels to classify parsing errors - see OleFileIO._raise_defect() +DEFECT_UNSURE = 10 # a case which looks weird, but not sure it's a defect +DEFECT_POTENTIAL = 20 # a potential defect +DEFECT_INCORRECT = 30 # an error according to specifications, but parsing + # can go on +DEFECT_FATAL = 40 # an error which cannot be ignored, parsing is + # impossible -#[PL] add useful constants to __all__: -for key in list(vars().keys()): - if key.startswith('STGTY_') or key.startswith('DEFECT_'): - __all__.append(key) +# Minimal size of an empty OLE file, with 512-bytes sectors = 1536 bytes +# (this is used in isOleFile and OleFile.open) +MINIMAL_OLEFILE_SIZE = 1536 + +# [PL] add useful constants to __all__: +# for key in list(vars().keys()): +# if key.startswith('STGTY_') or key.startswith('DEFECT_'): +# __all__.append(key) -#--- FUNCTIONS ---------------------------------------------------------------- +#=== FUNCTIONS =============================================================== -def isOleFile (filename): +def isOleFile(filename): """ - Test if file is an OLE container (according to its header). - - :param filename: file name or path (str, unicode) + Test if a file is an OLE container (according to the magic bytes in its header). + + :param filename: string-like or file-like object, OLE file to parse + + - if filename is a string smaller than 1536 bytes, it is the path + of the file to open. (bytes or unicode string) + - if filename is a string longer than 1535 bytes, it is parsed + as the content of an OLE file in memory. (bytes type only) + - if filename is a file-like object (with read and seek methods), + it is parsed as-is. + :returns: True if OLE, False otherwise. """ - f = open(filename, 'rb') - header = f.read(len(MAGIC)) + # check if filename is a string-like or file-like object: + if hasattr(filename, 'read'): + # file-like object: use it directly + header = filename.read(len(MAGIC)) + # just in case, seek back to start of file: + filename.seek(0) + elif isinstance(filename, bytes) and len(filename) >= MINIMAL_OLEFILE_SIZE: + # filename is a bytes string containing the OLE file to be parsed: + header = filename[:len(MAGIC)] + else: + # string-like object: filename of file on disk + header = open(filename, 'rb').read(len(MAGIC)) if header == MAGIC: return True else: @@ -398,23 +477,20 @@ def i16(c, o = 0): """ Converts a 2-bytes (16 bits) string to an integer. - :param c: string containing bytes to convert - :param o: offset of bytes to convert in string + c: string containing bytes to convert + o: offset of bytes to convert in string """ - return i8(c[o]) | (i8(c[o+1])<<8) + return struct.unpack("=len(fat): + if sect < 0 or sect >= len(fat): debug('sect=%d (%X) / len(fat)=%d' % (sect, sect, len(fat))) - debug('i=%d / nb_sectors=%d' %(i, nb_sectors)) + debug('i=%d / nb_sectors=%d' % (i, nb_sectors)) ## tmp_data = b"".join(data) ## f = open('test_debug.bin', 'wb') ## f.write(tmp_data) @@ -723,7 +771,7 @@ class _OleStream(io.BytesIO): # Note: if sector is the last of the file, sometimes it is not a # complete sector (of 512 or 4K), so we may read less than # sectorsize. - if len(sector_data)!=sectorsize and sect!=(len(fat)-1): + if len(sector_data) != sectorsize and sect != (len(fat)-1): debug('sect=%d / len(fat)=%d, seek=%d / filesize=%d, len read=%d' % (sect, len(fat), offset+sectorsize*sect, filesize, len(sector_data))) debug('seek+len(read)=%d' % (offset+sectorsize*sect+len(sector_data))) @@ -731,11 +779,11 @@ class _OleStream(io.BytesIO): data.append(sector_data) # jump to next sector in the FAT: try: - sect = fat[sect] + sect = fat[sect] & 0xFFFFFFFF # JYTHON-WORKAROUND except IndexError: # [PL] if pointer is out of the FAT an exception is raised raise IOError('incorrect OLE FAT, sector index out of range') - #[PL] Last sector should be a "end of chain" marker: + # [PL] Last sector should be a "end of chain" marker: if sect != ENDOFCHAIN: raise IOError('incorrect last sector index in OLE stream') data = b"".join(data) @@ -759,12 +807,12 @@ class _OleStream(io.BytesIO): #--- _OleDirectoryEntry ------------------------------------------------------- -class _OleDirectoryEntry: +class _OleDirectoryEntry(object): """ OLE2 Directory Entry """ - #[PL] parsing code moved from OleFileIO.loaddirectory + # [PL] parsing code moved from OleFileIO.loaddirectory # struct to parse directory entries: # <: little-endian byte order, standard sizes @@ -789,7 +837,6 @@ class _OleDirectoryEntry: DIRENTRY_SIZE = 128 assert struct.calcsize(STRUCT_DIRENTRY) == DIRENTRY_SIZE - def __init__(self, entry, sid, olefile): """ Constructor for an _OleDirectoryEntry object. @@ -830,23 +877,26 @@ class _OleDirectoryEntry: sizeHigh ) = struct.unpack(_OleDirectoryEntry.STRUCT_DIRENTRY, entry) if self.entry_type not in [STGTY_ROOT, STGTY_STORAGE, STGTY_STREAM, STGTY_EMPTY]: - olefile._raise_defect(DEFECT_INCORRECT, 'unhandled OLE storage type') + olefile.raise_defect(DEFECT_INCORRECT, 'unhandled OLE storage type') # only first directory entry can (and should) be root: if self.entry_type == STGTY_ROOT and sid != 0: - olefile._raise_defect(DEFECT_INCORRECT, 'duplicate OLE root entry') + olefile.raise_defect(DEFECT_INCORRECT, 'duplicate OLE root entry') if sid == 0 and self.entry_type != STGTY_ROOT: - olefile._raise_defect(DEFECT_INCORRECT, 'incorrect OLE root entry') + olefile.raise_defect(DEFECT_INCORRECT, 'incorrect OLE root entry') #debug (struct.unpack(fmt_entry, entry[:len_entry])) # name should be at most 31 unicode characters + null character, # so 64 bytes in total (31*2 + 2): - if namelength>64: - olefile._raise_defect(DEFECT_INCORRECT, 'incorrect DirEntry name length') + if namelength > 64: + olefile.raise_defect(DEFECT_INCORRECT, 'incorrect DirEntry name length') # if exception not raised, namelength is set to the maximum value: namelength = 64 # only characters without ending null char are kept: name = name[:(namelength-2)] - # name is converted from unicode to Latin-1: - self.name = _unicode(name) + #TODO: check if the name is actually followed by a null unicode character ([MS-CFB] 2.6.1) + #TODO: check if the name does not contain forbidden characters: + # [MS-CFB] 2.6.1: "The following characters are illegal and MUST NOT be part of the name: '/', '\', ':', '!'." + # name is converted from UTF-16LE to the path encoding specified in the OleFileIO: + self.name = olefile._decode_utf16_str(name) debug('DirEntry SID=%d: %s' % (self.sid, repr(self.name))) debug(' - type: %d' % self.entry_type) @@ -861,29 +911,27 @@ class _OleDirectoryEntry: if sizeHigh != 0 and sizeHigh != 0xFFFFFFFF: debug('sectorsize=%d, sizeLow=%d, sizeHigh=%d (%X)' % (olefile.sectorsize, sizeLow, sizeHigh, sizeHigh)) - olefile._raise_defect(DEFECT_UNSURE, 'incorrect OLE stream size') + olefile.raise_defect(DEFECT_UNSURE, 'incorrect OLE stream size') self.size = sizeLow else: - self.size = sizeLow + (long(sizeHigh)<<32) + self.size = sizeLow + (long(sizeHigh) << 32) debug(' - size: %d (sizeLow=%d, sizeHigh=%d)' % (self.size, sizeLow, sizeHigh)) self.clsid = _clsid(clsid) # a storage should have a null size, BUT some implementations such as # Word 8 for Mac seem to allow non-null values => Potential defect: if self.entry_type == STGTY_STORAGE and self.size != 0: - olefile._raise_defect(DEFECT_POTENTIAL, 'OLE storage with size>0') + olefile.raise_defect(DEFECT_POTENTIAL, 'OLE storage with size>0') # check if stream is not already referenced elsewhere: - if self.entry_type in (STGTY_ROOT, STGTY_STREAM) and self.size>0: + if self.entry_type in (STGTY_ROOT, STGTY_STREAM) and self.size > 0: if self.size < olefile.minisectorcutoff \ - and self.entry_type==STGTY_STREAM: # only streams can be in MiniFAT + and self.entry_type == STGTY_STREAM: # only streams can be in MiniFAT # ministream object minifat = True else: minifat = False olefile._check_duplicate_stream(self.isectStart, minifat) - - def build_storage_tree(self): """ Read and build the red-black tree attached to this _OleDirectoryEntry @@ -907,23 +955,22 @@ class _OleDirectoryEntry: # (see rich comparison methods in this class) self.kids.sort() - def append_kids(self, child_sid): """ Walk through red-black tree of children of this directory entry to add all of them to the kids list. (recursive method) - child_sid : index of child directory entry to use, or None when called - first time for the root. (only used during recursion) + :param child_sid : index of child directory entry to use, or None when called + first time for the root. (only used during recursion) """ - #[PL] this method was added to use simple recursion instead of a complex + # [PL] this method was added to use simple recursion instead of a complex # algorithm. # if this is not a storage or a leaf of the tree, nothing to do: if child_sid == NOSTREAM: return # check if child SID is in the proper range: - if child_sid<0 or child_sid>=len(self.olefile.direntries): - self.olefile._raise_defect(DEFECT_FATAL, 'OLE DirEntry index out of range') + if child_sid < 0 or child_sid >= len(self.olefile.direntries): + self.olefile.raise_defect(DEFECT_FATAL, 'OLE DirEntry index out of range') # get child direntry: child = self.olefile._load_direntry(child_sid) #direntries[child_sid] debug('append_kids: child_sid=%d - %s - sid_left=%d, sid_right=%d, sid_child=%d' @@ -935,7 +982,7 @@ class _OleDirectoryEntry: # Check if its name is not already used (case-insensitive): name_lower = child.name.lower() if name_lower in self.kids_dict: - self.olefile._raise_defect(DEFECT_INCORRECT, + self.olefile.raise_defect(DEFECT_INCORRECT, "Duplicate filename in OLE storage") # Then the child_sid _OleDirectoryEntry object is appended to the # kids list and dictionary: @@ -943,7 +990,7 @@ class _OleDirectoryEntry: self.kids_dict[name_lower] = child # Check if kid was not already referenced in a storage: if child.used: - self.olefile._raise_defect(DEFECT_INCORRECT, + self.olefile.raise_defect(DEFECT_INCORRECT, 'OLE Entry referenced more than once') child.used = True # Finally walk through right side of the tree: @@ -951,7 +998,6 @@ class _OleDirectoryEntry: # Afterwards build kid's own tree if it's also a storage: child.build_storage_tree() - def __eq__(self, other): "Compare entries by name" return self.name == other.name @@ -971,7 +1017,6 @@ class _OleDirectoryEntry: #TODO: replace by the same function as MS implementation ? # (order by name length first, then case-insensitive order) - def dump(self, tab = 0): "Dump this entry, and all its subentries (for debug purposes only)" TYPES = ["(invalid)", "(storage)", "(stream)", "(lockbytes)", @@ -986,13 +1031,12 @@ class _OleDirectoryEntry: for kid in self.kids: kid.dump(tab + 2) - def getmtime(self): """ Return modification time of a directory entry. :returns: None if modification time is null, a python datetime object - otherwise (UTC timezone) + otherwise (UTC timezone) new in version 0.26 """ @@ -1000,13 +1044,12 @@ class _OleDirectoryEntry: return None return filetime2datetime(self.modifyTime) - def getctime(self): """ Return creation time of a directory entry. :returns: None if modification time is null, a python datetime object - otherwise (UTC timezone) + otherwise (UTC timezone) new in version 0.26 """ @@ -1017,7 +1060,7 @@ class _OleDirectoryEntry: #--- OleFileIO ---------------------------------------------------------------- -class OleFileIO: +class OleFileIO(object): """ OLE container object @@ -1047,35 +1090,60 @@ class OleFileIO: TIFF files). """ - def __init__(self, filename = None, raise_defects=DEFECT_FATAL): + def __init__(self, filename=None, raise_defects=DEFECT_FATAL, + write_mode=False, debug=False, path_encoding=DEFAULT_PATH_ENCODING): """ - Constructor for OleFileIO class. + Constructor for the OleFileIO class. :param filename: file to open. + + - if filename is a string smaller than 1536 bytes, it is the path + of the file to open. (bytes or unicode string) + - if filename is a string longer than 1535 bytes, it is parsed + as the content of an OLE file in memory. (bytes type only) + - if filename is a file-like object (with read, seek and tell methods), + it is parsed as-is. + :param raise_defects: minimal level for defects to be raised as exceptions. - (use DEFECT_FATAL for a typical application, DEFECT_INCORRECT for a - security-oriented application, see source code for details) + (use DEFECT_FATAL for a typical application, DEFECT_INCORRECT for a + security-oriented application, see source code for details) + + :param write_mode: bool, if True the file is opened in read/write mode instead + of read-only by default. + + :param debug: bool, set debug mode + + :param path_encoding: None or str, name of the codec to use for path + names (streams and storages), or None for Unicode. + Unicode by default on Python 3+, UTF-8 on Python 2.x. + (new in olefile 0.42, was hardcoded to Latin-1 until olefile v0.41) """ + set_debug_mode(debug) # minimal level for defects to be raised as exceptions: self._raise_defects_level = raise_defects # list of defects/issues not raised as exceptions: # tuples of (exception type, message) self.parsing_issues = [] + self.write_mode = write_mode + self.path_encoding = path_encoding + self._filesize = None + self.fp = None if filename: - self.open(filename) + self.open(filename, write_mode=write_mode) - - def _raise_defect(self, defect_level, message, exception_type=IOError): + def raise_defect(self, defect_level, message, exception_type=IOError): """ This method should be called for any defect found during file parsing. It may raise an IOError exception according to the minimal level chosen for the OleFileIO object. :param defect_level: defect level, possible values are: - DEFECT_UNSURE : a case which looks weird, but not sure it's a defect - DEFECT_POTENTIAL : a potential defect - DEFECT_INCORRECT : an error according to specifications, but parsing can go on - DEFECT_FATAL : an error which cannot be ignored, parsing is impossible + + - DEFECT_UNSURE : a case which looks weird, but not sure it's a defect + - DEFECT_POTENTIAL : a potential defect + - DEFECT_INCORRECT : an error according to specifications, but parsing can go on + - DEFECT_FATAL : an error which cannot be ignored, parsing is impossible + :param message: string describing the defect, used with raised exception. :param exception_type: exception class to be raised, IOError by default """ @@ -1086,32 +1154,68 @@ class OleFileIO: # just record the issue, no exception raised: self.parsing_issues.append((exception_type, message)) - - def open(self, filename): + def _decode_utf16_str(self, utf16_str, errors='replace'): """ - Open an OLE2 file. - Reads the header, FAT and directory. + Decode a string encoded in UTF-16 LE format, as found in the OLE + directory or in property streams. Return a string encoded + according to the path_encoding specified for the OleFileIO object. - :param filename: string-like or file-like object + :param utf16_str: bytes string encoded in UTF-16 LE format + :param errors: str, see python documentation for str.decode() + :return: str, encoded according to path_encoding """ - #[PL] check if filename is a string-like or file-like object: + unicode_str = utf16_str.decode('UTF-16LE', errors) + if self.path_encoding: + # an encoding has been specified for path names: + return unicode_str.encode(self.path_encoding, errors) + else: + # path_encoding=None, return the Unicode string as-is: + return unicode_str + + def open(self, filename, write_mode=False): + """ + Open an OLE2 file in read-only or read/write mode. + Read and parse the header, FAT and directory. + + :param filename: string-like or file-like object, OLE file to parse + + - if filename is a string smaller than 1536 bytes, it is the path + of the file to open. (bytes or unicode string) + - if filename is a string longer than 1535 bytes, it is parsed + as the content of an OLE file in memory. (bytes type only) + - if filename is a file-like object (with read, seek and tell methods), + it is parsed as-is. + + :param write_mode: bool, if True the file is opened in read/write mode instead + of read-only by default. (ignored if filename is not a path) + """ + self.write_mode = write_mode + # [PL] check if filename is a string-like or file-like object: # (it is better to check for a read() method) if hasattr(filename, 'read'): - # file-like object + #TODO: also check seek and tell methods? + # file-like object: use it directly self.fp = filename + elif isinstance(filename, bytes) and len(filename) >= MINIMAL_OLEFILE_SIZE: + # filename is a bytes string containing the OLE file to be parsed: + # convert it to BytesIO + self.fp = io.BytesIO(filename) else: # string-like object: filename of file on disk - #TODO: if larger than 1024 bytes, this could be the actual data => BytesIO - self.fp = open(filename, "rb") - # old code fails if filename is not a plain string: - #if isinstance(filename, (bytes, basestring)): - # self.fp = open(filename, "rb") - #else: - # self.fp = filename + if self.write_mode: + # open file in mode 'read with update, binary' + # According to https://docs.python.org/2/library/functions.html#open + # 'w' would truncate the file, 'a' may only append on some Unixes + mode = 'r+b' + else: + # read-only mode by default + mode = 'rb' + self.fp = open(filename, mode) # obtain the filesize by using seek and tell, which should work on most # file-like objects: #TODO: do it above, using getsize with filename when possible? #TODO: fix code to fail with clear exception when filesize cannot be obtained + filesize = 0 self.fp.seek(0, os.SEEK_END) try: filesize = self.fp.tell() @@ -1127,7 +1231,7 @@ class OleFileIO: header = self.fp.read(512) if len(header) != 512 or header[:8] != MAGIC: - self._raise_defect(DEFECT_FATAL, "not an OLE2 structured storage file") + self.raise_defect(DEFECT_FATAL, "not an OLE2 structured storage file") # [PL] header structure according to AAF specifications: ##Header @@ -1168,7 +1272,7 @@ class OleFileIO: # '<' indicates little-endian byte ordering for Intel (cf. struct module help) fmt_header = '<8s16sHHHHHHLLLLLLLLLL' header_size = struct.calcsize(fmt_header) - debug( "fmt_header size = %d, +FAT = %d" % (header_size, header_size + 109*4) ) + debug("fmt_header size = %d, +FAT = %d" % (header_size, header_size + 109*4)) header1 = header[:header_size] ( self.Sig, @@ -1189,61 +1293,78 @@ class OleFileIO: self.sectDifStart, self.csectDif ) = struct.unpack(fmt_header, header1) - debug( struct.unpack(fmt_header, header1)) + debug(struct.unpack(fmt_header, header1)) if self.Sig != MAGIC: # OLE signature should always be present - self._raise_defect(DEFECT_FATAL, "incorrect OLE signature") + self.raise_defect(DEFECT_FATAL, "incorrect OLE signature") if self.clsid != bytearray(16): # according to AAF specs, CLSID should always be zero - self._raise_defect(DEFECT_INCORRECT, "incorrect CLSID in OLE header") - debug( "MinorVersion = %d" % self.MinorVersion ) - debug( "DllVersion = %d" % self.DllVersion ) + self.raise_defect(DEFECT_INCORRECT, "incorrect CLSID in OLE header") + debug("MinorVersion = %d" % self.MinorVersion) + debug("DllVersion = %d" % self.DllVersion) if self.DllVersion not in [3, 4]: # version 3: usual format, 512 bytes per sector # version 4: large format, 4K per sector - self._raise_defect(DEFECT_INCORRECT, "incorrect DllVersion in OLE header") - debug( "ByteOrder = %X" % self.ByteOrder ) + self.raise_defect(DEFECT_INCORRECT, "incorrect DllVersion in OLE header") + debug("ByteOrder = %X" % self.ByteOrder) if self.ByteOrder != 0xFFFE: # For now only common little-endian documents are handled correctly - self._raise_defect(DEFECT_FATAL, "incorrect ByteOrder in OLE header") + self.raise_defect(DEFECT_FATAL, "incorrect ByteOrder in OLE header") # TODO: add big-endian support for documents created on Mac ? + # But according to [MS-CFB] ? v20140502, ByteOrder MUST be 0xFFFE. self.SectorSize = 2**self.SectorShift - debug( "SectorSize = %d" % self.SectorSize ) + debug("SectorSize = %d" % self.SectorSize) if self.SectorSize not in [512, 4096]: - self._raise_defect(DEFECT_INCORRECT, "incorrect SectorSize in OLE header") - if (self.DllVersion==3 and self.SectorSize!=512) \ - or (self.DllVersion==4 and self.SectorSize!=4096): - self._raise_defect(DEFECT_INCORRECT, "SectorSize does not match DllVersion in OLE header") + self.raise_defect(DEFECT_INCORRECT, "incorrect SectorSize in OLE header") + if (self.DllVersion == 3 and self.SectorSize != 512) \ + or (self.DllVersion == 4 and self.SectorSize != 4096): + self.raise_defect(DEFECT_INCORRECT, "SectorSize does not match DllVersion in OLE header") self.MiniSectorSize = 2**self.MiniSectorShift - debug( "MiniSectorSize = %d" % self.MiniSectorSize ) + debug("MiniSectorSize = %d" % self.MiniSectorSize) if self.MiniSectorSize not in [64]: - self._raise_defect(DEFECT_INCORRECT, "incorrect MiniSectorSize in OLE header") + self.raise_defect(DEFECT_INCORRECT, "incorrect MiniSectorSize in OLE header") if self.Reserved != 0 or self.Reserved1 != 0: - self._raise_defect(DEFECT_INCORRECT, "incorrect OLE header (non-null reserved bytes)") - debug( "csectDir = %d" % self.csectDir ) - if self.SectorSize==512 and self.csectDir!=0: - self._raise_defect(DEFECT_INCORRECT, "incorrect csectDir in OLE header") - debug( "csectFat = %d" % self.csectFat ) - debug( "sectDirStart = %X" % self.sectDirStart ) - debug( "signature = %d" % self.signature ) + self.raise_defect(DEFECT_INCORRECT, "incorrect OLE header (non-null reserved bytes)") + debug("csectDir = %d" % self.csectDir) + # Number of directory sectors (only allowed if DllVersion != 3) + if self.SectorSize == 512 and self.csectDir != 0: + self.raise_defect(DEFECT_INCORRECT, "incorrect csectDir in OLE header") + debug("csectFat = %d" % self.csectFat) + # csectFat = number of FAT sectors in the file + debug("sectDirStart = %X" % self.sectDirStart) + # sectDirStart = 1st sector containing the directory + debug("signature = %d" % self.signature) # Signature should be zero, BUT some implementations do not follow this # rule => only a potential defect: + # (according to MS-CFB, may be != 0 for applications supporting file + # transactions) if self.signature != 0: - self._raise_defect(DEFECT_POTENTIAL, "incorrect OLE header (signature>0)") - debug( "MiniSectorCutoff = %d" % self.MiniSectorCutoff ) - debug( "MiniFatStart = %X" % self.MiniFatStart ) - debug( "csectMiniFat = %d" % self.csectMiniFat ) - debug( "sectDifStart = %X" % self.sectDifStart ) - debug( "csectDif = %d" % self.csectDif ) + self.raise_defect(DEFECT_POTENTIAL, "incorrect OLE header (signature>0)") + debug("MiniSectorCutoff = %d" % self.MiniSectorCutoff) + # MS-CFB: This integer field MUST be set to 0x00001000. This field + # specifies the maximum size of a user-defined data stream allocated + # from the mini FAT and mini stream, and that cutoff is 4096 bytes. + # Any user-defined data stream larger than or equal to this cutoff size + # must be allocated as normal sectors from the FAT. + if self.MiniSectorCutoff != 0x1000: + self.raise_defect(DEFECT_INCORRECT, "incorrect MiniSectorCutoff in OLE header") + debug("MiniFatStart = %X" % self.MiniFatStart) + debug("csectMiniFat = %d" % self.csectMiniFat) + debug("sectDifStart = %X" % self.sectDifStart) + debug("csectDif = %d" % self.csectDif) # calculate the number of sectors in the file # (-1 because header doesn't count) - self.nb_sect = ( (filesize + self.SectorSize-1) // self.SectorSize) - 1 - debug( "Number of sectors in the file: %d" % self.nb_sect ) + self.nb_sect = ((filesize + self.SectorSize-1) // self.SectorSize) - 1 + debug("Number of sectors in the file: %d" % self.nb_sect) + #TODO: change this test, because an OLE file MAY contain other data + # after the last sector. - # file clsid (probably never used, so we don't store it) - clsid = _clsid(header[8:24]) + # file clsid + self.clsid = _clsid(header[8:24]) + + #TODO: remove redundant attributes, and fix the code which uses them? self.sectorsize = self.SectorSize #1 << i16(header, 30) self.minisectorsize = self.MiniSectorSize #1 << i16(header, 32) self.minisectorcutoff = self.MiniSectorCutoff # i32(header, 56) @@ -1260,27 +1381,26 @@ class OleFileIO: # Load file allocation tables self.loadfat(header) - # Load direcory. This sets both the direntries list (ordered by sid) + # Load directory. This sets both the direntries list (ordered by sid) # and the root (ordered by hierarchy) members. self.loaddirectory(self.sectDirStart)#i32(header, 48)) self.ministream = None self.minifatsect = self.MiniFatStart #i32(header, 60) - def close(self): """ close the OLE file, to release the file object """ self.fp.close() - def _check_duplicate_stream(self, first_sect, minifat=False): """ Checks if a stream has not been already referenced elsewhere. This method should only be called once for each known stream, and only if stream size is not null. - :param first_sect: index of first sector of the stream in FAT - :param minifat: if True, stream is located in the MiniFAT, else in the FAT + + :param first_sect: int, index of first sector of the stream in FAT + :param minifat: bool, if True, stream is located in the MiniFAT, else in the FAT """ if minifat: debug('_check_duplicate_stream: sect=%d in MiniFAT' % first_sect) @@ -1288,24 +1408,23 @@ class OleFileIO: else: debug('_check_duplicate_stream: sect=%d in FAT' % first_sect) # some values can be safely ignored (not a real stream): - if first_sect in (DIFSECT,FATSECT,ENDOFCHAIN,FREESECT): + if first_sect in (DIFSECT, FATSECT, ENDOFCHAIN, FREESECT): return used_streams = self._used_streams_fat #TODO: would it be more efficient using a dict or hash values, instead # of a list of long ? if first_sect in used_streams: - self._raise_defect(DEFECT_INCORRECT, 'Stream referenced twice') + self.raise_defect(DEFECT_INCORRECT, 'Stream referenced twice') else: used_streams.append(first_sect) - def dumpfat(self, fat, firstindex=0): "Displays a part of FAT in human-readable form for debugging purpose" # [PL] added only for debug if not DEBUG_MODE: return # dictionary to convert special FAT values in human-readable strings - VPL=8 # valeurs par ligne (8+1 * 8+1 = 81) + VPL = 8 # values per line (8+1 * 8+1 = 81) fatnames = { FREESECT: "..free..", ENDOFCHAIN: "[ END. ]", @@ -1322,26 +1441,28 @@ class OleFileIO: index = l*VPL print("%8X:" % (firstindex+index), end=" ") for i in range(index, index+VPL): - if i>=nbsect: + if i >= nbsect: break sect = fat[i] - if sect in fatnames: - nom = fatnames[sect] + aux = sect & 0xFFFFFFFF # JYTHON-WORKAROUND + if aux in fatnames: + name = fatnames[aux] else: if sect == i+1: - nom = " --->" + name = " --->" else: - nom = "%8X" % sect - print(nom, end=" ") + name = "%8X" % sect + print(name, end=" ") print() - def dumpsect(self, sector, firstindex=0): "Displays a sector in a human-readable form, for debugging purpose." if not DEBUG_MODE: return - VPL=8 # number of values per line (8+1 * 8+1 = 81) + VPL = 8 # number of values per line (8+1 * 8+1 = 81) tab = array.array(UINT32, sector) + if sys.byteorder == 'big': + tab.byteswap() nbsect = len(tab) nlines = (nbsect+VPL-1)//VPL print("index", end=" ") @@ -1352,11 +1473,11 @@ class OleFileIO: index = l*VPL print("%8X:" % (firstindex+index), end=" ") for i in range(index, index+VPL): - if i>=nbsect: + if i >= nbsect: break sect = tab[i] - nom = "%8X" % sect - print(nom, end=" ") + name = "%8X" % sect + print(name, end=" ") print() def sect2array(self, sect): @@ -1370,11 +1491,10 @@ class OleFileIO: a.byteswap() return a - def loadfat_sect(self, sect): """ Adds the indexes of the given sector to the FAT - + :param sect: string containing the first FAT sector, or array of long integers :returns: index of last FAT sector. """ @@ -1388,9 +1508,11 @@ class OleFileIO: self.dumpsect(sect) # The FAT is a sector chain starting at the first index of itself. for isect in fat1: - #print("isect = %X" % isect) + isect = isect & 0xFFFFFFFF # JYTHON-WORKAROUND + debug("isect = %X" % isect) if isect == ENDOFCHAIN or isect == FREESECT: # the end of the sector chain has been reached + debug("found end of sector chain") break # read the FAT sector s = self.getsect(isect) @@ -1400,17 +1522,17 @@ class OleFileIO: self.fat = self.fat + nextfat return isect - def loadfat(self, header): """ Load the FAT table. """ - # The header contains a sector numbers - # for the first 109 FAT sectors. Additional sectors are - # described by DIF blocks + # The 1st sector of the file contains sector numbers for the first 109 + # FAT sectors, right after the header which is 76 bytes long. + # (always 109, whatever the sector size: 512 bytes = 76+4*109) + # Additional sectors are described by DIF blocks sect = header[76:512] - debug( "len(sect)=%d, so %d integers" % (len(sect), len(sect)//4) ) + debug("len(sect)=%d, so %d integers" % (len(sect), len(sect)//4)) #fat = [] # [PL] FAT is an array of 32 bits unsigned ints, it's more effective # to use an array than a list in Python. @@ -1420,7 +1542,7 @@ class OleFileIO: #self.dumpfat(self.fat) ## for i in range(0, len(sect), 4): ## ix = i32(sect, i) -## #[PL] if ix == -2 or ix == -1: # ix == 0xFFFFFFFE or ix == 0xFFFFFFFF: +## # [PL] if ix == -2 or ix == -1: # ix == 0xFFFFFFFE or ix == 0xFFFFFFFF: ## if ix == 0xFFFFFFFE or ix == 0xFFFFFFFF: ## break ## s = self.getsect(ix) @@ -1432,28 +1554,31 @@ class OleFileIO: if self.csectFat <= 109: # there must be at least 109 blocks in header and the rest in # DIFAT, so number of sectors must be >109. - self._raise_defect(DEFECT_INCORRECT, 'incorrect DIFAT, not enough sectors') + self.raise_defect(DEFECT_INCORRECT, 'incorrect DIFAT, not enough sectors') if self.sectDifStart >= self.nb_sect: # initial DIFAT block index must be valid - self._raise_defect(DEFECT_FATAL, 'incorrect DIFAT, first index out of range') - debug( "DIFAT analysis..." ) + self.raise_defect(DEFECT_FATAL, 'incorrect DIFAT, first index out of range') + debug("DIFAT analysis...") # We compute the necessary number of DIFAT sectors : - # (each DIFAT sector = 127 pointers + 1 towards next DIFAT sector) - nb_difat = (self.csectFat-109 + 126)//127 - debug( "nb_difat = %d" % nb_difat ) + # Number of pointers per DIFAT sector = (sectorsize/4)-1 + # (-1 because the last pointer is the next DIFAT sector number) + nb_difat_sectors = (self.sectorsize//4)-1 + # (if 512 bytes: each DIFAT sector = 127 pointers + 1 towards next DIFAT sector) + nb_difat = (self.csectFat-109 + nb_difat_sectors-1)//nb_difat_sectors + debug("nb_difat = %d" % nb_difat) if self.csectDif != nb_difat: raise IOError('incorrect DIFAT') isect_difat = self.sectDifStart for i in iterrange(nb_difat): - debug( "DIFAT block %d, sector %X" % (i, isect_difat) ) + debug("DIFAT block %d, sector %X" % (i, isect_difat)) #TODO: check if corresponding FAT SID = DIFSECT sector_difat = self.getsect(isect_difat) difat = self.sect2array(sector_difat) self.dumpsect(sector_difat) - self.loadfat_sect(difat[:127]) + self.loadfat_sect(difat[:nb_difat_sectors]) # last DIFAT pointer is next DIFAT sector: - isect_difat = difat[127] - debug( "next DIFAT sector: %X" % isect_difat ) + isect_difat = difat[nb_difat_sectors] + debug("next DIFAT sector: %X" % isect_difat) # checks: if isect_difat not in [ENDOFCHAIN, FREESECT]: # last DIFAT pointer value must be ENDOFCHAIN or FREESECT @@ -1471,7 +1596,6 @@ class OleFileIO: debug('\nFAT:') self.dumpfat(self.fat) - def loadminifat(self): """ Load the MiniFAT table. @@ -1493,11 +1617,11 @@ class OleFileIO: (self.minifatsect, self.csectMiniFat, used_size, stream_size, nb_minisectors)) if used_size > stream_size: # This is not really a problem, but may indicate a wrong implementation: - self._raise_defect(DEFECT_INCORRECT, 'OLE MiniStream is larger than MiniFAT') + self.raise_defect(DEFECT_INCORRECT, 'OLE MiniStream is larger than MiniFAT') # In any case, first read stream_size: s = self._open(self.minifatsect, stream_size, force_FAT=True).read() - #[PL] Old code replaced by an array: - #self.minifat = [i32(s, i) for i in range(0, len(s), 4)] + # [PL] Old code replaced by an array: + # self.minifat = [i32(s, i) for i in range(0, len(s), 4)] self.minifat = self.sect2array(s) # Then shrink the array to used size, to avoid indexes out of MiniStream: debug('MiniFAT shrunk from %d to %d sectors' % (len(self.minifat), nb_minisectors)) @@ -1509,33 +1633,64 @@ class OleFileIO: def getsect(self, sect): """ Read given sector from file on disk. - - :param sect: sector index + + :param sect: int, sector index :returns: a string containing the sector data. """ - # [PL] this original code was wrong when sectors are 4KB instead of + # From [MS-CFB]: A sector number can be converted into a byte offset + # into the file by using the following formula: + # (sector number + 1) x Sector Size. + # This implies that sector #0 of the file begins at byte offset Sector + # Size, not at 0. + + # [PL] the original code in PIL was wrong when sectors are 4KB instead of # 512 bytes: - #self.fp.seek(512 + self.sectorsize * sect) - #[PL]: added safety checks: - #print("getsect(%X)" % sect) + # self.fp.seek(512 + self.sectorsize * sect) + # [PL]: added safety checks: + # print("getsect(%X)" % sect) try: self.fp.seek(self.sectorsize * (sect+1)) except: debug('getsect(): sect=%X, seek=%d, filesize=%d' % (sect, self.sectorsize*(sect+1), self._filesize)) - self._raise_defect(DEFECT_FATAL, 'OLE sector index out of range') + self.raise_defect(DEFECT_FATAL, 'OLE sector index out of range') sector = self.fp.read(self.sectorsize) if len(sector) != self.sectorsize: debug('getsect(): sect=%X, read=%d, sectorsize=%d' % (sect, len(sector), self.sectorsize)) - self._raise_defect(DEFECT_FATAL, 'incomplete OLE sector') + self.raise_defect(DEFECT_FATAL, 'incomplete OLE sector') return sector + def write_sect(self, sect, data, padding=b'\x00'): + """ + Write given sector to file on disk. + + :param sect: int, sector index + :param data: bytes, sector data + :param padding: single byte, padding character if data < sector size + """ + if not isinstance(data, bytes): + raise TypeError("write_sect: data must be a bytes string") + if not isinstance(padding, bytes) or len(padding) != 1: + raise TypeError("write_sect: padding must be a bytes string of 1 char") + #TODO: we could allow padding=None for no padding at all + try: + self.fp.seek(self.sectorsize * (sect+1)) + except: + debug('write_sect(): sect=%X, seek=%d, filesize=%d' % + (sect, self.sectorsize*(sect+1), self._filesize)) + self.raise_defect(DEFECT_FATAL, 'OLE sector index out of range') + if len(data) < self.sectorsize: + # add padding + data += padding * (self.sectorsize - len(data)) + elif len(data) < self.sectorsize: + raise ValueError("Data is larger than sector size") + self.fp.write(data) def loaddirectory(self, sect): """ Load the directory. - + :param sect: sector index of directory stream. """ # The directory is stored in a standard @@ -1545,14 +1700,14 @@ class OleFileIO: # (stream size is not known in advance) self.directory_fp = self._open(sect) - #[PL] to detect malformed documents and avoid DoS attacks, the maximum + # [PL] to detect malformed documents and avoid DoS attacks, the maximum # number of directory entries can be calculated: max_entries = self.directory_fp.size // 128 debug('loaddirectory: size=%d, max_entries=%d' % (self.directory_fp.size, max_entries)) # Create list of directory entries - #self.direntries = [] + # self.direntries = [] # We start with a list of "None" object self.direntries = [None] * max_entries ## for sid in iterrange(max_entries): @@ -1567,8 +1722,7 @@ class OleFileIO: # read and build all storage trees, starting from the root: self.root.build_storage_tree() - - def _load_direntry (self, sid): + def _load_direntry(self, sid): """ Load a directory entry from the directory. This method should only be called once for each storage/stream when @@ -1576,14 +1730,15 @@ class OleFileIO: :param sid: index of storage/stream in the directory. :returns: a _OleDirectoryEntry object + :exception IOError: if the entry has always been referenced. """ # check if SID is OK: - if sid<0 or sid>=len(self.direntries): - self._raise_defect(DEFECT_FATAL, "OLE directory index out of range") + if sid < 0 or sid >= len(self.direntries): + self.raise_defect(DEFECT_FATAL, "OLE directory index out of range") # check if entry was already referenced: if self.direntries[sid] is not None: - self._raise_defect(DEFECT_INCORRECT, + self.raise_defect(DEFECT_INCORRECT, "double reference for OLE stream/storage") # if exception not raised, return the object return self.direntries[sid] @@ -1592,14 +1747,12 @@ class OleFileIO: self.direntries[sid] = _OleDirectoryEntry(entry, sid, self) return self.direntries[sid] - def dumpdirectory(self): """ Dump directory (for debugging only) """ self.root.dump() - def _open(self, start, size = 0x7FFFFFFF, force_FAT=False): """ Open a stream, either in FAT or MiniFAT according to its size. @@ -1608,7 +1761,7 @@ class OleFileIO: :param start: index of first sector :param size: size of stream (or nothing if size is unknown) :param force_FAT: if False (default), stream will be opened in FAT or MiniFAT - according to size. If True, it will always be opened in FAT. + according to size. If True, it will always be opened in FAT. """ debug('OleFileIO.open(): sect=%d, size=%d, force_FAT=%s' % (start, size, str(force_FAT))) @@ -1625,67 +1778,71 @@ class OleFileIO: (self.root.isectStart, size_ministream)) self.ministream = self._open(self.root.isectStart, size_ministream, force_FAT=True) - return _OleStream(self.ministream, start, size, 0, - self.minisectorsize, self.minifat, - self.ministream.size) + return _OleStream(fp=self.ministream, sect=start, size=size, + offset=0, sectorsize=self.minisectorsize, + fat=self.minifat, filesize=self.ministream.size) else: # standard stream - return _OleStream(self.fp, start, size, 512, - self.sectorsize, self.fat, self._filesize) - + return _OleStream(fp=self.fp, sect=start, size=size, + offset=self.sectorsize, + sectorsize=self.sectorsize, fat=self.fat, + filesize=self._filesize) def _list(self, files, prefix, node, streams=True, storages=False): """ - (listdir helper) + listdir helper + :param files: list of files to fill in :param prefix: current location in storage tree (list of names) :param node: current node (_OleDirectoryEntry object) :param streams: bool, include streams if True (True by default) - new in v0.26 :param storages: bool, include storages if True (False by default) - new in v0.26 - (note: the root storage is never included) + (note: the root storage is never included) """ prefix = prefix + [node.name] for entry in node.kids: - if entry.kids: + if entry.entry_type == STGTY_STORAGE: # this is a storage if storages: # add it to the list files.append(prefix[1:] + [entry.name]) # check its kids self._list(files, prefix, entry, streams, storages) - else: + elif entry.entry_type == STGTY_STREAM: # this is a stream if streams: # add it to the list files.append(prefix[1:] + [entry.name]) - + else: + self.raise_defect(DEFECT_INCORRECT, 'The directory tree contains an entry which is not a stream nor a storage.') def listdir(self, streams=True, storages=False): """ - Return a list of streams stored in this file + Return a list of streams and/or storages stored in this file :param streams: bool, include streams if True (True by default) - new in v0.26 :param storages: bool, include storages if True (False by default) - new in v0.26 (note: the root storage is never included) + :returns: list of stream and/or storage paths """ files = [] self._list(files, [], self.root, streams, storages) return files - def _find(self, filename): """ Returns directory entry of given filename. (openstream helper) Note: this method is case-insensitive. :param filename: path of stream in storage tree (except root entry), either: - + - a string using Unix path syntax, for example: 'storage_1/storage_1.2/stream' - - a list of storage filenames, path to the desired stream/storage. + - or a list of storage filenames, path to the desired stream/storage. Example: ['storage_1', 'storage_1.2', 'stream'] + :returns: sid of requested filename - raise IOError if file not found + :exception IOError: if file not found """ # if filename is a string instead of a list, split it on slashes to @@ -1703,18 +1860,18 @@ class OleFileIO: node = kid return node.sid - def openstream(self, filename): """ Open a stream as a read-only file object (BytesIO). + Note: filename is case-insensitive. :param filename: path of stream in storage tree (except root entry), either: - + - a string using Unix path syntax, for example: 'storage_1/storage_1.2/stream' - - a list of storage filenames, path to the desired stream/storage. + - or a list of storage filenames, path to the desired stream/storage. Example: ['storage_1', 'storage_1.2', 'stream'] - + :returns: file object (read-only) :exception IOError: if filename not found, or if this is not a stream. """ @@ -1724,6 +1881,65 @@ class OleFileIO: raise IOError("this file is not a stream") return self._open(entry.isectStart, entry.size) + def write_stream(self, stream_name, data): + """ + Write a stream to disk. For now, it is only possible to replace an + existing stream by data of the same size. + + :param stream_name: path of stream in storage tree (except root entry), either: + + - a string using Unix path syntax, for example: + 'storage_1/storage_1.2/stream' + - or a list of storage filenames, path to the desired stream/storage. + Example: ['storage_1', 'storage_1.2', 'stream'] + + :param data: bytes, data to be written, must be the same size as the original + stream. + """ + if not isinstance(data, bytes): + raise TypeError("write_stream: data must be a bytes string") + sid = self._find(stream_name) + entry = self.direntries[sid] + if entry.entry_type != STGTY_STREAM: + raise IOError("this is not a stream") + size = entry.size + if size != len(data): + raise ValueError("write_stream: data must be the same size as the existing stream") + if size < self.minisectorcutoff: + raise NotImplementedError("Writing a stream in MiniFAT is not implemented yet") + sect = entry.isectStart + # number of sectors to write + nb_sectors = (size + (self.sectorsize-1)) // self.sectorsize + debug('nb_sectors = %d' % nb_sectors) + for i in range(nb_sectors): + # try: + # self.fp.seek(offset + self.sectorsize * sect) + # except: + # debug('sect=%d, seek=%d' % + # (sect, offset+self.sectorsize*sect)) + # raise IOError('OLE sector index out of range') + # extract one sector from data, the last one being smaller: + if i < (nb_sectors-1): + data_sector = data[i*self.sectorsize:(i+1)*self.sectorsize] + #TODO: comment this if it works + assert(len(data_sector) == self.sectorsize) + else: + data_sector = data[i*self.sectorsize:] + # TODO: comment this if it works + debug('write_stream: size=%d sectorsize=%d data_sector=%d size%%sectorsize=%d' + % (size, self.sectorsize, len(data_sector), size % self.sectorsize)) + assert(len(data_sector) % self.sectorsize == size % self.sectorsize) + self.write_sect(sect, data_sector) +# self.fp.write(data_sector) + # jump to next sector in the FAT: + try: + sect = self.fat[sect] + except IndexError: + # [PL] if pointer is out of the FAT an exception is raised + raise IOError('incorrect OLE FAT, sector index out of range') + # [PL] Last sector should be a "end of chain" marker: + if sect != ENDOFCHAIN: + raise IOError('incorrect last sector index in OLE stream') def get_type(self, filename): """ @@ -1732,7 +1948,7 @@ class OleFileIO: :param filename: path of stream in storage tree. (see openstream for syntax) :returns: False if object does not exist, its entry type (>0) otherwise: - + - STGTY_STREAM: a stream - STGTY_STORAGE: a storage - STGTY_ROOT: the root entry @@ -1744,7 +1960,6 @@ class OleFileIO: except: return False - def getmtime(self, filename): """ Return modification time of a stream/storage. @@ -1760,7 +1975,6 @@ class OleFileIO: entry = self.direntries[sid] return entry.getmtime() - def getctime(self, filename): """ Return creation time of a stream/storage. @@ -1776,11 +1990,11 @@ class OleFileIO: entry = self.direntries[sid] return entry.getctime() - def exists(self, filename): """ Test if given filename exists as a stream or a storage in the OLE container. + Note: filename is case-insensitive. :param filename: path of stream in storage tree. (see openstream for syntax) :returns: True if object exist, else False. @@ -1791,7 +2005,6 @@ class OleFileIO: except: return False - def get_size(self, filename): """ Return size of a stream in the OLE container, in bytes. @@ -1799,7 +2012,7 @@ class OleFileIO: :param filename: path of stream in storage tree (see openstream for syntax) :returns: size in bytes (long integer) :exception IOError: if file not found - :exception TypeError: if this is not a stream + :exception TypeError: if this is not a stream. """ sid = self._find(filename) entry = self.direntries[sid] @@ -1808,7 +2021,6 @@ class OleFileIO: raise TypeError('object is not an OLE stream') return entry.size - def get_rootentry_name(self): """ Return root entry name. Should usually be 'Root Entry' or 'R' in most @@ -1816,7 +2028,6 @@ class OleFileIO: """ return self.root.name - def getproperties(self, filename, convert_time=False, no_conversion=None): """ Return properties described in substream. @@ -1824,11 +2035,13 @@ class OleFileIO: :param filename: path of stream in storage tree (see openstream for syntax) :param convert_time: bool, if True timestamps will be converted to Python datetime :param no_conversion: None or list of int, timestamps not to be converted - (for example total editing time is not a real timestamp) + (for example total editing time is not a real timestamp) + :returns: a dictionary of values indexed by id (integer) """ + # REFERENCE: [MS-OLEPS] https://msdn.microsoft.com/en-us/library/dd942421.aspx # make sure no_conversion is a list, just to simplify code below: - if no_conversion == None: + if no_conversion is None: no_conversion = [] # stream path as a string to report exceptions: streampath = filename @@ -1859,7 +2072,7 @@ class OleFileIO: # a fatal error when parsing the whole file msg = 'Error while parsing properties header in stream %s: %s' % ( repr(streampath), exc) - self._raise_defect(DEFECT_INCORRECT, msg, type(exc)) + self.raise_defect(DEFECT_INCORRECT, msg, type(exc)) return data for i in range(num_props): @@ -1869,7 +2082,7 @@ class OleFileIO: offset = i32(s, 12+i*8) type = i32(s, offset) - debug ('property id=%d: type=%d offset=%X' % (id, type, offset)) + debug('property id=%d: type=%d offset=%X' % (id, type, offset)) # test for common types first (should perhaps use # a dictionary instead?) @@ -1878,14 +2091,14 @@ class OleFileIO: value = i16(s, offset+4) if value >= 32768: value = value - 65536 - elif type == VT_UI2: # 2-byte unsigned integer + elif type == VT_UI2: # 2-byte unsigned integer value = i16(s, offset+4) elif type in (VT_I4, VT_INT, VT_ERROR): # VT_I4: 32-bit signed integer # VT_ERROR: HRESULT, similar to 32-bit signed integer, # see http://msdn.microsoft.com/en-us/library/cc230330.aspx value = i32(s, offset+4) - elif type in (VT_UI4, VT_UINT): # 4-byte unsigned integer + elif type in (VT_UI4, VT_UINT): # 4-byte unsigned integer value = i32(s, offset+4) # FIXME elif type in (VT_BSTR, VT_LPSTR): # CodePageString, see http://msdn.microsoft.com/en-us/library/dd942354.aspx @@ -1907,14 +2120,14 @@ class OleFileIO: # "the string should NOT contain embedded or additional trailing # null characters." count = i32(s, offset+4) - value = _unicode(s[offset+8:offset+8+count*2]) + value = self._decode_utf16_str(s[offset+8:offset+8+count*2]) elif type == VT_FILETIME: - value = long(i32(s, offset+4)) + (long(i32(s, offset+8))<<32) + value = long(i32(s, offset+4)) + (long(i32(s, offset+8)) << 32) # FILETIME is a 64-bit int: "number of 100ns periods # since Jan 1,1601". if convert_time and id not in no_conversion: debug('Converting property #%d to python datetime, value=%d=%fs' - %(id, value, float(value)/10000000)) + % (id, value, float(value) / 10000000)) # convert FILETIME to Python datetime.datetime # inspired from http://code.activestate.com/recipes/511425-filetime-to-datetime/ _FILETIME_null_date = datetime.datetime(1601, 1, 1, 0, 0, 0) @@ -1939,7 +2152,7 @@ class OleFileIO: value = bool(i16(s, offset+4)) else: value = None # everything else yields "None" - debug ('property id=%d: type=%d not implemented in parser yet' % (id, type)) + debug('property id=%d: type=%d not implemented in parser yet' % (id, type)) # missing: VT_EMPTY, VT_NULL, VT_R4, VT_R8, VT_CY, VT_DATE, # VT_DECIMAL, VT_I1, VT_I8, VT_UI8, @@ -1951,8 +2164,8 @@ class OleFileIO: # type of items, e.g. VT_VECTOR|VT_BSTR # see http://msdn.microsoft.com/en-us/library/dd942011.aspx - #print("%08x" % id, repr(value), end=" ") - #print("(%s)" % VT[i32(s, offset) & 0xFFF]) + # print("%08x" % id, repr(value), end=" ") + # print("(%s)" % VT[i32(s, offset) & 0xFFF]) data[id] = value except BaseException as exc: @@ -1960,7 +2173,7 @@ class OleFileIO: # a DEFECT_INCORRECT, because parsing can go on msg = 'Error while parsing property id %d in stream %s: %s' % ( id, repr(streampath), exc) - self._raise_defect(DEFECT_INCORRECT, msg, type(exc)) + self.raise_defect(DEFECT_INCORRECT, msg, type(exc)) return data @@ -1983,107 +2196,110 @@ class OleFileIO: if __name__ == "__main__": - import sys - # [PL] display quick usage info if launched from command-line if len(sys.argv) <= 1: - print(__doc__) - print(""" -Launched from command line, this script parses OLE files and prints info. + print('olefile version %s %s - %s' % (__version__, __date__, __author__)) + print( +""" +Launched from the command line, this script parses OLE files and prints info. -Usage: OleFileIO_PL.py [-d] [-c] [file2 ...] +Usage: olefile.py [-d] [-c] [file2 ...] Options: --d : debug mode (display a lot of debug information, for developers only) +-d : debug mode (displays a lot of debug information, for developers only) -c : check all streams (for debugging purposes) + +For more information, see http://www.decalage.info/olefile """) sys.exit() check_streams = False for filename in sys.argv[1:]: -## try: - # OPTIONS: - if filename == '-d': - # option to switch debug mode on: - set_debug_mode(True) - continue - if filename == '-c': - # option to switch check streams mode on: - check_streams = True - continue + # try: + # OPTIONS: + if filename == '-d': + # option to switch debug mode on: + set_debug_mode(True) + continue + if filename == '-c': + # option to switch check streams mode on: + check_streams = True + continue - ole = OleFileIO(filename)#, raise_defects=DEFECT_INCORRECT) - print("-" * 68) - print(filename) - print("-" * 68) - ole.dumpdirectory() + ole = OleFileIO(filename)#, raise_defects=DEFECT_INCORRECT) + print("-" * 68) + print(filename) + print("-" * 68) + ole.dumpdirectory() + for streamname in ole.listdir(): + if streamname[-1][0] == "\005": + print(streamname, ": properties") + props = ole.getproperties(streamname, convert_time=True) + props = sorted(props.items()) + for k, v in props: + # [PL]: avoid to display too large or binary values: + if isinstance(v, (basestring, bytes)): + if len(v) > 50: + v = v[:50] + if isinstance(v, bytes): + # quick and dirty binary check: + for c in (1, 2, 3, 4, 5, 6, 7, 11, 12, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31): + if c in bytearray(v): + v = '(binary data)' + break + print(" ", k, v) + + if check_streams: + # Read all streams to check if there are errors: + print('\nChecking streams...') for streamname in ole.listdir(): - if streamname[-1][0] == "\005": - print(streamname, ": properties") - props = ole.getproperties(streamname, convert_time=True) - props = sorted(props.items()) - for k, v in props: - #[PL]: avoid to display too large or binary values: - if isinstance(v, (basestring, bytes)): - if len(v) > 50: - v = v[:50] - if isinstance(v, bytes): - # quick and dirty binary check: - for c in (1,2,3,4,5,6,7,11,12,14,15,16,17,18,19,20, - 21,22,23,24,25,26,27,28,29,30,31): - if c in bytearray(v): - v = '(binary data)' - break - print(" ", k, v) - - if check_streams: - # Read all streams to check if there are errors: - print('\nChecking streams...') - for streamname in ole.listdir(): - # print name using repr() to convert binary chars to \xNN: - print('-', repr('/'.join(streamname)),'-', end=' ') - st_type = ole.get_type(streamname) - if st_type == STGTY_STREAM: - print('size %d' % ole.get_size(streamname)) - # just try to read stream in memory: - ole.openstream(streamname) - else: - print('NOT a stream : type=%d' % st_type) - print() - -## for streamname in ole.listdir(): -## # print name using repr() to convert binary chars to \xNN: -## print('-', repr('/'.join(streamname)),'-', end=' ') -## print(ole.getmtime(streamname)) -## print() - - print('Modification/Creation times of all directory entries:') - for entry in ole.direntries: - if entry is not None: - print('- %s: mtime=%s ctime=%s' % (entry.name, - entry.getmtime(), entry.getctime())) + # print name using repr() to convert binary chars to \xNN: + print('-', repr('/'.join(streamname)), '-', end=' ') + st_type = ole.get_type(streamname) + if st_type == STGTY_STREAM: + print('size %d' % ole.get_size(streamname)) + # just try to read stream in memory: + ole.openstream(streamname) + else: + print('NOT a stream : type=%d' % st_type) print() - # parse and display metadata: - meta = ole.get_metadata() - meta.dump() - print() - #[PL] Test a few new methods: - root = ole.get_rootentry_name() - print('Root entry name: "%s"' % root) - if ole.exists('worddocument'): - print("This is a Word document.") - print("type of stream 'WordDocument':", ole.get_type('worddocument')) - print("size :", ole.get_size('worddocument')) - if ole.exists('macros/vba'): - print("This document may contain VBA macros.") +# for streamname in ole.listdir(): +# # print name using repr() to convert binary chars to \xNN: +# print('-', repr('/'.join(streamname)),'-', end=' ') +# print(ole.getmtime(streamname)) +# print() - # print parsing issues: - print('\nNon-fatal issues raised during parsing:') - if ole.parsing_issues: - for exctype, msg in ole.parsing_issues: - print('- %s: %s' % (exctype.__name__, msg)) - else: - print('None') + print('Modification/Creation times of all directory entries:') + for entry in ole.direntries: + if entry is not None: + print('- %s: mtime=%s ctime=%s' % (entry.name, + entry.getmtime(), entry.getctime())) + print() + + # parse and display metadata: + meta = ole.get_metadata() + meta.dump() + print() + # [PL] Test a few new methods: + root = ole.get_rootentry_name() + print('Root entry name: "%s"' % root) + if ole.exists('worddocument'): + print("This is a Word document.") + print("type of stream 'WordDocument':", ole.get_type('worddocument')) + print("size :", ole.get_size('worddocument')) + if ole.exists('macros/vba'): + print("This document may contain VBA macros.") + + # print parsing issues: + print('\nNon-fatal issues raised during parsing:') + if ole.parsing_issues: + for exctype, msg in ole.parsing_issues: + print('- %s: %s' % (exctype.__name__, msg)) + else: + print('None') ## except IOError as v: ## print("***", "cannot read", file, "-", v) + +# this code was developed while listening to The Wedding Present "Sea Monsters" diff --git a/PIL/PSDraw.py b/PIL/PSDraw.py index 5a24441a8..a090a8f64 100644 --- a/PIL/PSDraw.py +++ b/PIL/PSDraw.py @@ -15,15 +15,13 @@ # See the README file for information on usage and redistribution. # -from __future__ import print_function - from PIL import EpsImagePlugin ## # Simple Postscript graphics interface. -class PSDraw: +class PSDraw(object): """ Sets up printing to the given file. If **file** is omitted, :py:attr:`sys.stdout` is assumed. @@ -35,25 +33,31 @@ class PSDraw: fp = sys.stdout self.fp = fp + def _fp_write(self, to_write): + if bytes is str: + self.fp.write(to_write) + else: + self.fp.write(bytes(to_write, 'UTF-8')) + def begin_document(self, id=None): """Set up printing of a document. (Write Postscript DSC header.)""" # FIXME: incomplete - self.fp.write("%!PS-Adobe-3.0\n" - "save\n" - "/showpage { } def\n" - "%%EndComments\n" - "%%BeginDocument\n") - # self.fp.write(ERROR_PS) # debugging! - self.fp.write(EDROFF_PS) - self.fp.write(VDI_PS) - self.fp.write("%%EndProlog\n") + self._fp_write("%!PS-Adobe-3.0\n" + "save\n" + "/showpage { } def\n" + "%%EndComments\n" + "%%BeginDocument\n") + # self.fp_write(ERROR_PS) # debugging! + self._fp_write(EDROFF_PS) + self._fp_write(VDI_PS) + self._fp_write("%%EndProlog\n") self.isofont = {} def end_document(self): """Ends printing. (Write Postscript DSC footer.)""" - self.fp.write("%%EndDocument\n" - "restore showpage\n" - "%%End\n") + self._fp_write("%%EndDocument\n" + "restore showpage\n" + "%%End\n") if hasattr(self.fp, "flush"): self.fp.flush() @@ -66,18 +70,11 @@ class PSDraw: """ if font not in self.isofont: # reencode font - self.fp.write("/PSDraw-%s ISOLatin1Encoding /%s E\n" % - (font, font)) + self._fp_write("/PSDraw-%s ISOLatin1Encoding /%s E\n" % + (font, font)) self.isofont[font] = 1 # rough - self.fp.write("/F0 %d /PSDraw-%s F\n" % (size, font)) - - def setink(self, ink): - """ - .. warning:: This has been in the PIL API for ages but was never implemented. - - """ - print("*** NOT YET IMPLEMENTED ***") + self._fp_write("/F0 %d /PSDraw-%s F\n" % (size, font)) def line(self, xy0, xy1): """ @@ -86,7 +83,7 @@ class PSDraw: left corner of the page). """ xy = xy0 + xy1 - self.fp.write("%d %d %d %d Vl\n" % xy) + self._fp_write("%d %d %d %d Vl\n" % xy) def rectangle(self, box): """ @@ -101,7 +98,7 @@ class PSDraw: %d %d M %d %d 0 Vr\n """ - self.fp.write("%d %d M %d %d 0 Vr\n" % box) + self._fp_write("%d %d M %d %d 0 Vr\n" % box) def text(self, xy, text): """ @@ -111,7 +108,7 @@ class PSDraw: text = "\\(".join(text.split("(")) text = "\\)".join(text.split(")")) xy = xy + (text,) - self.fp.write("%d %d M (%s) S\n" % xy) + self._fp_write("%d %d M (%s) S\n" % xy) def image(self, box, im, dpi=None): """Draw a PIL image, centered in the given box.""" @@ -135,14 +132,14 @@ class PSDraw: y = ymax dx = (xmax - x) / 2 + box[0] dy = (ymax - y) / 2 + box[1] - self.fp.write("gsave\n%f %f translate\n" % (dx, dy)) + self._fp_write("gsave\n%f %f translate\n" % (dx, dy)) if (x, y) != im.size: # EpsImagePlugin._save prints the image at (0,0,xsize,ysize) sx = x / im.size[0] sy = y / im.size[1] - self.fp.write("%f %f scale\n" % (sx, sy)) + self._fp_write("%f %f scale\n" % (sx, sy)) EpsImagePlugin._save(im, self.fp, None, 0) - self.fp.write("\ngrestore\n") + self._fp_write("\ngrestore\n") # -------------------------------------------------------------------- # Postscript driver diff --git a/PIL/PaletteFile.py b/PIL/PaletteFile.py index 37ba4cbff..ef50feefd 100644 --- a/PIL/PaletteFile.py +++ b/PIL/PaletteFile.py @@ -19,7 +19,7 @@ from PIL._binary import o8 ## # File handler for Teragon-style palette files. -class PaletteFile: +class PaletteFile(object): rawmode = "RGB" diff --git a/PIL/PcdImagePlugin.py b/PIL/PcdImagePlugin.py index 5ce7aa48c..06db5d628 100644 --- a/PIL/PcdImagePlugin.py +++ b/PIL/PcdImagePlugin.py @@ -52,25 +52,6 @@ class PcdImageFile(ImageFile.ImageFile): self.size = 768, 512 # FIXME: not correct for rotated images! self.tile = [("pcd", (0, 0)+self.size, 96*2048, None)] - def draft(self, mode, size): - - if len(self.tile) != 1: - return - - d, e, o, a = self.tile[0] - - if size: - scale = max(self.size[0] / size[0], self.size[1] / size[1]) - for s, o in [(4, 0*2048), (2, 0*2048), (1, 96*2048)]: - if scale >= s: - break - # e = e[0], e[1], (e[2]-e[0]+s-1)/s+e[0], (e[3]-e[1]+s-1)/s+e[1] - # self.size = ((self.size[0]+s-1)/s, (self.size[1]+s-1)/s) - - self.tile = [(d, e, o, a)] - - return self - # # registry diff --git a/PIL/PcfFontFile.py b/PIL/PcfFontFile.py index c19a1c532..c2006905e 100644 --- a/PIL/PcfFontFile.py +++ b/PIL/PcfFontFile.py @@ -204,7 +204,7 @@ class PcfFontFile(FontFile.FontFile): for i in range(4): bitmapSizes.append(i32(fp.read(4))) - byteorder = format & 4 # non-zero => MSB + # byteorder = format & 4 # non-zero => MSB bitorder = format & 8 # non-zero => MSB padindex = format & 3 diff --git a/PIL/PcxImagePlugin.py b/PIL/PcxImagePlugin.py index 0765f099e..40fafa0dc 100644 --- a/PIL/PcxImagePlugin.py +++ b/PIL/PcxImagePlugin.py @@ -25,7 +25,7 @@ # See the README file for information on usage and redistribution. # -__version__ = "0.6" +from __future__ import print_function from PIL import Image, ImageFile, ImagePalette, _binary @@ -33,6 +33,8 @@ i8 = _binary.i8 i16 = _binary.i16le o8 = _binary.o8 +__version__ = "0.6" + def _accept(prefix): return i8(prefix[0]) == 10 and i8(prefix[1]) in [0, 2, 3, 5] @@ -58,7 +60,7 @@ class PcxImageFile(ImageFile.ImageFile): if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]: raise SyntaxError("bad PCX image size") if Image.DEBUG: - print ("BBox: %s %s %s %s" % bbox) + print("BBox: %s %s %s %s" % bbox) # format version = i8(s[1]) @@ -66,8 +68,8 @@ class PcxImageFile(ImageFile.ImageFile): planes = i8(s[65]) stride = i16(s, 66) if Image.DEBUG: - print ("PCX version %s, bits %s, planes %s, stride %s" % - (version, bits, planes, stride)) + print("PCX version %s, bits %s, planes %s, stride %s" % + (version, bits, planes, stride)) self.info["dpi"] = i16(s, 12), i16(s, 14) @@ -106,7 +108,7 @@ class PcxImageFile(ImageFile.ImageFile): bbox = (0, 0) + self.size if Image.DEBUG: - print ("size: %sx%s" % self.size) + print("size: %sx%s" % self.size) self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))] @@ -143,7 +145,7 @@ def _save(im, fp, filename, check=0): # gets overwritten. if Image.DEBUG: - print ("PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d" % ( + print("PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d" % ( im.size[0], bits, stride)) # under windows, we could determine the current screen size with diff --git a/PIL/PdfImagePlugin.py b/PIL/PdfImagePlugin.py index 5113f099e..1d8c2ff93 100644 --- a/PIL/PdfImagePlugin.py +++ b/PIL/PdfImagePlugin.py @@ -63,7 +63,7 @@ def _save(im, fp, filename): xref = [0]*(5+1) # placeholders - class TextWriter: + class TextWriter(object): def __init__(self, fp): self.fp = fp diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 2110aa637..214ff9385 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -71,12 +71,27 @@ _MODES = { _simple_palette = re.compile(b'^\xff+\x00\xff*$') +_null_palette = re.compile(b'^\x00*$') + +# Maximum decompressed size for a iTXt or zTXt chunk. +# Eliminates decompression bombs where compressed chunks can expand 1000x +MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK +# Set the maximum total text chunk size. +MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK + + +def _safe_zlib_decompress(s): + dobj = zlib.decompressobj() + plaintext = dobj.decompress(s, MAX_TEXT_CHUNK) + if dobj.unconsumed_tail: + raise ValueError("Decompressed Data Too Large") + return plaintext # -------------------------------------------------------------------- # Support classes. Suitable for PNG and related formats like MNG etc. -class ChunkStream: +class ChunkStream(object): def __init__(self, fp): @@ -149,31 +164,56 @@ class ChunkStream: return cids -# -------------------------------------------------------------------- -# Subclass of string to allow iTXt chunks to look like strings while -# keeping their extra information - class iTXt(str): + """ + Subclass of string to allow iTXt chunks to look like strings while + keeping their extra information + + """ @staticmethod def __new__(cls, text, lang, tkey): + """ + :param value: value for this key + :param lang: language code + :param tkey: UTF-8 version of the key name + """ + self = str.__new__(cls, text) self.lang = lang self.tkey = tkey return self -# -------------------------------------------------------------------- -# PNG chunk container (for use with save(pnginfo=)) +class PngInfo(object): + """ + PNG chunk container (for use with save(pnginfo=)) -class PngInfo: + """ def __init__(self): self.chunks = [] def add(self, cid, data): + """Appends an arbitrary chunk. Use with caution. + + :param cid: a byte string, 4 bytes long. + :param data: a byte string of the encoded data + + """ + self.chunks.append((cid, data)) def add_itxt(self, key, value, lang="", tkey="", zip=False): + """Appends an iTXt chunk. + + :param key: latin-1 encodable text key name + :param value: value for this key + :param lang: language code + :param tkey: UTF-8 version of the key name + :param zip: compression flag + + """ + if not isinstance(key, bytes): key = key.encode("latin-1", "strict") if not isinstance(value, bytes): @@ -184,7 +224,6 @@ class PngInfo: tkey = tkey.encode("utf-8", "strict") if zip: - import zlib self.add(b"iTXt", key + b"\0\x01\0" + lang + b"\0" + tkey + b"\0" + zlib.compress(value)) else: @@ -192,6 +231,14 @@ class PngInfo: value) def add_text(self, key, value, zip=0): + """Appends a text chunk. + + :param key: latin-1 encodable text key name + :param value: value for this key, text or an + :py:class:`PIL.PngImagePlugin.iTXt` instance + :param zip: compression flag + + """ if isinstance(value, iTXt): return self.add_itxt(key, value, value.lang, value.tkey, bool(zip)) @@ -206,7 +253,6 @@ class PngInfo: key = key.encode('latin-1', 'strict') if zip: - import zlib self.add(b"zTXt", key + b"\0\0" + zlib.compress(value)) else: self.add(b"tEXt", key + b"\0" + value) @@ -229,6 +275,14 @@ class PngStream(ChunkStream): self.im_tile = None self.im_palette = None + self.text_memory = 0 + + def check_text_memory(self, chunklen): + self.text_memory += chunklen + if self.text_memory > MAX_TEXT_MEMORY: + raise ValueError("Too much memory used in text chunks: %s>MAX_TEXT_MEMORY" % + self.text_memory) + def chunk_iCCP(self, pos, length): # ICC profile @@ -247,7 +301,7 @@ class PngStream(ChunkStream): raise SyntaxError("Unknown compression method %s in iCCP chunk" % comp_method) try: - icc_profile = zlib.decompress(s[i+2:]) + icc_profile = _safe_zlib_decompress(s[i+2:]) except zlib.error: icc_profile = None # FIXME self.im_info["icc_profile"] = icc_profile @@ -297,6 +351,8 @@ class PngStream(ChunkStream): i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i + elif _null_palette.match(s): + self.im_info["transparency"] = 0 else: self.im_info["transparency"] = s elif self.im_mode == "L": @@ -341,6 +397,8 @@ class PngStream(ChunkStream): v = v.decode('latin-1', 'replace') self.im_info[k] = self.im_text[k] = v + self.check_text_memory(len(v)) + return s def chunk_zTXt(self, pos, length): @@ -359,9 +417,8 @@ class PngStream(ChunkStream): if comp_method != 0: raise SyntaxError("Unknown compression method %s in zTXt chunk" % comp_method) - import zlib try: - v = zlib.decompress(v[1:]) + v = _safe_zlib_decompress(v[1:]) except zlib.error: v = b"" @@ -371,6 +428,8 @@ class PngStream(ChunkStream): v = v.decode('latin-1', 'replace') self.im_info[k] = self.im_text[k] = v + self.check_text_memory(len(v)) + return s def chunk_iTXt(self, pos, length): @@ -390,9 +449,8 @@ class PngStream(ChunkStream): return s if cf != 0: if cm == 0: - import zlib try: - v = zlib.decompress(v) + v = _safe_zlib_decompress(v) except zlib.error: return s else: @@ -407,6 +465,7 @@ class PngStream(ChunkStream): return s self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk) + self.check_text_memory(len(v)) return s @@ -564,7 +623,7 @@ def putchunk(fp, cid, *data): fp.write(o16(hi) + o16(lo)) -class _idat: +class _idat(object): # wrap output from the encoder in IDAT chunks def __init__(self, fp, chunk): @@ -674,10 +733,6 @@ def _save(im, fp, filename, chunk=putchunk, check=0): alpha_bytes = 2**bits chunk(fp, b"tRNS", alpha[:alpha_bytes]) - if 0: - # FIXME: to be supported some day - chunk(fp, b"gAMA", o32(int(gamma * 100000.0))) - dpi = im.encoderinfo.get("dpi") if dpi: chunk(fp, b"pHYs", @@ -719,7 +774,7 @@ def _save(im, fp, filename, chunk=putchunk, check=0): def getchunks(im, **params): """Return a list of PNG chunks representing this image.""" - class collector: + class collector(object): data = [] def write(self, data): diff --git a/PIL/PsdImagePlugin.py b/PIL/PsdImagePlugin.py index 02c94a860..d30695adb 100644 --- a/PIL/PsdImagePlugin.py +++ b/PIL/PsdImagePlugin.py @@ -132,6 +132,10 @@ class PsdImageFile(ImageFile.ImageFile): self._fp = self.fp self.frame = 0 + @property + def n_frames(self): + return len(self.layers) + def seek(self, layer): # seek to given layer (1..max) if layer == self.frame: diff --git a/PIL/PyAccess.py b/PIL/PyAccess.py index 93aeae59e..4924facd5 100644 --- a/PIL/PyAccess.py +++ b/PIL/PyAccess.py @@ -51,10 +51,10 @@ class PyAccess(object): self.ysize = vals['ysize'] if DEBUG: - print (vals) + print(vals) self._post_init() - def _post_init(): + def _post_init(self): pass def __setitem__(self, xy, color): @@ -78,6 +78,8 @@ class PyAccess(object): images :param xy: The pixel coordinate, given as (x, y). + :returns: a pixel value for single band images, a tuple of + pixel values for multiband images. """ (x, y) = self.check_xy(xy) @@ -192,7 +194,7 @@ class _PyAccessI16_L(PyAccess): pixel = self.pixels[y][x] try: color = min(color, 65535) - except: + except TypeError: color = min(color[0], 65535) pixel.l = color & 0xFF diff --git a/PIL/SpiderImagePlugin.py b/PIL/SpiderImagePlugin.py index 306b348bc..7de5156b1 100644 --- a/PIL/SpiderImagePlugin.py +++ b/PIL/SpiderImagePlugin.py @@ -48,7 +48,7 @@ def isInt(f): return 1 else: return 0 - except: + except ValueError: return 0 iforms = [1, 3, -11, -12, -21, -22] @@ -127,12 +127,12 @@ class SpiderImageFile(ImageFile.ImageFile): if self.istack == 0 and self.imgnumber == 0: # stk=0, img=0: a regular 2D image offset = hdrlen - self.nimages = 1 + self._nimages = 1 elif self.istack > 0 and self.imgnumber == 0: # stk>0, img=0: Opening the stack for the first time self.imgbytes = int(h[12]) * int(h[2]) * 4 self.hdrlen = hdrlen - self.nimages = int(h[26]) + self._nimages = int(h[26]) # Point to the first image in the stack offset = hdrlen * 2 self.imgnumber = 1 @@ -154,6 +154,10 @@ class SpiderImageFile(ImageFile.ImageFile): (self.rawmode, 0, 1))] self.__fp = self.fp # FIXME: hack + @property + def n_frames(self): + return self._nimages + # 1st image index is zero (although SPIDER imgnumber starts at 1) def tell(self): if self.imgnumber < 1: @@ -164,7 +168,7 @@ class SpiderImageFile(ImageFile.ImageFile): def seek(self, frame): if self.istack == 0: return - if frame >= self.nimages: + if frame >= self._nimages: raise EOFError("attempt to seek past end of file") self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes) self.fp = self.__fp @@ -173,11 +177,11 @@ class SpiderImageFile(ImageFile.ImageFile): # returns a byte image after rescaling to 0..255 def convert2byte(self, depth=255): - (min, max) = self.getextrema() + (minimum, maximum) = self.getextrema() m = 1 - if max != min: - m = depth / (max-min) - b = -m * min + if maximum != minimum: + m = depth / (maximum-minimum) + b = -m * minimum return self.point(lambda i, m=m, b=b: i * m + b).convert("L") # returns a ImageTk.PhotoImage object, after rescaling to 0..255 @@ -271,7 +275,7 @@ def _save(im, fp, filename): def _save_spider(im, fp, filename): # get the filename extension and register it with Image - fn, ext = os.path.splitext(filename) + ext = os.path.splitext(filename)[1] Image.register_extension("SPIDER", ext) _save(im, fp, filename) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 4e76379c0..59de84273 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -54,6 +54,7 @@ import sys import collections import itertools import os +import io # Set these to true to force use of libtiff for reading or writing. READ_LIBTIFF = False @@ -149,6 +150,7 @@ OPEN_INFO = { (II, 0, 1, 2, (8,), ()): ("L", "L;IR"), (II, 0, 3, 1, (32,), ()): ("F", "F;32F"), (II, 1, 1, 1, (1,), ()): ("1", "1"), + (II, 1, 1, 1, (4,), ()): ("L", "L;4"), (II, 1, 1, 2, (1,), ()): ("1", "1;R"), (II, 1, 1, 1, (8,), ()): ("L", "L"), (II, 1, 1, 1, (8, 8), (2,)): ("LA", "LA"), @@ -292,7 +294,7 @@ class ImageFileDirectory(collections.MutableMapping): def named(self): """ - Returns the complete tag dictionary, with named tags where posible. + Returns the complete tag dictionary, with named tags where possible. """ from PIL import TiffTags result = {} @@ -424,6 +426,11 @@ class ImageFileDirectory(collections.MutableMapping): for i in range(i16(fp.read(2))): ifd = fp.read(12) + if len(ifd) != 12: + warnings.warn("Possibly corrupt EXIF data. " + "Expecting to read 12 bytes but only got %d." + % (len(ifd))) + continue tag, typ = i16(ifd), i16(ifd, 2) @@ -449,10 +456,10 @@ class ImageFileDirectory(collections.MutableMapping): if size > 4: here = fp.tell() if Image.DEBUG: - print ("Tag Location: %s" %here) + print("Tag Location: %s" % here) fp.seek(i32(ifd, 8)) if Image.DEBUG: - print ("Data Location: %s" %fp.tell()) + print("Data Location: %s" % fp.tell()) data = ImageFile._safe_read(fp, size) fp.seek(here) else: @@ -474,7 +481,14 @@ class ImageFileDirectory(collections.MutableMapping): else: print("- value:", self[tag]) - self.next = i32(fp.read(4)) + ifd = fp.read(4) + if len(ifd) != 4: + warnings.warn("Possibly corrupt EXIF data. " + "Expecting to read 4 bytes but only got %d." + % (len(ifd))) + return + + self.next = i32(ifd) # save primitives @@ -504,7 +518,7 @@ class ImageFileDirectory(collections.MutableMapping): typ = self.tagtype[tag] if Image.DEBUG: - print ("Tag %s, Type: %s, Value: %s" % (tag, typ, value)) + print("Tag %s, Type: %s, Value: %s" % (tag, typ, value)) if typ == 1: # byte data @@ -515,6 +529,15 @@ class ImageFileDirectory(collections.MutableMapping): elif typ == 7: # untyped data data = value = b"".join(value) + elif typ in (11, 12): + # float value + tmap = {11: 'f', 12: 'd'} + if not isinstance(value, tuple): + value = (value,) + a = array.array(tmap[typ], value) + if self.prefix != native_prefix: + a.byteswap() + data = a.tostring() elif isStringType(value[0]): # string data if isinstance(value, tuple): @@ -625,54 +648,63 @@ class TiffImageFile(ImageFile.ImageFile): self.__first = self.__next = self.ifd.i32(ifh, 4) self.__frame = -1 self.__fp = self.fp + self._frame_pos = [] + self._n_frames = None if Image.DEBUG: - print ("*** TiffImageFile._open ***") - print ("- __first:", self.__first) - print ("- ifh: ", ifh) + print("*** TiffImageFile._open ***") + print("- __first:", self.__first) + print("- ifh: ", ifh) # and load the first frame self._seek(0) + @property + def n_frames(self): + if self._n_frames is None: + current = self.tell() + try: + while True: + self._seek(self.tell() + 1) + except EOFError: + self._n_frames = self.tell() + 1 + self.seek(current) + return self._n_frames + def seek(self, frame): "Select a given frame as current image" - if frame < 0: - frame = 0 - self._seek(frame) + self._seek(max(frame, 0)) # Questionable backwards compatibility. # Create a new core image object on second and # subsequent frames in the image. Image may be # different size/mode. Image._decompression_bomb_check(self.size) self.im = Image.core.new(self.mode, self.size) - def tell(self): - "Return the current frame number" - return self._tell() - def _seek(self, frame): self.fp = self.__fp - if frame < self.__frame: - # rewind file - self.__frame = -1 - self.__next = self.__first - while self.__frame < frame: + while len(self._frame_pos) <= frame: if not self.__next: raise EOFError("no more images in TIFF file") if Image.DEBUG: - print("Seeking to frame %s, on frame %s, __next %s, location: %s"% - (frame, self.__frame, self.__next, self.fp.tell())) + print("Seeking to frame %s, on frame %s, __next %s, location: %s" % + (frame, self.__frame, self.__next, self.fp.tell())) # reset python3 buffered io handle in case fp # was passed to libtiff, invalidating the buffer self.fp.tell() self.fp.seek(self.__next) + self._frame_pos.append(self.__next) if Image.DEBUG: - print("Loading tags, location: %s"%self.fp.tell()) + print("Loading tags, location: %s" % self.fp.tell()) self.tag.load(self.fp) self.__next = self.tag.next self.__frame += 1 + self.fp.seek(self._frame_pos[frame]) + self.tag.load(self.fp) + self.__frame = frame self._setup() - - def _tell(self): + + def tell(self): + "Return the current frame number" return self.__frame def _decoder(self, rawmode, layer, tile=None): @@ -720,8 +752,8 @@ class TiffImageFile(ImageFile.ImageFile): # (self._compression, (extents tuple), # 0, (rawmode, self._compression, fp)) - ignored, extents, ignored_2, args = self.tile[0] - args = args + (self.ifd.offset,) + extents = self.tile[0][1] + args = self.tile[0][3] + (self.ifd.offset,) decoder = Image._getdecoder(self.mode, 'libtiff', args, self.decoderconfig) try: @@ -738,21 +770,21 @@ class TiffImageFile(ImageFile.ImageFile): # # Rearranging for supporting byteio items, since they have a fileno # that returns an IOError if there's no underlying fp. Easier to - # dea. with here by reordering. + # deal with here by reordering. if Image.DEBUG: - print ("have getvalue. just sending in a string from getvalue") + print("have getvalue. just sending in a string from getvalue") n, err = decoder.decode(self.fp.getvalue()) elif hasattr(self.fp, "fileno"): # we've got a actual file on disk, pass in the fp. if Image.DEBUG: - print ("have fileno, calling fileno version of the decoder.") + print("have fileno, calling fileno version of the decoder.") self.fp.seek(0) # 4 bytes, otherwise the trace might error out n, err = decoder.decode(b"fpfp") else: # we have something else. if Image.DEBUG: - print ("don't have fileno or getvalue. just reading") + print("don't have fileno or getvalue. just reading") # UNDONE -- so much for that buffer size thing. n, err = decoder.decode(self.fp.read()) @@ -932,7 +964,7 @@ class TiffImageFile(ImageFile.ImageFile): (0, min(y, ysize), w, min(y+h, ysize)), offsets[i], a)) if Image.DEBUG: - print ("tiles: ", self.tile) + print("tiles: ", self.tile) y = y + h if y >= self.size[1]: x = y = 0 @@ -967,14 +999,14 @@ class TiffImageFile(ImageFile.ImageFile): # fixup palette descriptor if self.mode == "P": - palette = [o8(a // 256) for a in self.tag[COLORMAP]] + palette = [o8(b // 256) for b in self.tag[COLORMAP]] self.palette = ImagePalette.raw("RGB;L", b"".join(palette)) # # -------------------------------------------------------------------- # Write TIFF files # little endian is default except for image modes with -# explict big endian byte-order +# explicit big endian byte-order SAVE_INFO = { # mode => rawmode, byteorder, photometrics, @@ -1066,31 +1098,25 @@ def _save(im, fp, filename): if "icc_profile" in im.info: ifd[ICCPROFILE] = im.info["icc_profile"] - if "description" in im.encoderinfo: - ifd[IMAGEDESCRIPTION] = im.encoderinfo["description"] - if "resolution" in im.encoderinfo: - ifd[X_RESOLUTION] = ifd[Y_RESOLUTION] \ - = _cvt_res(im.encoderinfo["resolution"]) - if "x resolution" in im.encoderinfo: - ifd[X_RESOLUTION] = _cvt_res(im.encoderinfo["x resolution"]) - if "y resolution" in im.encoderinfo: - ifd[Y_RESOLUTION] = _cvt_res(im.encoderinfo["y resolution"]) - if "resolution unit" in im.encoderinfo: - unit = im.encoderinfo["resolution unit"] - if unit == "inch": - ifd[RESOLUTION_UNIT] = 2 - elif unit == "cm" or unit == "centimeter": - ifd[RESOLUTION_UNIT] = 3 - else: - ifd[RESOLUTION_UNIT] = 1 - if "software" in im.encoderinfo: - ifd[SOFTWARE] = im.encoderinfo["software"] - if "date time" in im.encoderinfo: - ifd[DATE_TIME] = im.encoderinfo["date time"] - if "artist" in im.encoderinfo: - ifd[ARTIST] = im.encoderinfo["artist"] - if "copyright" in im.encoderinfo: - ifd[COPYRIGHT] = im.encoderinfo["copyright"] + for key, name, cvt in [ + (IMAGEDESCRIPTION, "description", lambda x: x), + (X_RESOLUTION, "resolution", _cvt_res), + (Y_RESOLUTION, "resolution", _cvt_res), + (X_RESOLUTION, "x_resolution", _cvt_res), + (Y_RESOLUTION, "y_resolution", _cvt_res), + (RESOLUTION_UNIT, "resolution_unit", + lambda x: {"inch": 2, "cm": 3, "centimeter": 3}.get(x, 1)), + (SOFTWARE, "software", lambda x: x), + (DATE_TIME, "date_time", lambda x: x), + (ARTIST, "artist", lambda x: x), + (COPYRIGHT, "copyright", lambda x: x)]: + name_with_spaces = name.replace("_", " ") + if "_" in name and name_with_spaces in im.encoderinfo: + warnings.warn("%r is deprecated; use %r instead" % + (name_with_spaces, name), DeprecationWarning) + ifd[key] = cvt(im.encoderinfo[name.replace("_", " ")]) + if name in im.encoderinfo: + ifd[key] = cvt(im.encoderinfo[name]) dpi = im.encoderinfo.get("dpi") if dpi: @@ -1123,12 +1149,15 @@ def _save(im, fp, filename): if libtiff: if Image.DEBUG: - print ("Saving using libtiff encoder") - print (ifd.items()) + print("Saving using libtiff encoder") + print(ifd.items()) _fp = 0 if hasattr(fp, "fileno"): - fp.seek(0) - _fp = os.dup(fp.fileno()) + try: + fp.seek(0) + _fp = os.dup(fp.fileno()) + except io.UnsupportedOperation: + pass # ICC Profile crashes. blocklist = [STRIPOFFSETS, STRIPBYTECOUNTS, ROWSPERSTRIP, ICCPROFILE] @@ -1153,8 +1182,11 @@ def _save(im, fp, filename): # following tiffcp.c->cpTag->TIFF_RATIONAL atts[k] = float(v[0][0])/float(v[0][1]) continue - if type(v) == tuple and len(v) > 2: + if (type(v) == tuple and + (len(v) > 2 or + (len(v) == 2 and v[1] == 0))): # List of ints? + # Avoid divide by zero in next if-clause if type(v[0]) in (int, float): atts[k] = list(v) continue @@ -1175,7 +1207,7 @@ def _save(im, fp, filename): atts[k] = v if Image.DEBUG: - print (atts) + print(atts) # libtiff always expects the bytes in native order. # we're storing image byte order. So, if the rawmode diff --git a/PIL/WmfImagePlugin.py b/PIL/WmfImagePlugin.py index 6146c1560..bdbbc72f0 100644 --- a/PIL/WmfImagePlugin.py +++ b/PIL/WmfImagePlugin.py @@ -37,7 +37,7 @@ def register_handler(handler): if hasattr(Image.core, "drawwmf"): # install default handler (windows only) - class WmfHandler: + class WmfHandler(object): def open(self, im): im.mode = "RGB" diff --git a/PIL/__init__.py b/PIL/__init__.py index 1bb1250c8..6d51a5dcb 100644 --- a/PIL/__init__.py +++ b/PIL/__init__.py @@ -12,7 +12,7 @@ # ;-) VERSION = '1.1.7' # PIL version -PILLOW_VERSION = '2.6.0' # Pillow +PILLOW_VERSION = '2.9.0.dev0' # Pillow _plugins = ['BmpImagePlugin', 'BufrStubImagePlugin', diff --git a/PIL/_binary.py b/PIL/_binary.py index 51ce45a79..2f5e8ffd4 100644 --- a/PIL/_binary.py +++ b/PIL/_binary.py @@ -11,6 +11,8 @@ # See the README file for information on usage and redistribution. # +from struct import unpack, pack + if bytes is str: def i8(c): return ord(c) @@ -34,7 +36,7 @@ def i16le(c, o=0): c: string containing bytes to convert o: offset of bytes to convert in string """ - return i8(c[o]) | (i8(c[o+1]) << 8) + return unpack("H", c[o:o+2])[0] def i32be(c, o=0): - return ((i8(c[o]) << 24) | (i8(c[o+1]) << 16) | - (i8(c[o+2]) << 8) | i8(c[o+3])) + return unpack(">I", c[o:o+4])[0] # Output, le = little endian, be = big endian def o16le(i): - return o8(i) + o8(i >> 8) + return pack("> 8) + o8(i >> 16) + o8(i >> 24) + return pack("> 8) + o8(i) + return pack(">H", i) def o32be(i): - return o8(i >> 24) + o8(i >> 16) + o8(i >> 8) + o8(i) + return pack(">I", i) # End of file diff --git a/PIL/features.py b/PIL/features.py new file mode 100644 index 000000000..fd87f094f --- /dev/null +++ b/PIL/features.py @@ -0,0 +1,67 @@ +from PIL import Image + +modules = { + "pil": "PIL._imaging", + "tkinter": "PIL._imagingtk", + "freetype2": "PIL._imagingft", + "littlecms2": "PIL._imagingcms", + "webp": "PIL._webp", + "transp_webp": ("WEBP", "WebPDecoderBuggyAlpha") +} + + +def check_module(feature): + if feature not in modules: + raise ValueError("Unknown module %s" % feature) + + module = modules[feature] + + method_to_call = None + if type(module) is tuple: + module, method_to_call = module + + try: + imported_module = __import__(module) + except ImportError: + # If a method is being checked, None means that + # rather than the method failing, the module required for the method + # failed to be imported first + return None if method_to_call else False + + if method_to_call: + method = getattr(imported_module, method_to_call) + return method() is True + else: + return True + + +def get_supported_modules(): + supported_modules = [] + for feature in modules: + if check_module(feature): + supported_modules.append(feature) + return supported_modules + +codecs = { + "jpg": "jpeg", + "jpg_2000": "jpeg2k", + "zlib": "zip", + "libtiff": "libtiff" +} + + +def check_codec(feature): + if feature not in codecs: + raise ValueError("Unknown codec %s" % feature) + + codec = codecs[feature] + + return codec + "_encoder" in dir(Image.core) + + +def get_supported_codecs(): + supported_codecs = [] + for feature in codecs: + if check_codec(feature): + supported_codecs.append(feature) + return supported_codecs diff --git a/README.rst b/README.rst index dfd3e2c31..be50c5531 100644 --- a/README.rst +++ b/README.rst @@ -1,19 +1,23 @@ Pillow ====== -*Python Imaging Library (Fork)* +Python Imaging Library (Fork) +----------------------------- -Pillow is the "friendly" PIL fork by `Alex Clark and Contributors `_. PIL is the Python Imaging Library by Fredrik Lundh and Contributors. For more information, please `read the documentation `_, `check the changelog `_ and `find out how to contribute `_. +Pillow is the "friendly PIL fork" by `Alex Clark and Contributors `_. PIL is the Python Imaging Library by Fredrik Lundh and Contributors. -.. image:: https://travis-ci.org/python-pillow/Pillow.svg?branch=master +.. + image:: https://travis-ci.org/python-pillow/Pillow.svg?branch=master :target: https://travis-ci.org/python-pillow/Pillow - :alt: Travis CI build status + :alt: Travis CI build status (Linux) -.. image:: https://pypip.in/v/Pillow/badge.png +.. + image:: https://pypip.in/v/Pillow/badge.png :target: https://pypi.python.org/pypi/Pillow/ :alt: Latest PyPI version -.. image:: https://pypip.in/d/Pillow/badge.png +.. + image:: https://pypip.in/d/Pillow/badge.png :target: https://pypi.python.org/pypi/Pillow/ :alt: Number of PyPI downloads @@ -24,3 +28,21 @@ Pillow is the "friendly" PIL fork by `Alex Clark and Contributors `_ + + - `Pre-fork `_ + +- `Contribute `_ + + - `Issues `_ + +- `Documentation `_ + + - `About `_ + - `Guides `_ + - `Installation `_ + - `Reference `_ diff --git a/RELEASING.md b/RELEASING.md index 1bd8a28ae..d9c4d761e 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -2,29 +2,62 @@ ## Main Release -Released quarterly. +Released quarterly on the first day of January, April, July, October. -* [ ] Get master to the appropriate code release state. [Travis CI](https://travis-ci.org/python-pillow/Pillow) should be running cleanly for all merges to master. -* [ ] Update version in `PIL/__init__.py`, `setup.py`, `_imaging.c`, Update date in `CHANGES.rst`. -* [ ] Run pre-release check via `make pre` -* [ ] Tag and push to release branch in python-pillow repo. -* [ ] Upload binaries. +* [ ] Open a release ticket e.g. https://github.com/python-pillow/Pillow/issues/1174 +* [ ] Develop and prepare release in ``master`` branch. +* [ ] Check [Travis CI](https://travis-ci.org/python-pillow/Pillow) to confirm passing tests in ``master`` branch. +* [ ] In compliance with https://www.python.org/dev/peps/pep-0440/, update version identifier in: +``` + PIL/__init__.py setup.py _imaging.c +``` +* [ ] Update `CHANGES.rst`. +* [ ] Run pre-release check via `make pre`. +* [ ] Create branch and tag for release e.g.: +``` + $ git branch 2.9.x + $ git tag 2.9.0 + $ git push --all + $ git push --tags +``` +* [ ] Create and upload source distributions e.g.: +``` + $ make sdistup +``` +* [ ] Create and upload [binary distributions](#binary-distributions) +* [ ] Manually hide old versions on PyPI as needed, such that only the latest main release is visible when viewing https://pypi.python.org/pypi/Pillow ## Point Release -Released as required for security or installation fixes. +Released as needed for security, installation or critical bug fixes. -* [ ] Make necessary changes in master. -* [ ] Cherry pick individual commits. Touch up `CHANGES.rst` to reflect reality. -* [ ] Update version in `PIL/__init__.py`, `setup.py`, `_imaging.c` -* [ ] Run pre-release check via `make pre` -* [ ] Push to release branch in personal repo. Let Travis run cleanly. -* [ ] Tag and push to release branch in python-pillow repo. -* [ ] Upload binaries. +* [ ] Make necessary changes in ``master`` branch. +* [ ] Update `CHANGES.rst`. +* [ ] Cherry pick individual commits from ``master`` branch to release branch e.g. ``2.9.x``. +* [ ] Check [Travis CI](https://travis-ci.org/python-pillow/Pillow) to confirm passing tests in release branch e.g. ``2.9.x``. +* [ ] Checkout release branch e.g.: +``` + git checkout -t remotes/origin/2.9.x +``` +* [ ] In compliance with https://www.python.org/dev/peps/pep-0440/, update version identifier in: +``` + PIL/__init__.py setup.py _imaging.c +``` +* [ ] Run pre-release check via `make pre`. +* [ ] Create tag for release e.g.: +``` + $ git tag 2.9.1 + $ git push --tags +``` +* [ ] Create and upload source distributions e.g.: +``` + $ make sdistup +``` +* [ ] Create and upload [binary distributions](#binary-distributions) ## Embargoed Release -Security fixes that need to be pushed to the distros prior to public release. +Released as needed privately to individual vendors for critical security-related bug fixes. * [ ] Prepare patch for all versions that will get a fix. Test against local installations. * [ ] Commit against master, cherry pick to affected release branches. @@ -34,26 +67,39 @@ Security fixes that need to be pushed to the distros prior to public release. * [ ] Amend any commits with the CVE # * [ ] On release date, tag and push to GitHub. ``` -git checkout 2.5.x -git tag 2.5.3 -git push origin 2.5.x -git push origin --tags + git checkout 2.5.x + git tag 2.5.3 + git push origin 2.5.x + git push origin --tags ``` -* [ ] Upload binaries - - -## Binary Upload Process - -* [ ] Ping cgohlke for Windows binaries -* [ ] From a clean source directory with no extra temp files: +* [ ] Create and upload source distributions e.g.: ``` -python setup.py register -python setup.py sdist --format=zip upload -python setup.py sdist upload + $ make sdistup ``` -(Debian requests a tarball, everyone else would just prefer that we choose one and stick to it. So both it is) -* [ ] Push a commit to https://github.com/python-pillow/pillow-wheels to build OSX versions (UNDONE latest tag or specific release???) -* [ ] Retrieve the OS X Wheels from Rackspace files, upload to PyPi (Twine?) -* [ ] Grab Windows binaries, `twine upload dist/*.[whl|egg]`. Manually upload .exe installers. -* [ ] Announce release availability. [Twitter](https://twitter.com/pythonpillow), web. +* [ ] Create and upload [binary distributions](#binary-distributions) +## Binary Distributions + +### Windows +* [ ] Contact @cgohlke for Windows binaries via release ticket e.g. https://github.com/python-pillow/Pillow/issues/1174. +* [ ] Download and extract tarball from @cgohlke and ``twine upload *``. + +### OS X +* [ ] Use the [Pillow OS X Wheel Builder](https://github.com/python-pillow/pillow-wheels): +``` + $ git checkout https://github.com/python-pillow/pillow-wheels + $ cd pillow-wheels + $ git submodule init + $ git submodule update + $ cd Pillow + $ git fetch --all + $ git commit -a -m "Pillow -> 2.9.0" + $ git push +``` +* [ ] Download distributions from the [Pillow OS X Wheel Builder container](http://cdf58691c5cf45771290-6a3b6a0f5f6ab91aadc447b2a897dd9a.r50.cf2.rackcdn.com/) and ``twine upload *``. + +### Linux + +## Publicize Release + +* [ ] Announce release availability via [Twitter](https://twitter.com/pythonpillow) e.g. https://twitter.com/aclark4life/status/583366798302691328. diff --git a/Scripts/README.rst b/Scripts/README.rst index 1b26ea68c..f86a9a8b6 100644 --- a/Scripts/README.rst +++ b/Scripts/README.rst @@ -63,20 +63,3 @@ explode.py -------------------------------------------------------------------- Split a sequence file into individual frames. - -image2py.py --------------------------------------------------------------------- - -Convert an image to a Python module containing an IMAGE variable. -Note that the module using the module must include JPEG and ZIP -decoders, unless the -u option is used. - -olesummary.py --------------------------------------------------------------------- - -Uses the OleFileIO module to dump the summary information from an OLE -structured storage file. This works with most OLE files, including -Word documents, FlashPix images, etc. - -Note that datetime fields currently show the number of seconds since -January 1st, 1601. diff --git a/Scripts/createfontdatachunk.py b/Scripts/createfontdatachunk.py index 0c860701a..b9514eb6e 100644 --- a/Scripts/createfontdatachunk.py +++ b/Scripts/createfontdatachunk.py @@ -1,3 +1,4 @@ +from __future__ import print_function import base64 import os import sys diff --git a/Scripts/enhancer.py b/Scripts/enhancer.py index fe250c9f8..6393c983f 100644 --- a/Scripts/enhancer.py +++ b/Scripts/enhancer.py @@ -8,9 +8,9 @@ # try: - from tkinter import * + from tkinter import Tk, Toplevel, Frame, Label, Scale, HORIZONTAL except ImportError: - from Tkinter import * + from Tkinter import Tk, Toplevel, Frame, Label, Scale, HORIZONTAL from PIL import Image, ImageTk, ImageEnhance import sys @@ -18,6 +18,7 @@ import sys # # enhancer widget + class Enhance(Frame): def __init__(self, master, image, name, enhancer, lo, hi): Frame.__init__(self, master) @@ -25,7 +26,7 @@ class Enhance(Frame): # set up the image self.tkim = ImageTk.PhotoImage(image.mode, image.size) self.enhancer = enhancer(image) - self.update("1.0") # normalize + self.update("1.0") # normalize # image window Label(self, image=self.tkim).pack() diff --git a/Scripts/explode.py b/Scripts/explode.py index b8680f631..53436100b 100644 --- a/Scripts/explode.py +++ b/Scripts/explode.py @@ -9,11 +9,13 @@ from __future__ import print_function from PIL import Image -import os, sys +import os +import sys -class Interval: - def __init__(self, interval = "0"): +class Interval(object): + + def __init__(self, interval="0"): self.setinterval(interval) diff --git a/Scripts/gifmaker.py b/Scripts/gifmaker.py index 9fa5e71a4..28c6fb25d 100644 --- a/Scripts/gifmaker.py +++ b/Scripts/gifmaker.py @@ -49,20 +49,23 @@ from PIL.GifImagePlugin import getheader, getdata # -------------------------------------------------------------------- # sequence iterator -class image_sequence: + +class image_sequence(object): def __init__(self, im): self.im = im + def __getitem__(self, ix): try: if ix: self.im.seek(ix) return self.im except EOFError: - raise IndexError # end of sequence + raise IndexError # end of sequence # -------------------------------------------------------------------- # straightforward delta encoding + def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" @@ -72,13 +75,13 @@ def makedelta(fp, sequence): for im in sequence: - # - # FIXME: write graphics control block before each frame + # To specify duration, add the time in milliseconds to getdata(), + # e.g. getdata(im, duration=1000) if not previous: # global header - for s in getheader(im) + getdata(im): + for s in getheader(im)[0] + getdata(im): fp.write(s) else: @@ -91,7 +94,7 @@ def makedelta(fp, sequence): if bbox: # compress difference - for s in getdata(im.crop(bbox), offset = bbox[:2]): + for s in getdata(im.crop(bbox), offset=bbox[:2]): fp.write(s) else: @@ -109,6 +112,7 @@ def makedelta(fp, sequence): # -------------------------------------------------------------------- # main hack + def compress(infile, outfile): # open input image, and force loading of first frame diff --git a/Scripts/painter.py b/Scripts/painter.py index 80c4db6a0..234f06171 100644 --- a/Scripts/painter.py +++ b/Scripts/painter.py @@ -10,9 +10,9 @@ # try: - from tkinter import * + from tkinter import Tk, Canvas, NW except ImportError: - from Tkinter import * + from Tkinter import Tk, Canvas, NW from PIL import Image, ImageTk import sys @@ -20,6 +20,7 @@ import sys # # painter widget + class PaintCanvas(Canvas): def __init__(self, master, image): Canvas.__init__(self, master, width=image.size[0], height=image.size[1]) @@ -33,7 +34,7 @@ class PaintCanvas(Canvas): box = x, y, min(xsize, x+tilesize), min(ysize, y+tilesize) tile = ImageTk.PhotoImage(image.crop(box)) self.create_image(x, y, image=tile, anchor=NW) - self.tile[(x,y)] = box, tile + self.tile[(x, y)] = box, tile self.image = image @@ -59,7 +60,7 @@ class PaintCanvas(Canvas): xy, tile = self.tile[(x, y)] tile.paste(self.image.crop(xy)) except KeyError: - pass # outside the image + pass # outside the image self.update_idletasks() # diff --git a/Scripts/pilconvert.py b/Scripts/pilconvert.py index 934167351..b9ebd52ae 100644 --- a/Scripts/pilconvert.py +++ b/Scripts/pilconvert.py @@ -15,11 +15,13 @@ from __future__ import print_function -import site -import getopt, string, sys +import getopt +import string +import sys from PIL import Image + def usage(): print("PIL Convert 0.5/1998-12-30 -- convert image files") print("Usage: pilconvert [option] infile outfile") @@ -47,10 +49,10 @@ except getopt.error as v: print(v) sys.exit(1) -format = None +output_format = None convert = None -options = { } +options = {} for o, a in opt: @@ -66,7 +68,7 @@ for o, a in opt: sys.exit(1) elif o == "-c": - format = a + output_format = a if o == "-g": convert = "L" @@ -88,8 +90,8 @@ try: if convert and im.mode != convert: im.draft(convert, im.size) im = im.convert(convert) - if format: - im.save(argv[1], format, **options) + if output_format: + im.save(argv[1], output_format, **options) else: im.save(argv[1], **options) except: diff --git a/Scripts/pildriver.py b/Scripts/pildriver.py index e45b05008..32989ccdf 100644 --- a/Scripts/pildriver.py +++ b/Scripts/pildriver.py @@ -52,7 +52,8 @@ from __future__ import print_function from PIL import Image -class PILDriver: + +class PILDriver(object): verbose = 0 @@ -497,10 +498,6 @@ class PILDriver: if __name__ == '__main__': import sys - try: - import readline - except ImportError: - pass # not available on all platforms # If we see command-line arguments, interpret them as a stack state # and execute. Otherwise go interactive. diff --git a/Scripts/pilfile.py b/Scripts/pilfile.py index 1b77b0e78..b954114ac 100644 --- a/Scripts/pilfile.py +++ b/Scripts/pilfile.py @@ -19,8 +19,9 @@ from __future__ import print_function -import site -import getopt, glob, sys +import getopt +import glob +import sys from PIL import Image @@ -59,6 +60,7 @@ for o, a in opt: elif o == "-D": Image.DEBUG += 1 + def globfix(files): # expand wildcards where necessary if sys.platform == "win32": diff --git a/Scripts/pilfont.py b/Scripts/pilfont.py index ec25e7a71..4425c072c 100644 --- a/Scripts/pilfont.py +++ b/Scripts/pilfont.py @@ -14,7 +14,8 @@ from __future__ import print_function VERSION = "0.4" -import glob, sys +import glob +import sys # drivers from PIL import BdfFontFile diff --git a/Scripts/pilprint.py b/Scripts/pilprint.py index be42e0a75..2a5e23061 100644 --- a/Scripts/pilprint.py +++ b/Scripts/pilprint.py @@ -12,24 +12,25 @@ # from __future__ import print_function +import getopt +import os +import sys VERSION = "pilprint 0.3/2003-05-05" from PIL import Image from PIL import PSDraw -letter = ( 1.0*72, 1.0*72, 7.5*72, 10.0*72 ) +letter = (1.0*72, 1.0*72, 7.5*72, 10.0*72) -def description(file, image): - import os - title = os.path.splitext(os.path.split(file)[1])[0] + +def description(filepath, image): + title = os.path.splitext(os.path.split(filepath)[1])[0] format = " (%dx%d " if image.format: format = " (" + image.format + " %dx%d " return title + format % image.size + image.mode + ")" -import getopt, os, sys - if len(sys.argv) == 1: print("PIL Print 0.2a1/96-10-04 -- print image files") print("Usage: pilprint files...") @@ -45,8 +46,8 @@ except getopt.error as v: print(v) sys.exit(1) -printer = None # print to stdout -monochrome = 1 # reduce file size for most common case +printer = None # print to stdout +monochrome = 1 # reduce file size for most common case for o, a in opt: if o == "-d": @@ -64,12 +65,12 @@ for o, a in opt: # printer channel printer = "lpr -P%s" % a -for file in argv: +for filepath in argv: try: - im = Image.open(file) + im = Image.open(filepath) - title = description(file, im) + title = description(filepath, im) if monochrome and im.mode not in ["1", "L"]: im.draft("L", im.size) diff --git a/Scripts/player.py b/Scripts/player.py index 0c90286c5..43877415a 100644 --- a/Scripts/player.py +++ b/Scripts/player.py @@ -56,7 +56,7 @@ class UI(Label): del self.im[0] self.image.paste(im) except IndexError: - return # end of list + return # end of list else: @@ -65,7 +65,7 @@ class UI(Label): im.seek(im.tell() + 1) self.image.paste(im) except EOFError: - return # end of file + return # end of file try: duration = im.info["duration"] diff --git a/Scripts/thresholder.py b/Scripts/thresholder.py index 29d4592d9..bfeebfa9f 100644 --- a/Scripts/thresholder.py +++ b/Scripts/thresholder.py @@ -18,8 +18,9 @@ import sys # # an image viewer + class UI(Frame): - def __init__(self, master, im, value = 128): + def __init__(self, master, im, value=128): Frame.__init__(self, master) self.image = im @@ -31,7 +32,7 @@ class UI(Frame): self.canvas.pack() scale = Scale(self, orient=HORIZONTAL, from_=0, to=255, - resolution=1, command=self.update, length=256) + resolution=1, command=self.update_scale, length=256) scale.set(value) scale.bind("", self.redraw) scale.pack() @@ -40,21 +41,21 @@ class UI(Frame): # be too slow on some platforms) # self.redraw() - def update(self, value): + def update_scale(self, value): self.value = eval(value) self.redraw() - def redraw(self, event = None): + def redraw(self, event=None): # create overlay (note the explicit conversion to mode "1") - im = self.image.point(lambda v,t=self.value: v>=t, "1") + im = self.image.point(lambda v, t=self.value: v >= t, "1") self.overlay = ImageTk.BitmapImage(im, foreground="green") # update canvas self.canvas.delete("overlay") self.canvas.create_image(0, 0, image=self.overlay, anchor=NW, - tags="overlay") + tags="overlay") # -------------------------------------------------------------------- # main diff --git a/Scripts/viewer.py b/Scripts/viewer.py index 86b2526cd..f9bccec4f 100644 --- a/Scripts/viewer.py +++ b/Scripts/viewer.py @@ -7,15 +7,16 @@ from __future__ import print_function try: - from tkinter import * + from tkinter import Tk, Label except ImportError: - from Tkinter import * + from Tkinter import Tk, Label from PIL import Image, ImageTk # # an image viewer + class UI(Label): def __init__(self, master, im): diff --git a/Tests/README.rst b/Tests/README.rst index 6fd770f55..e3f160b09 100644 --- a/Tests/README.rst +++ b/Tests/README.rst @@ -3,7 +3,7 @@ Pillow Tests Test scripts are named ``test_xxx.py`` and use the ``unittest`` module. A base class and helper functions can be found in ``helper.py``. -Depedencies +Dependencies ----------- Install:: diff --git a/Tests/bench_cffi_access.py b/Tests/bench_cffi_access.py index 769396f53..5b64813e4 100644 --- a/Tests/bench_cffi_access.py +++ b/Tests/bench_cffi_access.py @@ -47,7 +47,7 @@ class BenchCffiAccess(PillowTestCase): self.assertEqual(caccess[(0, 0)], access[(0, 0)]) - print ("Size: %sx%s" % im.size) + print("Size: %sx%s" % im.size) timer(iterate_get, 'PyAccess - get', im.size, access) timer(iterate_set, 'PyAccess - set', im.size, access) timer(iterate_get, 'C-api - get', im.size, caccess) diff --git a/Tests/bench_get.py b/Tests/bench_get.py index 8f786acf0..9c51c0258 100644 --- a/Tests/bench_get.py +++ b/Tests/bench_get.py @@ -10,7 +10,7 @@ def bench(mode): get = im.im.getpixel xy = 50, 50 # position shouldn't really matter t0 = timeit.default_timer() - for i in range(1000000): + for _ in range(1000000): get(xy) print(mode, timeit.default_timer() - t0, "us") diff --git a/Tests/check_imaging_leaks.py b/Tests/check_imaging_leaks.py new file mode 100644 index 000000000..c6d99b8d1 --- /dev/null +++ b/Tests/check_imaging_leaks.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +from __future__ import division +from helper import unittest, PillowTestCase +import sys +from PIL import Image, ImageFilter + +min_iterations = 100 +max_iterations = 10000 + + +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +class TestImagingLeaks(PillowTestCase): + + def _get_mem_usage(self): + from resource import getpagesize, getrusage, RUSAGE_SELF + mem = getrusage(RUSAGE_SELF).ru_maxrss + return mem * getpagesize() / 1024 / 1024 + + def _test_leak(self, min_iterations, max_iterations, fn, *args, **kwargs): + mem_limit = None + for i in range(max_iterations): + fn(*args, **kwargs) + mem = self._get_mem_usage() + if i < min_iterations: + mem_limit = mem + 1 + continue + self.assertLessEqual(mem, mem_limit, + msg='memory usage limit exceeded after %d iterations' + % (i + 1)) + + def test_leak_putdata(self): + im = Image.new('RGB', (25, 25)) + self._test_leak(min_iterations, max_iterations, im.putdata, im.getdata()) + + def test_leak_getlist(self): + im = Image.new('P', (25, 25)) + self._test_leak(min_iterations, max_iterations, + # Pass a new list at each iteration. + lambda: im.point(range(256))) + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/check_j2k_leaks.py b/Tests/check_j2k_leaks.py index 9dbb8c1f4..8e9c4ca20 100755 --- a/Tests/check_j2k_leaks.py +++ b/Tests/check_j2k_leaks.py @@ -21,7 +21,7 @@ class TestJpegLeaks(PillowTestCase): from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK setrlimit(RLIMIT_STACK, (stack_size, stack_size)) setrlimit(RLIMIT_AS, (mem_limit, mem_limit)) - for count in range(iterations): + for _ in range(iterations): with Image.open(test_file) as im: im.load() @@ -29,13 +29,13 @@ class TestJpegLeaks(PillowTestCase): from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK setrlimit(RLIMIT_STACK, (stack_size, stack_size)) setrlimit(RLIMIT_AS, (mem_limit, mem_limit)) - for count in range(iterations): + for _ in range(iterations): with Image.open(test_file) as im: im.load() test_output = BytesIO() im.save(test_output, "JPEG2000") test_output.seek(0) - output = test_output.read() + test_output.read() if __name__ == '__main__': diff --git a/Tests/check_jpeg_leaks.py b/Tests/check_jpeg_leaks.py index adf7652cf..4d13978f8 100644 --- a/Tests/check_jpeg_leaks.py +++ b/Tests/check_jpeg_leaks.py @@ -1,5 +1,4 @@ from helper import unittest, PillowTestCase, hopper -from PIL import Image from io import BytesIO import sys @@ -71,9 +70,8 @@ post-patch: | :@:@@: :#:::@ :::@::::@ : :: : @ :::::: :@:: ::: :::: @:: @:::::::@:::: 0 +----------------------------------------------------------------------->Gi 0 8.421 - -""" +""" def test_qtables_leak(self): im = hopper('RGB') @@ -103,8 +101,7 @@ post-patch: qtables = [standard_l_qtable, standard_chrominance_qtable] - - for count in range(iterations): + for _ in range(iterations): test_output = BytesIO() im.save(test_output, "JPEG", qtables=qtables) @@ -135,7 +132,7 @@ pre patch: 0 +----------------------------------------------------------------------->Gi 0 11.37 - + post patch: MB @@ -168,12 +165,12 @@ post patch: im = hopper('RGB') exif = b'12345678'*4096 - for count in range(iterations): + for _ in range(iterations): test_output = BytesIO() im.save(test_output, "JPEG", exif=exif) - - """ + def test_base_save(self): + """ base case: MB 20.99^ ::::: :::::::::::::::::::::::::::::::::::::::::::@::: @@ -199,11 +196,9 @@ base case: 0 +----------------------------------------------------------------------->Gi 0 7.882 """ - - def test_base_save(self): im = hopper('RGB') - for count in range(iterations): + for _ in range(iterations): test_output = BytesIO() im.save(test_output, "JPEG") diff --git a/Tests/check_png_dos.py b/Tests/check_png_dos.py new file mode 100644 index 000000000..762c9607a --- /dev/null +++ b/Tests/check_png_dos.py @@ -0,0 +1,48 @@ +from helper import unittest, PillowTestCase +from PIL import Image, PngImagePlugin +from io import BytesIO +import zlib + +TEST_FILE = "Tests/images/png_decompression_dos.png" + + +class TestPngDos(PillowTestCase): + def test_dos_text(self): + + try: + im = Image.open(TEST_FILE) + im.load() + except ValueError as msg: + self.assertTrue(msg, "Decompressed Data Too Large") + return + + for s in im.text.values(): + self.assertLess(len(s), 1024*1024, "Text chunk larger than 1M") + + def test_dos_total_memory(self): + im = Image.new('L', (1, 1)) + compressed_data = zlib.compress('a'*1024*1023) + + info = PngImagePlugin.PngInfo() + + for x in range(64): + info.add_text('t%s' % x, compressed_data, 1) + info.add_itxt('i%s' % x, compressed_data, zip=True) + + b = BytesIO() + im.save(b, 'PNG', pnginfo=info) + b.seek(0) + + try: + im2 = Image.open(b) + except ValueError as msg: + self.assertIn("Too much memory", msg) + return + + total_len = 0 + for txt in im2.text.values(): + total_len += len(txt) + self.assertLess(total_len, 64*1024*1024, "Total text chunks greater than 64M") + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/check_webp_leaks.py b/Tests/check_webp_leaks.py new file mode 100644 index 000000000..0f54f382d --- /dev/null +++ b/Tests/check_webp_leaks.py @@ -0,0 +1,38 @@ +from __future__ import division +from helper import unittest, PillowTestCase +import sys +from PIL import Image +from io import BytesIO + +# Limits for testing the leak +mem_limit = 16 # max increase in MB +iterations = 5000 +test_file = "Tests/images/hopper.webp" + + +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +class TestWebPLeaks(PillowTestCase): + + def setUp(self): + try: + from PIL import _webp + except ImportError: + self.skipTest('WebP support not installed') + + def _get_mem_usage(self): + from resource import getpagesize, getrusage, RUSAGE_SELF + mem = getrusage(RUSAGE_SELF).ru_maxrss + return mem * getpagesize() / 1024 / 1024 + + def test_leak_load(self): + with open(test_file, 'rb') as f: + im_data = f.read() + start_mem = self._get_mem_usage() + for _ in range(iterations): + with Image.open(BytesIO(im_data)) as im: + im.load() + mem = (self._get_mem_usage() - start_mem) + self.assertLess(mem, mem_limit, msg='memory usage limit exceeded') + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/fonts/DejaVuSans-bitmap.ttf b/Tests/fonts/DejaVuSans-bitmap.ttf new file mode 100644 index 000000000..702cce37d Binary files /dev/null and b/Tests/fonts/DejaVuSans-bitmap.ttf differ diff --git a/Tests/fonts/DejaVuSans.ttf b/Tests/fonts/DejaVuSans.ttf new file mode 100644 index 000000000..683bbbd41 Binary files /dev/null and b/Tests/fonts/DejaVuSans.ttf differ diff --git a/Tests/helper.py b/Tests/helper.py index 9ee5a8259..1255b1819 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -130,7 +130,7 @@ class PillowTestCase(unittest.TestCase): # Skip if platform/travis matches, and # PILLOW_RUN_KNOWN_BAD is not true in the environment. if bool(os.environ.get('PILLOW_RUN_KNOWN_BAD', False)): - print (os.environ.get('PILLOW_RUN_KNOWN_BAD', False)) + print(os.environ.get('PILLOW_RUN_KNOWN_BAD', False)) return skip = True @@ -165,7 +165,6 @@ class PillowTestCase(unittest.TestCase): # helpers -import sys py3 = (sys.version_info >= (3, 0)) @@ -175,31 +174,33 @@ def fromstring(data): return Image.open(BytesIO(data)) -def tostring(im, format, **options): +def tostring(im, string_format, **options): from io import BytesIO out = BytesIO() - im.save(out, format, **options) + im.save(out, string_format, **options) return out.getvalue() -def hopper(mode="RGB", cache={}): +def hopper(mode=None, cache={}): from PIL import Image - im = None - # FIXME: Implement caching to reduce reading from disk but so an original - # copy is returned each time and the cached image isn't modified by tests + if mode is None: + # Always return fresh not-yet-loaded version of image. + # Operations on not-yet-loaded images is separate class of errors + # what we should catch. + return Image.open("Tests/images/hopper.ppm") + # Use caching to reduce reading from disk but so an original copy is + # returned each time and the cached image isn't modified by tests # (for fast, isolated, repeatable tests). - # im = cache.get(mode) + im = cache.get(mode) if im is None: - if mode == "RGB": - im = Image.open("Tests/images/hopper.ppm") - elif mode == "F": + if mode == "F": im = hopper("L").convert(mode) elif mode[:4] == "I;16": im = hopper("I").convert(mode) else: - im = hopper("RGB").convert(mode) - # cache[mode] = im - return im + im = hopper().convert(mode) + cache[mode] = im + return im.copy() def command_succeeds(cmd): @@ -207,7 +208,6 @@ def command_succeeds(cmd): Runs the command, which must be a list of strings. Returns True if the command succeeds, or False if an OSError was raised by subprocess.Popen. """ - import os import subprocess with open(os.devnull, 'w') as f: try: diff --git a/Tests/images/color_snakes.png b/Tests/images/color_snakes.png new file mode 100644 index 000000000..bf3a35196 Binary files /dev/null and b/Tests/images/color_snakes.png differ diff --git a/Tests/images/hopper.im b/Tests/images/hopper.im new file mode 100644 index 000000000..ceeb01049 Binary files /dev/null and b/Tests/images/hopper.im differ diff --git a/Tests/images/hopper.msp b/Tests/images/hopper.msp new file mode 100644 index 000000000..91d9a147f Binary files /dev/null and b/Tests/images/hopper.msp differ diff --git a/Tests/images/hopper_bad_exif.jpg b/Tests/images/hopper_bad_exif.jpg new file mode 100644 index 000000000..4cfeea1dc Binary files /dev/null and b/Tests/images/hopper_bad_exif.jpg differ diff --git a/Tests/images/hopper_gray_4bpp.tif b/Tests/images/hopper_gray_4bpp.tif new file mode 100644 index 000000000..7d23958a7 Binary files /dev/null and b/Tests/images/hopper_gray_4bpp.tif differ diff --git a/Tests/images/hopper_merged.psd b/Tests/images/hopper_merged.psd new file mode 100644 index 000000000..6b5c7ebe1 Binary files /dev/null and b/Tests/images/hopper_merged.psd differ diff --git a/Tests/images/illu10_no_preview.eps b/Tests/images/illu10_no_preview.eps new file mode 100644 index 000000000..ffe8a537a Binary files /dev/null and b/Tests/images/illu10_no_preview.eps differ diff --git a/Tests/images/illu10_preview.eps b/Tests/images/illu10_preview.eps new file mode 100644 index 000000000..74d2fec93 Binary files /dev/null and b/Tests/images/illu10_preview.eps differ diff --git a/Tests/images/illuCS6_no_preview.eps b/Tests/images/illuCS6_no_preview.eps new file mode 100644 index 000000000..4be1ea9a8 Binary files /dev/null and b/Tests/images/illuCS6_no_preview.eps differ diff --git a/Tests/images/illuCS6_preview.eps b/Tests/images/illuCS6_preview.eps new file mode 100644 index 000000000..5e676a754 Binary files /dev/null and b/Tests/images/illuCS6_preview.eps differ diff --git a/Tests/images/png_decompression_dos.png b/Tests/images/png_decompression_dos.png new file mode 100644 index 000000000..986561b2e Binary files /dev/null and b/Tests/images/png_decompression_dos.png differ diff --git a/Tests/images/tRNS_null_1x1.png b/Tests/images/tRNS_null_1x1.png new file mode 100644 index 000000000..976eae939 Binary files /dev/null and b/Tests/images/tRNS_null_1x1.png differ diff --git a/Tests/images/total-pages-zero.tif b/Tests/images/total-pages-zero.tif new file mode 100644 index 000000000..50df07af3 Binary files /dev/null and b/Tests/images/total-pages-zero.tif differ diff --git a/Tests/import_all.py b/Tests/import_all.py index d8299d969..88a102b64 100644 --- a/Tests/import_all.py +++ b/Tests/import_all.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys sys.path.insert(0, ".") diff --git a/Tests/large_memory_numpy_test.py b/Tests/large_memory_numpy_test.py index 159d767d6..5e5a58441 100644 --- a/Tests/large_memory_numpy_test.py +++ b/Tests/large_memory_numpy_test.py @@ -14,7 +14,7 @@ from PIL import Image try: import numpy as np except: - sys.exit("Skipping: Numpy not installed") + raise unittest.SkipTest("numpy not installed") YDIM = 32769 XDIM = 48000 diff --git a/Tests/make_hash.py b/Tests/make_hash.py index 88bb2994b..a92886df9 100644 --- a/Tests/make_hash.py +++ b/Tests/make_hash.py @@ -1,5 +1,7 @@ # brute-force search for access descriptor hash table +from __future__ import print_function + modes = [ "1", "L", "LA", diff --git a/Tests/test_binary.py b/Tests/test_binary.py new file mode 100644 index 000000000..1e44d9641 --- /dev/null +++ b/Tests/test_binary.py @@ -0,0 +1,29 @@ +from helper import unittest, PillowTestCase + +from PIL import _binary + + +class TestBinary(PillowTestCase): + + def test_standard(self): + self.assertEqual(_binary.i8(b'*'), 42) + self.assertEqual(_binary.o8(42), b'*') + + def test_little_endian(self): + self.assertEqual(_binary.i16le(b'\xff\xff\x00\x00'), 65535) + self.assertEqual(_binary.i32le(b'\xff\xff\x00\x00'), 65535) + + self.assertEqual(_binary.o16le(65535), b'\xff\xff') + self.assertEqual(_binary.o32le(65535), b'\xff\xff\x00\x00') + + def test_big_endian(self): + self.assertEqual(_binary.i16be(b'\x00\x00\xff\xff'), 0) + self.assertEqual(_binary.i32be(b'\x00\x00\xff\xff'), 65535) + + self.assertEqual(_binary.o16be(65535), b'\xff\xff') + self.assertEqual(_binary.o32be(65535), b'\x00\x00\xff\xff') + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py index b45ea76f6..79ad439ba 100644 --- a/Tests/test_bmp_reference.py +++ b/Tests/test_bmp_reference.py @@ -58,10 +58,10 @@ class TestBmpReference(PillowTestCase): } def get_compare(f): - (head, name) = os.path.split(f) + name = os.path.split(f)[1] if name in file_map: return os.path.join(base, 'html', file_map[name]) - (name, ext) = os.path.splitext(name) + name = os.path.splitext(name)[0] return os.path.join(base, 'html', "%s.png" % name) for f in self.get_files('g'): diff --git a/Tests/test_box_blur.py b/Tests/test_box_blur.py new file mode 100644 index 000000000..8c52054a2 --- /dev/null +++ b/Tests/test_box_blur.py @@ -0,0 +1,235 @@ +from helper import unittest, PillowTestCase + +from PIL import Image, ImageOps + + +sample = Image.new("L", (7, 5)) +sample.putdata(sum([ + [210, 50, 20, 10, 220, 230, 80], + [190, 210, 20, 180, 170, 40, 110], + [120, 210, 250, 60, 220, 0, 220], + [220, 40, 230, 80, 130, 250, 40], + [250, 0, 80, 30, 60, 20, 110], +], [])) + + +class ImageMock(object): + def __init__(self): + self.im = self + + def load(self): + pass + + def _new(self, im): + return im + + def box_blur(self, radius, n): + return radius, n + + +class TestBoxBlurApi(PillowTestCase): + + def test_imageops_box_blur(self): + i = ImageOps.box_blur(sample, 1) + self.assertEqual(i.mode, sample.mode) + self.assertEqual(i.size, sample.size) + self.assertIsInstance(i, Image.Image) + + +class TestBoxBlur(PillowTestCase): + + def box_blur(self, image, radius=1, n=1): + return image._new(image.im.box_blur(radius, n)) + + def assertImage(self, im, data, delta=0): + it = iter(im.getdata()) + for data_row in data: + im_row = [next(it) for _ in range(im.size[0])] + if any( + abs(data_v - im_v) > delta + for data_v, im_v in zip(data_row, im_row) + ): + self.assertEqual(im_row, data_row) + self.assertRaises(StopIteration, next, it) + + def assertBlur(self, im, radius, data, passes=1, delta=0): + # check grayscale image + self.assertImage(self.box_blur(im, radius, passes), data, delta) + rgba = Image.merge('RGBA', (im, im, im, im)) + for band in self.box_blur(rgba, radius, passes).split(): + self.assertImage(band, data, delta) + + def test_color_modes(self): + self.assertRaises(ValueError, self.box_blur, sample.convert("1")) + self.assertRaises(ValueError, self.box_blur, sample.convert("P")) + self.box_blur(sample.convert("L")) + self.box_blur(sample.convert("LA")) + self.assertRaises(ValueError, self.box_blur, sample.convert("I")) + self.assertRaises(ValueError, self.box_blur, sample.convert("F")) + self.box_blur(sample.convert("RGB")) + self.box_blur(sample.convert("RGBA")) + self.box_blur(sample.convert("CMYK")) + self.assertRaises(ValueError, self.box_blur, sample.convert("YCbCr")) + + def test_radius_0(self): + self.assertBlur( + sample, 0, + [ + [210, 50, 20, 10, 220, 230, 80], + [190, 210, 20, 180, 170, 40, 110], + [120, 210, 250, 60, 220, 0, 220], + [220, 40, 230, 80, 130, 250, 40], + [250, 0, 80, 30, 60, 20, 110], + ] + ) + + def test_radius_0_02(self): + self.assertBlur( + sample, 0.02, + [ + [206, 55, 20, 17, 215, 223, 83], + [189, 203, 31, 171, 169, 46, 110], + [125, 206, 241, 69, 210, 13, 210], + [215, 49, 221, 82, 131, 235, 48], + [244, 7, 80, 32, 60, 27, 107], + ], + delta=2, + ) + + def test_radius_0_05(self): + self.assertBlur( + sample, 0.05, + [ + [202, 62, 22, 27, 209, 215, 88], + [188, 194, 44, 161, 168, 56, 111], + [131, 201, 229, 81, 198, 31, 198], + [209, 62, 209, 86, 133, 216, 59], + [237, 17, 80, 36, 60, 35, 103], + ], + delta=2, + ) + + def test_radius_0_1(self): + self.assertBlur( + sample, 0.1, + [ + [196, 72, 24, 40, 200, 203, 93], + [187, 183, 62, 148, 166, 68, 111], + [139, 193, 213, 96, 182, 54, 182], + [201, 78, 193, 91, 133, 191, 73], + [227, 31, 80, 42, 61, 47, 99], + ], + delta=1, + ) + + def test_radius_0_5(self): + self.assertBlur( + sample, 0.5, + [ + [176, 101, 46, 83, 163, 165, 111], + [176, 149, 108, 122, 144, 120, 117], + [164, 171, 159, 141, 134, 119, 129], + [170, 136, 133, 114, 116, 124, 109], + [184, 95, 72, 70, 69, 81, 89], + ], + delta=1, + ) + + def test_radius_1(self): + self.assertBlur( + sample, 1, + [ + [170, 109, 63, 97, 146, 153, 116], + [168, 142, 112, 128, 126, 143, 121], + [169, 166, 142, 149, 126, 131, 114], + [159, 156, 109, 127, 94, 117, 112], + [164, 128, 63, 87, 76, 89, 90], + ], + delta=1, + ) + + def test_radius_1_5(self): + self.assertBlur( + sample, 1.5, + [ + [155, 120, 105, 112, 124, 137, 130], + [160, 136, 124, 125, 127, 134, 130], + [166, 147, 130, 125, 120, 121, 119], + [168, 145, 119, 109, 103, 105, 110], + [168, 134, 96, 85, 85, 89, 97], + ], + delta=1, + ) + + def test_radius_bigger_then_half(self): + self.assertBlur( + sample, 3, + [ + [144, 145, 142, 128, 114, 115, 117], + [148, 145, 137, 122, 109, 111, 112], + [152, 145, 131, 117, 103, 107, 108], + [156, 144, 126, 111, 97, 102, 103], + [160, 144, 121, 106, 92, 98, 99], + ], + delta=1, + ) + + def test_radius_bigger_then_width(self): + self.assertBlur( + sample, 10, + [ + [158, 153, 147, 141, 135, 129, 123], + [159, 153, 147, 141, 136, 130, 124], + [159, 154, 148, 142, 136, 130, 124], + [160, 154, 148, 142, 137, 131, 125], + [160, 155, 149, 143, 137, 131, 125], + ], + delta=0, + ) + + def test_exteme_large_radius(self): + self.assertBlur( + sample, 600, + [ + [162, 162, 162, 162, 162, 162, 162], + [162, 162, 162, 162, 162, 162, 162], + [162, 162, 162, 162, 162, 162, 162], + [162, 162, 162, 162, 162, 162, 162], + [162, 162, 162, 162, 162, 162, 162], + ], + delta=1, + ) + + def test_two_passes(self): + self.assertBlur( + sample, 1, + [ + [153, 123, 102, 109, 132, 135, 129], + [159, 138, 123, 121, 133, 131, 126], + [162, 147, 136, 124, 127, 121, 121], + [159, 140, 125, 108, 111, 106, 108], + [154, 126, 105, 87, 94, 93, 97], + ], + passes=2, + delta=1, + ) + + def test_three_passes(self): + self.assertBlur( + sample, 1, + [ + [146, 131, 116, 118, 126, 131, 130], + [151, 138, 125, 123, 126, 128, 127], + [154, 143, 129, 123, 120, 120, 119], + [152, 139, 122, 113, 108, 108, 108], + [148, 132, 112, 102, 97, 99, 100], + ], + passes=3, + delta=1, + ) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/Tests/test_cffi.py b/Tests/test_cffi.py index 5599dbb48..5d5427685 100644 --- a/Tests/test_cffi.py +++ b/Tests/test_cffi.py @@ -49,10 +49,10 @@ class TestCffi(PillowTestCase): self.skipTest("No cffi") def _test_get_access(self, im): - """ Do we get the same thing as the old pixel access """ + """Do we get the same thing as the old pixel access - """ Using private interfaces, forcing a capi access and - a pyaccess for the same image """ + Using private interfaces, forcing a capi access and + a pyaccess for the same image""" caccess = im.im.pixel_access(False) access = PyAccess.new(im, False) @@ -90,10 +90,10 @@ class TestCffi(PillowTestCase): # self._test_get_access(im) def _test_set_access(self, im, color): - """ Are we writing the correct bits into the image? """ + """Are we writing the correct bits into the image? - """ Using private interfaces, forcing a capi access and - a pyaccess for the same image """ + Using private interfaces, forcing a capi access and + a pyaccess for the same image""" caccess = im.im.pixel_access(False) access = PyAccess.new(im, False) diff --git a/Tests/test_features.py b/Tests/test_features.py new file mode 100644 index 000000000..8da22088e --- /dev/null +++ b/Tests/test_features.py @@ -0,0 +1,35 @@ +from helper import unittest, PillowTestCase + +from PIL import features + + +class TestFeatures(PillowTestCase): + + def test_check_features(self): + for feature in features.modules: + self.assertTrue( + features.check_module(feature) in [True, False, None]) + for feature in features.codecs: + self.assertTrue(features.check_codec(feature) in [True, False]) + + def test_supported_features(self): + self.assertTrue(type(features.get_supported_modules()) is list) + self.assertTrue(type(features.get_supported_codecs()) is list) + + def test_unsupported_codec(self): + # Arrange + codec = "unsupported_codec" + # Act / Assert + self.assertRaises(ValueError, lambda: features.check_codec(codec)) + + def test_unsupported_module(self): + # Arrange + module = "unsupported_module" + # Act / Assert + self.assertRaises(ValueError, lambda: features.check_module(module)) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/Tests/test_file_cur.py b/Tests/test_file_cur.py index 54bfe84fe..07bf3a750 100644 --- a/Tests/test_file_cur.py +++ b/Tests/test_file_cur.py @@ -16,9 +16,9 @@ class TestFileCur(PillowTestCase): self.assertEqual(im.size, (32, 32)) self.assertIsInstance(im, CurImagePlugin.CurImageFile) # Check some pixel colors to ensure image is loaded properly - self.assertEqual(im.getpixel((10, 1)), (0, 0, 0)) - self.assertEqual(im.getpixel((11, 1)), (253, 254, 254)) - self.assertEqual(im.getpixel((16, 16)), (84, 87, 86)) + self.assertEqual(im.getpixel((10, 1)), (0, 0, 0, 0)) + self.assertEqual(im.getpixel((11, 1)), (253, 254, 254, 1)) + self.assertEqual(im.getpixel((16, 16)), (84, 87, 86, 255)) if __name__ == '__main__': diff --git a/Tests/test_file_dcx.py b/Tests/test_file_dcx.py index 2acf7b459..7d2ae32d8 100644 --- a/Tests/test_file_dcx.py +++ b/Tests/test_file_dcx.py @@ -30,6 +30,10 @@ class TestFileDcx(PillowTestCase): # Assert self.assertEqual(frame, 0) + def test_n_frames(self): + im = Image.open(TEST_FILE) + self.assertEqual(im.n_frames, 1) + def test_seek_too_far(self): # Arrange im = Image.open(TEST_FILE) diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index a17ce274f..f1fbac922 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -73,7 +73,7 @@ class TestFileEps(PillowTestCase): image1_scale1_compare = Image.open(file1_compare).convert("RGB") image1_scale1_compare.load() self.assert_image_similar(img, image1_scale1_compare, 5) - + def test_render_scale1(self): # We need png support for these render test codecs = dir(Image.core) @@ -148,8 +148,8 @@ class TestFileEps(PillowTestCase): # open image with binary preview Image.open(file3) - def _test_readline(self,t, ending): - ending = "Failure with line ending: %s" %("".join("%s" %ord(s) for s in ending)) + def _test_readline(self, t, ending): + ending = "Failure with line ending: %s" % ("".join("%s" % ord(s) for s in ending)) self.assertEqual(t.readline().strip('\r\n'), 'something', ending) self.assertEqual(t.readline().strip('\r\n'), 'else', ending) self.assertEqual(t.readline().strip('\r\n'), 'baz', ending) @@ -164,66 +164,80 @@ class TestFileEps(PillowTestCase): return t = StringIO.StringIO(test_string) self._test_readline(t, ending) - + def _test_readline_io(self, test_string, ending): - import io if str is bytes: - t = io.StringIO(unicode(test_string)) + t = io.StringIO(unicode(test_string)) else: t = io.StringIO(test_string) self._test_readline(t, ending) def _test_readline_file_universal(self, test_string, ending): f = self.tempfile('temp.txt') - with open(f,'wb') as w: + with open(f, 'wb') as w: if str is bytes: w.write(test_string) else: w.write(test_string.encode('UTF-8')) - with open(f,'rU') as t: + with open(f, 'rU') as t: self._test_readline(t, ending) def _test_readline_file_psfile(self, test_string, ending): f = self.tempfile('temp.txt') - with open(f,'wb') as w: + with open(f, 'wb') as w: if str is bytes: w.write(test_string) else: w.write(test_string.encode('UTF-8')) - with open(f,'rb') as r: + with open(f, 'rb') as r: t = EpsImagePlugin.PSFile(r) self._test_readline(t, ending) - + def test_readline(self): # check all the freaking line endings possible from the spec - #test_string = u'something\r\nelse\n\rbaz\rbif\n' + # test_string = u'something\r\nelse\n\rbaz\rbif\n' line_endings = ['\r\n', '\n'] - not_working_endings = ['\n\r', '\r'] + not_working_endings = ['\n\r', '\r'] strings = ['something', 'else', 'baz', 'bif'] for ending in line_endings: s = ending.join(strings) - # Native python versions will pass these endings. - #self._test_readline_stringio(s, ending) - #self._test_readline_io(s, ending) - #self._test_readline_file_universal(s, ending) - + # Native Python versions will pass these endings. + # self._test_readline_stringio(s, ending) + # self._test_readline_io(s, ending) + # self._test_readline_file_universal(s, ending) + self._test_readline_file_psfile(s, ending) for ending in not_working_endings: # these only work with the PSFile, while they're in spec, # they're not likely to be used s = ending.join(strings) - - # Native python versions may fail on these endings. - #self._test_readline_stringio(s, ending) - #self._test_readline_io(s, ending) - #self._test_readline_file_universal(s, ending) - + + # Native Python versions may fail on these endings. + # self._test_readline_stringio(s, ending) + # self._test_readline_io(s, ending) + # self._test_readline_file_universal(s, ending) + self._test_readline_file_psfile(s, ending) - + + def test_open_eps(self): + # https://github.com/python-pillow/Pillow/issues/1104 + # Arrange + FILES = ["Tests/images/illu10_no_preview.eps", + "Tests/images/illu10_preview.eps", + "Tests/images/illuCS6_no_preview.eps", + "Tests/images/illuCS6_preview.eps"] + + # Act + for filename in FILES: + img = Image.open(filename) + + # Assert + self.assertEqual(img.mode, "RGB") + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index 3580edc18..04c2006c9 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -5,19 +5,23 @@ from PIL import Image # sample ppm stream # created as an export of a palette image from Gimp2.6 # save as...-> hopper.fli, default options. -file = "Tests/images/hopper.fli" -data = open(file, "rb").read() +test_file = "Tests/images/hopper.fli" +data = open(test_file, "rb").read() class TestFileFli(PillowTestCase): def test_sanity(self): - im = Image.open(file) + im = Image.open(test_file) im.load() self.assertEqual(im.mode, "P") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "FLI") + def test_n_frames(self): + im = Image.open(test_file) + self.assertEqual(im.n_frames, 2) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 200b48372..0e9e65a18 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -28,20 +28,29 @@ class TestFileGif(PillowTestCase): def test_optimize(self): from io import BytesIO - def test(optimize): + def test_grayscale(optimize): im = Image.new("L", (1, 1), 0) - file = BytesIO() - im.save(file, "GIF", optimize=optimize) - return len(file.getvalue()) - self.assertEqual(test(0), 800) - self.assertEqual(test(1), 38) + filename = BytesIO() + im.save(filename, "GIF", optimize=optimize) + return len(filename.getvalue()) + + def test_bilevel(optimize): + im = Image.new("1", (1, 1), 0) + test_file = BytesIO() + im.save(test_file, "GIF", optimize=optimize) + return len(test_file.getvalue()) + + self.assertEqual(test_grayscale(0), 800) + self.assertEqual(test_grayscale(1), 38) + self.assertEqual(test_bilevel(0), 800) + self.assertEqual(test_bilevel(1), 800) def test_optimize_full_l(self): from io import BytesIO im = Image.frombytes("L", (16, 16), bytes(bytearray(range(256)))) - file = BytesIO() - im.save(file, "GIF", optimize=True) + test_file = BytesIO() + im.save(test_file, "GIF", optimize=True) self.assertEqual(im.mode, "L") def test_roundtrip(self): @@ -68,7 +77,7 @@ class TestFileGif(PillowTestCase): im = Image.open(TEST_GIF) im = im.convert('RGB') - im = im.resize((100, 100), Image.ANTIALIAS) + im = im.resize((100, 100), Image.LANCZOS) im2 = im.convert('P', palette=Image.ADAPTIVE, colors=256) f = self.tempfile('temp.gif') @@ -125,6 +134,10 @@ class TestFileGif(PillowTestCase): except EOFError: self.assertEqual(framecount, 5) + def test_n_frames(self): + im = Image.open("Tests/images/iss634.gif") + self.assertEqual(im.n_frames, 43) + def test_dispose_none(self): img = Image.open("Tests/images/dispose_none.gif") try: @@ -160,6 +173,33 @@ class TestFileGif(PillowTestCase): # first frame self.assertEqual(img.histogram()[img.info['transparency']], 0) + def test_duration(self): + duration = 1000 + + out = self.tempfile('temp.gif') + fp = open(out, "wb") + im = Image.new('L', (100, 100), '#000') + for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration): + fp.write(s) + fp.write(b";") + fp.close() + reread = Image.open(out) + + self.assertEqual(reread.info['duration'], duration) + + def test_number_of_loops(self): + number_of_loops = 2 + + out = self.tempfile('temp.gif') + fp = open(out, "wb") + im = Image.new('L', (100, 100), '#000') + for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops): + fp.write(s) + fp.write(b";") + fp.close() + reread = Image.open(out) + + self.assertEqual(reread.info['loop'], number_of_loops) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index 99f6da9e3..67eb1335f 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -2,9 +2,11 @@ from helper import unittest, PillowTestCase from PIL import Image +import sys + # sample icon file -file = "Tests/images/pillow.icns" -data = open(file, "rb").read() +TEST_FILE = "Tests/images/pillow.icns" +data = open(TEST_FILE, "rb").read() enable_jpeg2k = hasattr(Image.core, 'jp2klib_version') @@ -14,20 +16,34 @@ class TestFileIcns(PillowTestCase): def test_sanity(self): # Loading this icon by default should result in the largest size # (512x512@2x) being loaded - im = Image.open(file) + im = Image.open(TEST_FILE) im.load() self.assertEqual(im.mode, "RGBA") self.assertEqual(im.size, (1024, 1024)) self.assertEqual(im.format, "ICNS") + @unittest.skipIf(sys.platform != 'darwin', + "requires MacOS") + def test_save(self): + im = Image.open(TEST_FILE) + + temp_file = self.tempfile("temp.icns") + im.save(temp_file) + + reread = Image.open(temp_file) + + self.assertEqual(reread.mode, "RGBA") + self.assertEqual(reread.size, (1024, 1024)) + self.assertEqual(reread.format, "ICNS") + def test_sizes(self): # Check that we can load all of the sizes, and that the final pixel # dimensions are as expected - im = Image.open(file) + im = Image.open(TEST_FILE) for w, h, r in im.info['sizes']: wr = w * r hr = h * r - im2 = Image.open(file) + im2 = Image.open(TEST_FILE) im2.size = (w, h, r) im2.load() self.assertEqual(im2.mode, 'RGBA') diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 12f4ed3f3..f7b52b124 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -1,5 +1,6 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper +import io from PIL import Image # sample ppm stream @@ -16,6 +17,31 @@ class TestFileIco(PillowTestCase): self.assertEqual(im.size, (16, 16)) self.assertEqual(im.format, "ICO") + def test_save_to_bytes(self): + output = io.BytesIO() + im = hopper() + im.save(output, "ico", sizes=[(32, 32), (64, 64)]) + + # the default image + output.seek(0) + reloaded = Image.open(output) + self.assertEqual(reloaded.info['sizes'], set([(32, 32), (64, 64)])) + + self.assertEqual(im.mode, reloaded.mode) + self.assertEqual((64, 64), reloaded.size) + self.assertEqual(reloaded.format, "ICO") + self.assert_image_equal(reloaded, hopper().resize((64, 64), Image.LANCZOS)) + + # the other one + output.seek(0) + reloaded = Image.open(output) + reloaded.size = (32, 32) + + self.assertEqual(im.mode, reloaded.mode) + self.assertEqual((32, 32), reloaded.size) + self.assertEqual(reloaded.format, "ICO") + self.assert_image_equal(reloaded, hopper().resize((32, 32), Image.LANCZOS)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py new file mode 100644 index 000000000..24e00b2f0 --- /dev/null +++ b/Tests/test_file_im.py @@ -0,0 +1,33 @@ +from helper import unittest, PillowTestCase, hopper + +from PIL import Image + +# sample im +TEST_IM = "Tests/images/hopper.im" + + +class TestFileIm(PillowTestCase): + + def test_sanity(self): + im = Image.open(TEST_IM) + im.load() + self.assertEqual(im.mode, "RGB") + self.assertEqual(im.size, (128, 128)) + self.assertEqual(im.format, "IM") + + def test_n_frames(self): + im = Image.open(TEST_IM) + self.assertEqual(im.n_frames, 1) + + def test_roundtrip(self): + out = self.tempfile('temp.im') + im = hopper() + im.save(out) + reread = Image.open(out) + + self.assert_image_equal(reread, im) + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/Tests/test_file_iptc.py b/Tests/test_file_iptc.py index 14eb135aa..84fa873be 100644 --- a/Tests/test_file_iptc.py +++ b/Tests/test_file_iptc.py @@ -11,7 +11,7 @@ class TestFileIptc(PillowTestCase): def dummy_IptcImagePlugin(self): # Create an IptcImagePlugin object without initializing it - class FakeImage: + class FakeImage(object): pass im = FakeImage() im.__class__ = IptcImagePlugin.IptcImageFile diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index b0f179d5f..d4929dd58 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -3,6 +3,7 @@ from helper import djpeg_available, cjpeg_available import random from io import BytesIO +import os from PIL import Image from PIL import ImageFile @@ -22,10 +23,10 @@ class TestFileJpeg(PillowTestCase): def roundtrip(self, im, **options): out = BytesIO() im.save(out, "JPEG", **options) - bytes = out.tell() + test_bytes = out.tell() out.seek(0) im = Image.open(out) - im.bytes = bytes # for testing only + im.bytes = test_bytes # for testing only return im def test_sanity(self): @@ -300,15 +301,12 @@ class TestFileJpeg(PillowTestCase): # sequence wrong length self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[])) # sequence wrong length - self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1,2,3,4,5])) + self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1, 2, 3, 4, 5])) # qtable entry not a sequence self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1])) # qtable entry has wrong number of items - self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[[1,2,3,4]])) - - - + self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[[1, 2, 3, 4]])) @unittest.skipUnless(djpeg_available(), "djpeg not available") def test_load_djpeg(self): @@ -334,6 +332,24 @@ class TestFileJpeg(PillowTestCase): self.assertEqual(tag_ids['RelatedImageWidth'], 0x1001) self.assertEqual(tag_ids['RelatedImageLength'], 0x1002) + def test_MAXBLOCK_scaling(self): + def gen_random_image(size): + """ Generates a very hard to compress file + :param size: tuple + """ + return Image.frombytes('RGB', size, os.urandom(size[0]*size[1] * 3)) + + im = gen_random_image((512, 512)) + f = self.tempfile("temp.jpeg") + im.save(f, quality=100, optimize=True) + + reloaded = Image.open(f) + + # none of these should crash + reloaded.save(f, quality='keep') + reloaded.save(f, quality='keep', progressive=True) + reloaded.save(f, quality='keep', optimize=True) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index db67e9551..9768a881d 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -22,10 +22,10 @@ class TestFileJpeg2k(PillowTestCase): def roundtrip(self, im, **options): out = BytesIO() im.save(out, "JPEG2000", **options) - bytes = out.tell() + test_bytes = out.tell() out.seek(0) im = Image.open(out) - im.bytes = bytes # for testing only + im.bytes = test_bytes # for testing only im.load() return im diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 85b796242..896038b9d 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1,9 +1,12 @@ +from __future__ import print_function from helper import unittest, PillowTestCase, hopper, py3 import os +import io from PIL import Image, TiffImagePlugin + class LibTiffTestCase(PillowTestCase): def setUp(self): @@ -25,33 +28,34 @@ class LibTiffTestCase(PillowTestCase): self.assertEqual(im._compression, 'group4') except: print("No _compression") - print (dir(im)) + print(dir(im)) # can we write it back out, in a different form. out = self.tempfile("temp.png") im.save(out) + class TestFileLibTiff(LibTiffTestCase): def test_g4_tiff(self): """Test the ordinary file path load path""" - file = "Tests/images/hopper_g4_500.tif" - im = Image.open(file) + test_file = "Tests/images/hopper_g4_500.tif" + im = Image.open(test_file) self.assertEqual(im.size, (500, 500)) self._assert_noerr(im) def test_g4_large(self): - file = "Tests/images/pport_g4.tif" - im = Image.open(file) + test_file = "Tests/images/pport_g4.tif" + im = Image.open(test_file) self._assert_noerr(im) def test_g4_tiff_file(self): """Testing the string load path""" - file = "Tests/images/hopper_g4_500.tif" - with open(file, 'rb') as f: + test_file = "Tests/images/hopper_g4_500.tif" + with open(test_file, 'rb') as f: im = Image.open(f) self.assertEqual(im.size, (500, 500)) @@ -59,10 +63,9 @@ class TestFileLibTiff(LibTiffTestCase): def test_g4_tiff_bytesio(self): """Testing the stringio loading code path""" - from io import BytesIO - file = "Tests/images/hopper_g4_500.tif" - s = BytesIO() - with open(file, 'rb') as f: + test_file = "Tests/images/hopper_g4_500.tif" + s = io.BytesIO() + with open(test_file, 'rb') as f: s.write(f.read()) s.seek(0) im = Image.open(s) @@ -87,8 +90,8 @@ class TestFileLibTiff(LibTiffTestCase): def test_g4_write(self): """Checking to see that the saved image is the same as what we wrote""" - file = "Tests/images/hopper_g4_500.tif" - orig = Image.open(file) + test_file = "Tests/images/hopper_g4_500.tif" + orig = Image.open(test_file) out = self.tempfile("temp.tif") rot = orig.transpose(Image.ROTATE_90) @@ -106,8 +109,8 @@ class TestFileLibTiff(LibTiffTestCase): self.assertNotEqual(orig.tobytes(), reread.tobytes()) def test_adobe_deflate_tiff(self): - file = "Tests/images/tiff_adobe_deflate.tif" - im = Image.open(file) + test_file = "Tests/images/tiff_adobe_deflate.tif" + im = Image.open(test_file) self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (278, 374)) @@ -213,8 +216,8 @@ class TestFileLibTiff(LibTiffTestCase): def test_g4_string_info(self): """Tests String data in info directory""" - file = "Tests/images/hopper_g4_500.tif" - orig = Image.open(file) + test_file = "Tests/images/hopper_g4_500.tif" + orig = Image.open(test_file) out = self.tempfile("temp.tif") @@ -241,13 +244,13 @@ class TestFileLibTiff(LibTiffTestCase): im2 = Image.open('Tests/images/12in16bit.tif') if Image.DEBUG: - print (im.getpixel((0, 0))) - print (im.getpixel((0, 1))) - print (im.getpixel((0, 2))) + print(im.getpixel((0, 0))) + print(im.getpixel((0, 1))) + print(im.getpixel((0, 2))) - print (im2.getpixel((0, 0))) - print (im2.getpixel((0, 1))) - print (im2.getpixel((0, 2))) + print(im2.getpixel((0, 0))) + print(im2.getpixel((0, 1))) + print(im2.getpixel((0, 2))) self.assert_image_equal(im, im2) @@ -288,7 +291,7 @@ class TestFileLibTiff(LibTiffTestCase): im2 = Image.open(out) self.assert_image_equal(im, im2) - def xtest_bw_compression_wRGB(self): + def xtest_bw_compression_w_rgb(self): """ This test passes, but when running all tests causes a failure due to output on stderr from the error thrown by libtiff. We need to capture that but not now""" @@ -319,19 +322,19 @@ class TestFileLibTiff(LibTiffTestCase): # file is a multipage tiff, 10x10 green, 10x10 red, 20x20 blue im.seek(0) - self.assertEqual(im.size, (10,10)) - self.assertEqual(im.convert('RGB').getpixel((0,0)), (0,128,0)) + self.assertEqual(im.size, (10, 10)) + self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 128, 0)) self.assertTrue(im.tag.next) im.seek(1) - self.assertEqual(im.size, (10,10)) - self.assertEqual(im.convert('RGB').getpixel((0,0)), (255,0,0)) + self.assertEqual(im.size, (10, 10)) + self.assertEqual(im.convert('RGB').getpixel((0, 0)), (255, 0, 0)) self.assertTrue(im.tag.next) im.seek(2) self.assertFalse(im.tag.next) - self.assertEqual(im.size, (20,20)) - self.assertEqual(im.convert('RGB').getpixel((0,0)), (0,0,255)) + self.assertEqual(im.size, (20, 20)) + self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 0, 255)) TiffImagePlugin.READ_LIBTIFF = False @@ -342,6 +345,49 @@ class TestFileLibTiff(LibTiffTestCase): im.load() self.assertFalse(im.tag.next) + def test_4bit(self): + # Arrange + test_file = "Tests/images/hopper_gray_4bpp.tif" + original = hopper("L") + + # Act + TiffImagePlugin.READ_LIBTIFF = True + im = Image.open(test_file) + TiffImagePlugin.READ_LIBTIFF = False + + # Assert + self.assertEqual(im.size, (128, 128)) + self.assertEqual(im.mode, "L") + self.assert_image_similar(im, original, 7.3) + + def test_save_bytesio(self): + # PR 1011 + # Test TIFF saving to io.BytesIO() object. + + TiffImagePlugin.WRITE_LIBTIFF = True + TiffImagePlugin.READ_LIBTIFF = True + + # Generate test image + pilim = hopper() + + def save_bytesio(compression=None): + + buffer_io = io.BytesIO() + pilim.save(buffer_io, format="tiff", compression=compression) + buffer_io.seek(0) + + pilim_load = Image.open(buffer_io) + self.assert_image_similar(pilim, pilim_load, 0) + + # save_bytesio() + save_bytesio('raw') + save_bytesio("packbits") + save_bytesio("tiff_lzw") + + TiffImagePlugin.WRITE_LIBTIFF = False + TiffImagePlugin.READ_LIBTIFF = False + + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_libtiff_small.py b/Tests/test_file_libtiff_small.py index cd16292a0..c6a639ae9 100644 --- a/Tests/test_file_libtiff_small.py +++ b/Tests/test_file_libtiff_small.py @@ -18,8 +18,8 @@ class TestFileLibTiffSmall(LibTiffTestCase): def test_g4_hopper_file(self): """Testing the open file load path""" - file = "Tests/images/hopper_g4.tif" - with open(file, 'rb') as f: + test_file = "Tests/images/hopper_g4.tif" + with open(test_file, 'rb') as f: im = Image.open(f) self.assertEqual(im.size, (128, 128)) @@ -28,9 +28,9 @@ class TestFileLibTiffSmall(LibTiffTestCase): def test_g4_hopper_bytesio(self): """Testing the bytesio loading code path""" from io import BytesIO - file = "Tests/images/hopper_g4.tif" + test_file = "Tests/images/hopper_g4.tif" s = BytesIO() - with open(file, 'rb') as f: + with open(test_file, 'rb') as f: s.write(f.read()) s.seek(0) im = Image.open(s) @@ -41,8 +41,8 @@ class TestFileLibTiffSmall(LibTiffTestCase): def test_g4_hopper(self): """The 128x128 lena image failed for some reason.""" - file = "Tests/images/hopper_g4.tif" - im = Image.open(file) + test_file = "Tests/images/hopper_g4.tif" + im = Image.open(test_file) self.assertEqual(im.size, (128, 128)) self._assert_noerr(im) diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 066f39dc4..1a0ebc453 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -17,10 +17,10 @@ class TestFileMpo(PillowTestCase): # Note that for now, there is no MPO saving functionality out = BytesIO() im.save(out, "MPO", **options) - bytes = out.tell() + test_bytes = out.tell() out.seek(0) im = Image.open(out) - im.bytes = bytes # for testing only + im.bytes = test_bytes # for testing only return im def test_sanity(self): @@ -95,6 +95,10 @@ class TestFileMpo(PillowTestCase): im.seek(0) self.assertEqual(im.tell(), 0) + def test_n_frames(self): + im = Image.open("Tests/images/sugarshack.mpo") + self.assertEqual(im.n_frames, 2) + def test_image_grab(self): for test_file in test_files: im = Image.open(test_file) diff --git a/Tests/test_file_msp.py b/Tests/test_file_msp.py index c080247a0..f4b1af75e 100644 --- a/Tests/test_file_msp.py +++ b/Tests/test_file_msp.py @@ -2,11 +2,12 @@ from helper import unittest, PillowTestCase, hopper from PIL import Image +TEST_FILE = "Tests/images/hopper.msp" + class TestFileMsp(PillowTestCase): def test_sanity(self): - file = self.tempfile("temp.msp") hopper("1").save(file) @@ -17,6 +18,23 @@ class TestFileMsp(PillowTestCase): self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "MSP") + def test_open(self): + # Arrange + # Act + im = Image.open(TEST_FILE) + + # Assert + self.assertEqual(im.size, (128, 128)) + self.assert_image_similar(im, hopper("1"), 4) + + def test_cannot_save_wrong_mode(self): + # Arrange + im = hopper() + filename = self.tempfile("temp.msp") + + # Act/Assert + self.assertRaises(IOError, lambda: im.save(filename)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index 36d6e0315..10d17d349 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -31,8 +31,8 @@ class TestFilePcx(PillowTestCase): def test_pil184(self): # Check reading of files where xmin/xmax is not zero. - file = "Tests/images/pil184.pcx" - im = Image.open(file) + test_file = "Tests/images/pil184.pcx" + im = Image.open(test_file) self.assertEqual(im.size, (447, 144)) self.assertEqual(im.tile[0][1], (0, 0, 447, 144)) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 7a43414eb..f438e24cc 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -19,9 +19,9 @@ MAGIC = PngImagePlugin._MAGIC def chunk(cid, *data): - file = BytesIO() - PngImagePlugin.putchunk(*(file, cid) + data) - return file.getvalue() + test_file = BytesIO() + PngImagePlugin.putchunk(*(test_file, cid) + data) + return test_file.getvalue() o32 = PngImagePlugin.o32 @@ -56,37 +56,37 @@ class TestFilePng(PillowTestCase): self.assertRegexpMatches( Image.core.zlib_version, "\d+\.\d+\.\d+(\.\d+)?$") - file = self.tempfile("temp.png") + test_file = self.tempfile("temp.png") - hopper("RGB").save(file) + hopper("RGB").save(test_file) - im = Image.open(file) + im = Image.open(test_file) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "PNG") - hopper("1").save(file) - im = Image.open(file) + hopper("1").save(test_file) + im = Image.open(test_file) - hopper("L").save(file) - im = Image.open(file) + hopper("L").save(test_file) + im = Image.open(test_file) - hopper("P").save(file) - im = Image.open(file) + hopper("P").save(test_file) + im = Image.open(test_file) - hopper("RGB").save(file) - im = Image.open(file) + hopper("RGB").save(test_file) + im = Image.open(test_file) - hopper("I").save(file) - im = Image.open(file) + hopper("I").save(test_file) + im = Image.open(test_file) def test_broken(self): # Check reading of totally broken files. In this case, the test # file was checked into Subversion as a text file. - file = "Tests/images/broken.png" - self.assertRaises(IOError, lambda: Image.open(file)) + test_file = "Tests/images/broken.png" + self.assertRaises(IOError, lambda: Image.open(test_file)) def test_bad_text(self): # Make sure PIL can read malformed tEXt chunks (@PIL152) @@ -153,7 +153,7 @@ class TestFilePng(PillowTestCase): im = load(HEAD + chunk(b'iTXt', b'spam\0\1\0en\0Spam\0' + zlib.compress(b"egg")[:1]) + TAIL) - self.assertEqual(im.info, {}) + self.assertEqual(im.info, {'spam': ''}) im = load(HEAD + chunk(b'iTXt', b'spam\0\1\1en\0Spam\0' + zlib.compress(b"egg")) + TAIL) @@ -167,16 +167,16 @@ class TestFilePng(PillowTestCase): def test_interlace(self): - file = "Tests/images/pil123p.png" - im = Image.open(file) + test_file = "Tests/images/pil123p.png" + im = Image.open(test_file) self.assert_image(im, "P", (162, 150)) self.assertTrue(im.info.get("interlace")) im.load() - file = "Tests/images/pil123rgba.png" - im = Image.open(file) + test_file = "Tests/images/pil123rgba.png" + im = Image.open(test_file) self.assert_image(im, "RGBA", (162, 150)) self.assertTrue(im.info.get("interlace")) @@ -184,8 +184,8 @@ class TestFilePng(PillowTestCase): im.load() def test_load_transparent_p(self): - file = "Tests/images/pil123p.png" - im = Image.open(file) + test_file = "Tests/images/pil123p.png" + im = Image.open(test_file) self.assert_image(im, "P", (162, 150)) im = im.convert("RGBA") @@ -195,8 +195,8 @@ class TestFilePng(PillowTestCase): self.assertEqual(len(im.split()[3].getcolors()), 124) def test_load_transparent_rgb(self): - file = "Tests/images/rgb_trns.png" - im = Image.open(file) + test_file = "Tests/images/rgb_trns.png" + im = Image.open(test_file) self.assert_image(im, "RGB", (64, 64)) im = im.convert("RGBA") @@ -209,22 +209,22 @@ class TestFilePng(PillowTestCase): in_file = "Tests/images/pil123p.png" im = Image.open(in_file) - file = self.tempfile("temp.png") - im.save(file) + test_file = self.tempfile("temp.png") + im.save(test_file) def test_save_p_single_transparency(self): in_file = "Tests/images/p_trns_single.png" im = Image.open(in_file) - file = self.tempfile("temp.png") - im.save(file) + test_file = self.tempfile("temp.png") + im.save(test_file) def test_save_l_transparency(self): in_file = "Tests/images/l_trns.png" im = Image.open(in_file) - file = self.tempfile("temp.png") - im.save(file) + test_file = self.tempfile("temp.png") + im.save(test_file) # There are 559 transparent pixels. im = im.convert('RGBA') @@ -234,8 +234,8 @@ class TestFilePng(PillowTestCase): in_file = "Tests/images/caption_6_33_22.png" im = Image.open(in_file) - file = self.tempfile("temp.png") - im.save(file) + test_file = self.tempfile("temp.png") + im.save(test_file) def test_load_verify(self): # Check open/load/verify exception (@PIL150) @@ -245,7 +245,7 @@ class TestFilePng(PillowTestCase): im = Image.open(TEST_PNG_FILE) im.load() - self.assertRaises(RuntimeError, lambda: im.verify()) + self.assertRaises(RuntimeError, im.verify) def test_roundtrip_dpi(self): # Check dpi roundtripping @@ -329,8 +329,8 @@ class TestFilePng(PillowTestCase): # Check writing and reading of tRNS chunks for RGB images. # Independent file sample provided by Sebastian Spaeth. - file = "Tests/images/caption_6_33_22.png" - im = Image.open(file) + test_file = "Tests/images/caption_6_33_22.png" + im = Image.open(test_file) self.assertEqual(im.info["transparency"], (248, 248, 248)) # check saving transparency by default @@ -354,6 +354,13 @@ class TestFilePng(PillowTestCase): self.assert_image_equal(im2.convert('RGBA'), im.convert('RGBA')) + def test_trns_null(self): + # Check reading images with null tRNS value, issue #1239 + test_file = "Tests/images/tRNS_null_1x1.png" + im = Image.open(test_file) + + self.assertEqual(im.info["transparency"], 0) + def test_save_icc_profile_none(self): # check saving files with an ICC profile set to None (omit profile) in_file = "Tests/images/icc_profile_none.png" @@ -374,6 +381,13 @@ class TestFilePng(PillowTestCase): im = roundtrip(im) self.assertEqual(im.info['icc_profile'], expected_icc) + def test_repr_png(self): + im = hopper() + + repr_png = Image.open(BytesIO(im._repr_png_())) + self.assertEqual(repr_png.format, 'PNG') + self.assert_image_equal(im, repr_png) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 3731fd9b1..80c2e60da 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -3,14 +3,14 @@ from helper import unittest, PillowTestCase from PIL import Image # sample ppm stream -file = "Tests/images/hopper.ppm" -data = open(file, "rb").read() +test_file = "Tests/images/hopper.ppm" +data = open(test_file, "rb").read() class TestFilePpm(PillowTestCase): def test_sanity(self): - im = Image.open(file) + im = Image.open(test_file) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 46cef4c78..dca3601b2 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -3,19 +3,26 @@ from helper import unittest, PillowTestCase from PIL import Image # sample ppm stream -file = "Tests/images/hopper.psd" -data = open(file, "rb").read() +test_file = "Tests/images/hopper.psd" +data = open(test_file, "rb").read() class TestImagePsd(PillowTestCase): def test_sanity(self): - im = Image.open(file) + im = Image.open(test_file) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "PSD") + def test_n_frames(self): + im = Image.open("Tests/images/hopper_merged.psd") + self.assertEqual(im.n_frames, 1) + + im = Image.open(test_file) + self.assertEqual(im.n_frames, 2) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index 7bfedad1a..7d24b2fe5 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -42,6 +42,10 @@ class TestImageSpider(PillowTestCase): # Assert self.assertEqual(index, 0) + def test_n_frames(self): + im = Image.open(TEST_FILE) + self.assertEqual(im.n_frames, 1) + def test_loadImageSeries(self): # Arrange not_spider_file = "Tests/images/hopper.ppm" diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 94e0741f8..cf456d609 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -1,42 +1,45 @@ +from __future__ import print_function from helper import unittest, PillowTestCase, hopper, py3 from PIL import Image, TiffImagePlugin +import struct + class TestFileTiff(PillowTestCase): def test_sanity(self): - file = self.tempfile("temp.tif") + filename = self.tempfile("temp.tif") - hopper("RGB").save(file) + hopper("RGB").save(filename) - im = Image.open(file) + im = Image.open(filename) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "TIFF") - hopper("1").save(file) - im = Image.open(file) + hopper("1").save(filename) + im = Image.open(filename) - hopper("L").save(file) - im = Image.open(file) + hopper("L").save(filename) + im = Image.open(filename) - hopper("P").save(file) - im = Image.open(file) + hopper("P").save(filename) + im = Image.open(filename) - hopper("RGB").save(file) - im = Image.open(file) + hopper("RGB").save(filename) + im = Image.open(filename) - hopper("I").save(file) - im = Image.open(file) + hopper("I").save(filename) + im = Image.open(filename) def test_mac_tiff(self): # Read RGBa images from Mac OS X [@PIL136] - file = "Tests/images/pil136.tiff" - im = Image.open(file) + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) self.assertEqual(im.mode, "RGBA") self.assertEqual(im.size, (55, 43)) @@ -50,8 +53,8 @@ class TestFileTiff(PillowTestCase): if "jpeg_decoder" not in codecs: self.skipTest("jpeg support not available") - file = "Tests/images/pil168.tif" - im = Image.open(file) + filename = "Tests/images/pil168.tif" + im = Image.open(filename) self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (256, 256)) @@ -66,8 +69,8 @@ class TestFileTiff(PillowTestCase): def test_xyres_tiff(self): from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION - file = "Tests/images/pil168.tif" - im = Image.open(file) + filename = "Tests/images/pil168.tif" + im = Image.open(filename) assert isinstance(im.tag.tags[X_RESOLUTION][0], tuple) assert isinstance(im.tag.tags[Y_RESOLUTION][0], tuple) # Try to read a file where X,Y_RESOLUTION are ints @@ -76,6 +79,13 @@ class TestFileTiff(PillowTestCase): im._setup() self.assertEqual(im.info['dpi'], (72., 72.)) + def test_bad_exif(self): + try: + Image.open('Tests/images/hopper_bad_exif.jpg')._getexif() + except struct.error: + self.fail("Bad EXIF data should not pass incorrect values to " + "_binary unpack") + def test_little_endian(self): im = Image.open('Tests/images/16bit.cropped.tif') self.assertEqual(im.getpixel((0, 0)), 480) @@ -121,13 +131,13 @@ class TestFileTiff(PillowTestCase): im2 = Image.open('Tests/images/12in16bit.tif') if Image.DEBUG: - print (im.getpixel((0, 0))) - print (im.getpixel((0, 1))) - print (im.getpixel((0, 2))) + print(im.getpixel((0, 0))) + print(im.getpixel((0, 1))) + print(im.getpixel((0, 2))) - print (im2.getpixel((0, 0))) - print (im2.getpixel((0, 1))) - print (im2.getpixel((0, 2))) + print(im2.getpixel((0, 0))) + print(im2.getpixel((0, 1))) + print(im2.getpixel((0, 2))) self.assert_image_equal(im, im2) @@ -141,10 +151,17 @@ class TestFileTiff(PillowTestCase): self.assertEqual( im.getextrema(), (-3.140936851501465, 3.140684127807617)) + def test_n_frames(self): + im = Image.open('Tests/images/multipage-lastframe.tif') + self.assertEqual(im.n_frames, 1) + + im = Image.open('Tests/images/multipage.tiff') + self.assertEqual(im.n_frames, 3) + def test_multipage(self): # issue #862 im = Image.open('Tests/images/multipage.tiff') - # file is a multipage tiff, 10x10 green, 10x10 red, 20x20 blue + # file is a multipage tiff: 10x10 green, 10x10 red, 20x20 blue im.seek(0) self.assertEqual(im.size, (10, 10)) @@ -168,25 +185,36 @@ class TestFileTiff(PillowTestCase): def test___str__(self): # Arrange - file = "Tests/images/pil136.tiff" - im = Image.open(file) + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) # Act ret = str(im.ifd) # Assert self.assertIsInstance(ret, str) + + def test_as_dict(self): + # Arrange + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) + + # Act + ret = im.ifd.as_dict() + + # Assert + self.assertIsInstance(ret, dict) + self.assertEqual( - ret, - '{256: (55,), 257: (43,), 258: (8, 8, 8, 8), 259: (1,), ' - '262: (2,), 296: (2,), 273: (8,), 338: (1,), 277: (4,), ' - '279: (9460,), 282: ((720000, 10000),), ' - '283: ((720000, 10000),), 284: (1,)}') + ret, {256: (55,), 257: (43,), 258: (8, 8, 8, 8), 259: (1,), + 262: (2,), 296: (2,), 273: (8,), 338: (1,), 277: (4,), + 279: (9460,), 282: ((720000, 10000),), + 283: ((720000, 10000),), 284: (1,)}) def test__delitem__(self): # Arrange - file = "Tests/images/pil136.tiff" - im = Image.open(file) + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) len_before = len(im.ifd.as_dict()) # Act @@ -209,7 +237,6 @@ class TestFileTiff(PillowTestCase): def test_load_string(self): # Arrange - from PIL import TiffImagePlugin ifd = TiffImagePlugin.ImageFileDirectory() data = b"abc\0" @@ -243,8 +270,8 @@ class TestFileTiff(PillowTestCase): def test_seek(self): # Arrange - file = "Tests/images/pil136.tiff" - im = Image.open(file) + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) # Act im.seek(-1) @@ -254,8 +281,8 @@ class TestFileTiff(PillowTestCase): def test_seek_eof(self): # Arrange - file = "Tests/images/pil136.tiff" - im = Image.open(file) + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) self.assertEqual(im.tell(), 0) # Act / Assert @@ -294,6 +321,71 @@ class TestFileTiff(PillowTestCase): # Assert self.assertEqual(ret, [0, 1]) + def test_4bit(self): + # Arrange + test_file = "Tests/images/hopper_gray_4bpp.tif" + original = hopper("L") + + # Act + im = Image.open(test_file) + + # Assert + self.assertEqual(im.size, (128, 128)) + self.assertEqual(im.mode, "L") + self.assert_image_similar(im, original, 7.3) + + def test_page_number_x_0(self): + # Issue 973 + # Test TIFF with tag 297 (Page Number) having value of 0 0. + # The first number is the current page number. + # The second is the total number of pages, zero means not available. + + # Arrange + outfile = self.tempfile("temp.tif") + + # Created by printing a page in Chrome to PDF, then: + # /usr/bin/gs -q -sDEVICE=tiffg3 -sOutputFile=total-pages-zero.tif + # -dNOPAUSE /tmp/test.pdf -c quit + infile = "Tests/images/total-pages-zero.tif" + im = Image.open(infile) + + # Act / Assert + # Should not divide by zero + im.save(outfile) + + def test_with_underscores(self): + # Arrange: use underscores + kwargs = {'resolution_unit': 'inch', + 'x_resolution': 72, + 'y_resolution': 36} + filename = self.tempfile("temp.tif") + + # Act + hopper("RGB").save(filename, **kwargs) + + # Assert + from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION + im = Image.open(filename) + self.assertEqual(im.tag.tags[X_RESOLUTION][0][0], 72) + self.assertEqual(im.tag.tags[Y_RESOLUTION][0][0], 36) + + def test_deprecation_warning_with_spaces(self): + # Arrange: use spaces + kwargs = {'resolution unit': 'inch', + 'x resolution': 36, + 'y resolution': 72} + filename = self.tempfile("temp.tif") + + # Act + self.assert_warning(DeprecationWarning, + lambda: hopper("RGB").save(filename, **kwargs)) + + # Assert + from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION + im = Image.open(filename) + self.assertEqual(im.tag.tags[X_RESOLUTION][0][0], 36) + self.assertEqual(im.tag.tags[Y_RESOLUTION][0][0], 72) + def test_save_tiff_with_jpegtables(self): # Arrange outfile = self.tempfile("temp.tif") diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 314590006..dfc16682b 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -16,10 +16,18 @@ class TestFileTiffMetadata(PillowTestCase): img = hopper() textdata = "This is some arbitrary metadata for a text field" + floatdata = 12.345 + doubledata = 67.89 + info = TiffImagePlugin.ImageFileDirectory() info[tag_ids['ImageJMetaDataByteCounts']] = len(textdata) info[tag_ids['ImageJMetaData']] = textdata + info[tag_ids['RollAngle']] = floatdata + info.tagtype[tag_ids['RollAngle']] = 11 + + info[tag_ids['YawAngle']] = doubledata + info.tagtype[tag_ids['YawAngle']] = 12 f = self.tempfile("temp.tif") @@ -29,6 +37,9 @@ class TestFileTiffMetadata(PillowTestCase): self.assertEqual(loaded.tag[50838], (len(textdata),)) self.assertEqual(loaded.tag[50839], textdata) + self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']][0], floatdata, + places=5) + self.assertAlmostEqual(loaded.tag[tag_ids['YawAngle']][0], doubledata) def test_read_metadata(self): img = Image.open('Tests/images/hopper_g4.tif') diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 1ee375131..8c8313dd9 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -4,7 +4,7 @@ from PIL import Image try: from PIL import _webp -except: +except ImportError: # Skip in setUp() pass @@ -14,7 +14,7 @@ class TestFileWebp(PillowTestCase): def setUp(self): try: from PIL import _webp - except: + except ImportError: self.skipTest('WebP support not installed') def test_version(self): diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py index 43dee648a..f316b71e1 100644 --- a/Tests/test_file_webp_alpha.py +++ b/Tests/test_file_webp_alpha.py @@ -4,7 +4,7 @@ from PIL import Image try: from PIL import _webp -except: +except ImportError: pass # Skip in setUp() @@ -14,7 +14,7 @@ class TestFileWebpAlpha(PillowTestCase): def setUp(self): try: from PIL import _webp - except: + except ImportError: self.skipTest('WebP support not installed') if _webp.WebPDecoderBuggyAlpha(self): @@ -80,10 +80,14 @@ class TestFileWebpAlpha(PillowTestCase): self.assertEqual(image.mode, "RGBA") self.assertEqual(image.size, (10, 10)) self.assertEqual(image.format, "WEBP") - image.load - image.getdata + image.load() + image.getdata() - self.assert_image_similar(image, pil_image, 1.0) + # early versions of webp are known to produce higher deviations: deal with it + if _webp.WebPDecoderVersion(self) <= 0x201: + self.assert_image_similar(image, pil_image, 3.0) + else: + self.assert_image_similar(image, pil_image, 1.0) if __name__ == '__main__': diff --git a/Tests/test_file_webp_metadata.py b/Tests/test_file_webp_metadata.py index f2f18d713..8b1254d61 100644 --- a/Tests/test_file_webp_metadata.py +++ b/Tests/test_file_webp_metadata.py @@ -15,7 +15,6 @@ class TestFileWebpMetadata(PillowTestCase): if not _webp.HAVE_WEBPMUX: self.skipTest('WebPMux support not installed') - def test_read_exif_metadata(self): file_path = "Tests/images/flower.webp" @@ -42,12 +41,12 @@ class TestFileWebpMetadata(PillowTestCase): image = Image.open(file_path) expected_exif = image.info['exif'] - buffer = BytesIO() + test_buffer = BytesIO() - image.save(buffer, "webp", exif=expected_exif) + image.save(test_buffer, "webp", exif=expected_exif) - buffer.seek(0) - webp_image = Image.open(buffer) + test_buffer.seek(0) + webp_image = Image.open(test_buffer) webp_exif = webp_image.info.get('exif', None) self.assertTrue(webp_exif) @@ -77,12 +76,12 @@ class TestFileWebpMetadata(PillowTestCase): image = Image.open(file_path) expected_icc_profile = image.info['icc_profile'] - buffer = BytesIO() + test_buffer = BytesIO() - image.save(buffer, "webp", icc_profile=expected_icc_profile) + image.save(test_buffer, "webp", icc_profile=expected_icc_profile) - buffer.seek(0) - webp_image = Image.open(buffer) + test_buffer.seek(0) + webp_image = Image.open(test_buffer) webp_icc_profile = webp_image.info.get('icc_profile', None) @@ -97,14 +96,14 @@ class TestFileWebpMetadata(PillowTestCase): file_path = "Tests/images/flower.jpg" image = Image.open(file_path) - image.info['exif'] + self.assertTrue('exif' in image.info) - buffer = BytesIO() + test_buffer = BytesIO() - image.save(buffer, "webp") + image.save(test_buffer, "webp") - buffer.seek(0) - webp_image = Image.open(buffer) + test_buffer.seek(0) + webp_image = Image.open(test_buffer) self.assertFalse(webp_image._getexif()) diff --git a/Tests/test_file_xpm.py b/Tests/test_file_xpm.py index 9a666a705..6a6817048 100644 --- a/Tests/test_file_xpm.py +++ b/Tests/test_file_xpm.py @@ -15,7 +15,7 @@ class TestFileXpm(PillowTestCase): self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "XPM") - #large error due to quantization->44 colors. + # large error due to quantization->44 colors. self.assert_image_similar(im.convert('RGB'), hopper('RGB'), 60) def test_load_read(self): diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py index 0df8e866b..b844f1228 100644 --- a/Tests/test_font_bdf.py +++ b/Tests/test_font_bdf.py @@ -9,8 +9,8 @@ class TestFontBdf(PillowTestCase): def test_sanity(self): - file = open(filename, "rb") - font = BdfFontFile.BdfFontFile(file) + test_file = open(filename, "rb") + font = BdfFontFile.BdfFontFile(test_file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 190) diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index 5e9e02c8c..3cc6afa64 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -17,8 +17,8 @@ class TestFontPcf(PillowTestCase): self.skipTest("zlib support not available") def save_font(self): - file = open(fontname, "rb") - font = PcfFontFile.PcfFontFile(file) + test_file = open(fontname, "rb") + font = PcfFontFile.PcfFontFile(test_file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 192) diff --git a/Tests/test_format_hsv.py b/Tests/test_format_hsv.py index d9baf91bf..dd4413f41 100644 --- a/Tests/test_format_hsv.py +++ b/Tests/test_format_hsv.py @@ -1,3 +1,4 @@ +from __future__ import print_function from helper import unittest, PillowTestCase, hopper from PIL import Image diff --git a/Tests/test_image.py b/Tests/test_image.py index 26e45b10a..caee70fec 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1,7 +1,6 @@ from helper import unittest, PillowTestCase, hopper from PIL import Image -import sys class TestImage(PillowTestCase): @@ -42,8 +41,8 @@ class TestImage(PillowTestCase): im.paste(0, (0, 0, 100, 100)) self.assertFalse(im.readonly) - file = self.tempfile("temp.ppm") - im._dump(file) + test_file = self.tempfile("temp.ppm") + im._dump(test_file) def test_comparison_with_other_type(self): # Arrange @@ -168,8 +167,6 @@ class TestImage(PillowTestCase): ValueError, lambda: Image.effect_mandelbrot(size, extent, quality)) - @unittest.skipUnless(sys.platform.startswith('win32'), - "Stalls on Travis CI, passes on Windows") def test_effect_noise(self): # Arrange size = (100, 100) @@ -180,8 +177,8 @@ class TestImage(PillowTestCase): # Assert self.assertEqual(im.size, (100, 100)) - self.assertEqual(im.getpixel((0, 0)), 60) - self.assertEqual(im.getpixel((0, 1)), 28) + self.assertEqual(im.mode, "L") + self.assertNotEqual(im.getpixel((0, 0)), im.getpixel((0, 1))) def test_effect_spread(self): # Arrange diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index d29fd3dfd..6a694b3ca 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -76,10 +76,10 @@ class TestImageFilter(PillowTestCase): # 0 1 2 # 3 4 5 # 6 7 8 - min = im.filter(ImageFilter.MinFilter).getpixel((1, 1)) + minimum = im.filter(ImageFilter.MinFilter).getpixel((1, 1)) med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1)) - max = im.filter(ImageFilter.MaxFilter).getpixel((1, 1)) - return min, med, max + maximum = im.filter(ImageFilter.MaxFilter).getpixel((1, 1)) + return minimum, med, maximum self.assertEqual(rankfilter("1"), (0, 4, 8)) self.assertEqual(rankfilter("L"), (0, 4, 8)) diff --git a/Tests/test_image_load.py b/Tests/test_image_load.py index b1fc73182..f9d92e13c 100644 --- a/Tests/test_image_load.py +++ b/Tests/test_image_load.py @@ -18,7 +18,7 @@ class TestImageLoad(PillowTestCase): def test_close(self): im = Image.open("Tests/images/hopper.gif") im.close() - self.assertRaises(ValueError, lambda: im.load()) + self.assertRaises(ValueError, im.load) self.assertRaises(ValueError, lambda: im.getpixel((0, 0))) def test_contextmanager(self): diff --git a/Tests/test_image_putdata.py b/Tests/test_image_putdata.py index 46a2ace71..b46456eba 100644 --- a/Tests/test_image_putdata.py +++ b/Tests/test_image_putdata.py @@ -1,4 +1,5 @@ from helper import unittest, PillowTestCase, hopper +from array import array import sys @@ -63,6 +64,25 @@ class TestImagePutData(PillowTestCase): target = [2.0 * float(elt) + 256.0 for elt in data] self.assertEqual(list(im.getdata()), target) + def test_array_B(self): + # shouldn't segfault + # see https://github.com/python-pillow/Pillow/issues/1008 + + arr = array('B', [0])*15000 + im = Image.new('L', (150, 100)) + im.putdata(arr) + + self.assertEqual(len(im.getdata()), len(arr)) + + def test_array_F(self): + # shouldn't segfault + # see https://github.com/python-pillow/Pillow/issues/1008 + + im = Image.new('F', (150, 100)) + arr = array('f', [0.0])*15000 + im.putdata(arr) + + self.assertEqual(len(im.getdata()), len(arr)) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index 603f598d8..d59f8ba14 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -1,5 +1,91 @@ +""" +Tests for resize functionality. +""" +from itertools import permutations + from helper import unittest, PillowTestCase, hopper +from PIL import Image + + +class TestImagingCoreResize(PillowTestCase): + + def resize(self, im, size, f): + # Image class independent version of resize. + im.load() + return im._new(im.im.resize(size, f)) + + def test_nearest_mode(self): + for mode in ["1", "P", "L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr", + "I;16"]: # exotic mode + im = hopper(mode) + r = self.resize(im, (15, 12), Image.NEAREST) + self.assertEqual(r.mode, mode) + self.assertEqual(r.size, (15, 12)) + self.assertEqual(r.im.bands, im.im.bands) + + def test_convolution_modes(self): + self.assertRaises(ValueError, self.resize, hopper("1"), + (15, 12), Image.BILINEAR) + self.assertRaises(ValueError, self.resize, hopper("P"), + (15, 12), Image.BILINEAR) + self.assertRaises(ValueError, self.resize, hopper("I;16"), + (15, 12), Image.BILINEAR) + for mode in ["L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr"]: + im = hopper(mode) + r = self.resize(im, (15, 12), Image.BILINEAR) + self.assertEqual(r.mode, mode) + self.assertEqual(r.size, (15, 12)) + self.assertEqual(r.im.bands, im.im.bands) + + def test_reduce_filters(self): + for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]: + r = self.resize(hopper("RGB"), (15, 12), f) + self.assertEqual(r.mode, "RGB") + self.assertEqual(r.size, (15, 12)) + + def test_enlarge_filters(self): + for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]: + r = self.resize(hopper("RGB"), (212, 195), f) + self.assertEqual(r.mode, "RGB") + self.assertEqual(r.size, (212, 195)) + + def test_endianness(self): + # Make an image with one colored pixel, in one channel. + # When resized, that channel should be the same as a GS image. + # Other channels should be unaffected. + # The R and A channels should not swap, which is indicative of + # an endianness issues. + + samples = { + 'blank': Image.new('L', (2, 2), 0), + 'filled': Image.new('L', (2, 2), 255), + 'dirty': Image.new('L', (2, 2), 0), + } + samples['dirty'].putpixel((1, 1), 128) + + for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]: + # samples resized with current filter + references = dict( + (name, self.resize(ch, (4, 4), f)) + for name, ch in samples.items() + ) + + for mode, channels_set in [ + ('RGB', ('blank', 'filled', 'dirty')), + ('RGBA', ('blank', 'blank', 'filled', 'dirty')), + ('LA', ('filled', 'dirty')), + ]: + for channels in set(permutations(channels_set)): + # compile image from different channels permutations + im = Image.merge(mode, [samples[ch] for ch in channels]) + resized = self.resize(im, (4, 4), f) + + for i, ch in enumerate(resized.split()): + # check what resized channel in image is the same + # as separately resized channel + self.assert_image_equal(ch, references[channels[i]]) + class TestImageResize(PillowTestCase): @@ -9,8 +95,8 @@ class TestImageResize(PillowTestCase): self.assertEqual(out.mode, mode) self.assertEqual(out.size, size) for mode in "1", "P", "L", "RGB", "I", "F": - resize(mode, (100, 100)) - resize(mode, (200, 200)) + resize(mode, (112, 103)) + resize(mode, (188, 214)) if __name__ == '__main__': diff --git a/Tests/test_image_split.py b/Tests/test_image_split.py index 0e057523b..beab7b546 100644 --- a/Tests/test_image_split.py +++ b/Tests/test_image_split.py @@ -45,13 +45,13 @@ class TestImageSplit(PillowTestCase): codecs = dir(Image.core) if 'zip_encoder' in codecs: - file = self.tempfile("temp.png") + test_file = self.tempfile("temp.png") else: - file = self.tempfile("temp.pcx") + test_file = self.tempfile("temp.pcx") def split_open(mode): - hopper(mode).save(file) - im = Image.open(file) + hopper(mode).save(test_file) + im = Image.open(test_file) return len(im.split()) self.assertEqual(split_open("1"), 1) self.assertEqual(split_open("L"), 1) diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index c4111d0b6..26ff8822c 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -44,7 +44,9 @@ class TestImageTransform(PillowTestCase): w//2, h//2, w//2, 0), Image.BILINEAR) - scaled = im.resize((w*2, h*2), Image.BILINEAR).crop((0, 0, w, h)) + scaled = im.transform((w, h), Image.AFFINE, + (.5, 0, 0, 0, .5, 0), + Image.BILINEAR) self.assert_image_equal(transformed, scaled) @@ -61,9 +63,9 @@ class TestImageTransform(PillowTestCase): w, h, w, 0))], # ul -> ccw around quad Image.BILINEAR) - # transformed.save('transformed.png') - - scaled = im.resize((w//2, h//2), Image.BILINEAR) + scaled = im.transform((w//2, h//2), Image.AFFINE, + (2, 0, 0, 0, 2, 0), + Image.BILINEAR) checker = Image.new('RGBA', im.size) checker.paste(scaled, (0, 0)) @@ -126,12 +128,13 @@ class TestImageTransform(PillowTestCase): # Running by default, but I'd totally understand not doing it in # the future - foo = [ + pattern = [ Image.new('RGBA', (1024, 1024), (a, a, a, a)) - for a in range(1, 65)] + for a in range(1, 65) + ] # Yeah. Watch some JIT optimize this out. - foo = None + pattern = None self.test_mesh() diff --git a/Tests/test_image_transpose.py b/Tests/test_image_transpose.py index 3e4257bc0..e13fc8605 100644 --- a/Tests/test_image_transpose.py +++ b/Tests/test_image_transpose.py @@ -1,30 +1,115 @@ -from helper import unittest, PillowTestCase, hopper +import helper +from helper import unittest, PillowTestCase -from PIL import Image - -FLIP_LEFT_RIGHT = Image.FLIP_LEFT_RIGHT -FLIP_TOP_BOTTOM = Image.FLIP_TOP_BOTTOM -ROTATE_90 = Image.ROTATE_90 -ROTATE_180 = Image.ROTATE_180 -ROTATE_270 = Image.ROTATE_270 +from PIL.Image import (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180, + ROTATE_270, TRANSPOSE) class TestImageTranspose(PillowTestCase): - def test_sanity(self): + hopper = { + 'L': helper.hopper('L').crop((0, 0, 121, 127)).copy(), + 'RGB': helper.hopper('RGB').crop((0, 0, 121, 127)).copy(), + } - im = hopper() + def test_flip_left_right(self): + def transpose(mode): + im = self.hopper[mode] + out = im.transpose(FLIP_LEFT_RIGHT) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size) - im.transpose(FLIP_LEFT_RIGHT) - im.transpose(FLIP_TOP_BOTTOM) + x, y = im.size + self.assertEqual(im.getpixel((1, 1)), out.getpixel((x-2, 1))) + self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, 1))) + self.assertEqual(im.getpixel((1, y-2)), out.getpixel((x-2, y-2))) + self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, y-2))) - im.transpose(ROTATE_90) - im.transpose(ROTATE_180) - im.transpose(ROTATE_270) + for mode in ("L", "RGB"): + transpose(mode) + + def test_flip_top_bottom(self): + def transpose(mode): + im = self.hopper[mode] + out = im.transpose(FLIP_TOP_BOTTOM) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size) + + x, y = im.size + self.assertEqual(im.getpixel((1, 1)), out.getpixel((1, y-2))) + self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((x-2, y-2))) + self.assertEqual(im.getpixel((1, y-2)), out.getpixel((1, 1))) + self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((x-2, 1))) + + for mode in ("L", "RGB"): + transpose(mode) + + def test_rotate_90(self): + def transpose(mode): + im = self.hopper[mode] + out = im.transpose(ROTATE_90) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size[::-1]) + + x, y = im.size + self.assertEqual(im.getpixel((1, 1)), out.getpixel((1, x-2))) + self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, 1))) + self.assertEqual(im.getpixel((1, y-2)), out.getpixel((y-2, x-2))) + self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((y-2, 1))) + + for mode in ("L", "RGB"): + transpose(mode) + + def test_rotate_180(self): + def transpose(mode): + im = self.hopper[mode] + out = im.transpose(ROTATE_180) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size) + + x, y = im.size + self.assertEqual(im.getpixel((1, 1)), out.getpixel((x-2, y-2))) + self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, y-2))) + self.assertEqual(im.getpixel((1, y-2)), out.getpixel((x-2, 1))) + self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, 1))) + + for mode in ("L", "RGB"): + transpose(mode) + + def test_rotate_270(self): + def transpose(mode): + im = self.hopper[mode] + out = im.transpose(ROTATE_270) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size[::-1]) + + x, y = im.size + self.assertEqual(im.getpixel((1, 1)), out.getpixel((y-2, 1))) + self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((y-2, x-2))) + self.assertEqual(im.getpixel((1, y-2)), out.getpixel((1, 1))) + self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((1, x-2))) + + for mode in ("L", "RGB"): + transpose(mode) + + def test_transpose(self): + def transpose(mode): + im = self.hopper[mode] + out = im.transpose(TRANSPOSE) + self.assertEqual(out.mode, mode) + self.assertEqual(out.size, im.size[::-1]) + + x, y = im.size + self.assertEqual(im.getpixel((1, 1)), out.getpixel((1, 1))) + self.assertEqual(im.getpixel((x-2, 1)), out.getpixel((1, x-2))) + self.assertEqual(im.getpixel((1, y-2)), out.getpixel((y-2, 1))) + self.assertEqual(im.getpixel((x-2, y-2)), out.getpixel((y-2, x-2))) + + for mode in ("L", "RGB"): + transpose(mode) def test_roundtrip(self): - - im = hopper() + im = self.hopper['L'] def transpose(first, second): return im.transpose(first).transpose(second) @@ -33,12 +118,13 @@ class TestImageTranspose(PillowTestCase): im, transpose(FLIP_LEFT_RIGHT, FLIP_LEFT_RIGHT)) self.assert_image_equal( im, transpose(FLIP_TOP_BOTTOM, FLIP_TOP_BOTTOM)) - self.assert_image_equal(im, transpose(ROTATE_90, ROTATE_270)) self.assert_image_equal(im, transpose(ROTATE_180, ROTATE_180)) + self.assert_image_equal( + im.transpose(TRANSPOSE), transpose(ROTATE_90, FLIP_TOP_BOTTOM)) + self.assert_image_equal( + im.transpose(TRANSPOSE), transpose(ROTATE_270, FLIP_LEFT_RIGHT)) if __name__ == '__main__': unittest.main() - -# End of file diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index d669b4de5..7a2a57151 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -4,7 +4,7 @@ from PIL import Image from io import BytesIO import os - + try: from PIL import ImageCms from PIL.ImageCms import ImageCmsProfile @@ -17,6 +17,7 @@ except ImportError as v: SRGB = "Tests/icc/sRGB_IEC61966-2-1_black_scaled.icc" HAVE_PROFILE = os.path.exists(SRGB) + class TestImageCms(PillowTestCase): def setUp(self): @@ -32,7 +33,7 @@ class TestImageCms(PillowTestCase): self.skipTest("SRGB profile not available") def test_sanity(self): - + # basic smoke test. # this mostly follows the cms_test outline. @@ -240,7 +241,6 @@ class TestImageCms(PillowTestCase): self.assert_image_similar(hopper(), out, 2) def test_profile_tobytes(self): - from io import BytesIO i = Image.open("Tests/images/rgb.jpg") p = ImageCms.getOpenProfile(BytesIO(i.info["icc_profile"])) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 6adc6c1f2..a1ed20a3a 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -64,7 +64,7 @@ class TestImageDraw(PillowTestCase): # Assert self.assert_image_similar( - im, Image.open("Tests/images/imagedraw_arc.png"),1) + im, Image.open("Tests/images/imagedraw_arc.png"), 1) def test_arc1(self): self.helper_arc(BBOX1) @@ -97,7 +97,7 @@ class TestImageDraw(PillowTestCase): # Assert self.assert_image_similar( - im, Image.open("Tests/images/imagedraw_chord.png"),1) + im, Image.open("Tests/images/imagedraw_chord.png"), 1) def test_chord1(self): self.helper_chord(BBOX1) @@ -116,7 +116,7 @@ class TestImageDraw(PillowTestCase): # Assert self.assert_image_similar( - im, Image.open("Tests/images/imagedraw_ellipse.png"),1) + im, Image.open("Tests/images/imagedraw_ellipse.png"), 1) def test_ellipse1(self): self.helper_ellipse(BBOX1) @@ -154,7 +154,7 @@ class TestImageDraw(PillowTestCase): # Assert self.assert_image_similar( - im, Image.open("Tests/images/imagedraw_pieslice.png"),1) + im, Image.open("Tests/images/imagedraw_pieslice.png"), 1) def test_pieslice1(self): self.helper_pieslice(BBOX1) @@ -300,32 +300,32 @@ class TestImageDraw(PillowTestCase): img, draw = self.create_base_image_draw((20, 20)) draw.line((5, 5, 14, 5), BLACK, 2) self.assert_image_equal( - img, expected, 'line straigth horizontal normal 2px wide failed') + img, expected, 'line straight horizontal normal 2px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_horizontal_w2px_inverted.png')) expected.load() img, draw = self.create_base_image_draw((20, 20)) draw.line((14, 5, 5, 5), BLACK, 2) self.assert_image_equal( - img, expected, 'line straigth horizontal inverted 2px wide failed') + img, expected, 'line straight horizontal inverted 2px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_horizontal_w3px.png')) expected.load() img, draw = self.create_base_image_draw((20, 20)) draw.line((5, 5, 14, 5), BLACK, 3) self.assert_image_equal( - img, expected, 'line straigth horizontal normal 3px wide failed') + img, expected, 'line straight horizontal normal 3px wide failed') img, draw = self.create_base_image_draw((20, 20)) draw.line((14, 5, 5, 5), BLACK, 3) self.assert_image_equal( - img, expected, 'line straigth horizontal inverted 3px wide failed') + img, expected, 'line straight horizontal inverted 3px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_horizontal_w101px.png')) expected.load() img, draw = self.create_base_image_draw((200, 110)) draw.line((5, 55, 195, 55), BLACK, 101) self.assert_image_equal( - img, expected, 'line straigth horizontal 101px wide failed') + img, expected, 'line straight horizontal 101px wide failed') def test_line_h_s1_w2(self): self.skipTest('failing') @@ -344,32 +344,32 @@ class TestImageDraw(PillowTestCase): img, draw = self.create_base_image_draw((20, 20)) draw.line((5, 5, 5, 14), BLACK, 2) self.assert_image_equal( - img, expected, 'line straigth vertical normal 2px wide failed') + img, expected, 'line straight vertical normal 2px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_vertical_w2px_inverted.png')) expected.load() img, draw = self.create_base_image_draw((20, 20)) draw.line((5, 14, 5, 5), BLACK, 2) self.assert_image_equal( - img, expected, 'line straigth vertical inverted 2px wide failed') + img, expected, 'line straight vertical inverted 2px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_vertical_w3px.png')) expected.load() img, draw = self.create_base_image_draw((20, 20)) draw.line((5, 5, 5, 14), BLACK, 3) self.assert_image_equal( - img, expected, 'line straigth vertical normal 3px wide failed') + img, expected, 'line straight vertical normal 3px wide failed') img, draw = self.create_base_image_draw((20, 20)) draw.line((5, 14, 5, 5), BLACK, 3) self.assert_image_equal( - img, expected, 'line straigth vertical inverted 3px wide failed') + img, expected, 'line straight vertical inverted 3px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_vertical_w101px.png')) expected.load() img, draw = self.create_base_image_draw((110, 200)) draw.line((55, 5, 55, 195), BLACK, 101) self.assert_image_equal(img, expected, - 'line straigth vertical 101px wide failed') + 'line straight vertical 101px wide failed') expected = Image.open(os.path.join(IMAGES_PATH, 'line_vertical_slope1px_w2px.png')) expected.load() diff --git a/Tests/test_imageenhance.py b/Tests/test_imageenhance.py index 58042db85..82ba2e5db 100644 --- a/Tests/test_imageenhance.py +++ b/Tests/test_imageenhance.py @@ -21,23 +21,22 @@ class TestImageEnhance(PillowTestCase): im = Image.new("RGB", (1, 1)) ImageEnhance.Sharpness(im).enhance(0.5) - def _half_transparent_image(self): # returns an image, half transparent, half solid im = hopper('RGB') - + transparent = Image.new('L', im.size, 0) solid = Image.new('L', (im.size[0]//2, im.size[1]), 255) - transparent.paste(solid, (0,0)) + transparent.paste(solid, (0, 0)) im.putalpha(transparent) return im - def _check_alpha(self,im, original, op, amount): + def _check_alpha(self, im, original, op, amount): self.assertEqual(im.getbands(), original.getbands()) self.assert_image_equal(im.split()[-1], original.split()[-1], "Diff on %s: %s" % (op, amount)) - + def test_alpha(self): # Issue https://github.com/python-pillow/Pillow/issues/899 # Is alpha preserved through image enhancement? @@ -45,10 +44,10 @@ class TestImageEnhance(PillowTestCase): original = self._half_transparent_image() for op in ['Color', 'Brightness', 'Contrast', 'Sharpness']: - for amount in [0,0.5,1.0]: - self._check_alpha(getattr(ImageEnhance,op)(original).enhance(amount), + for amount in [0, 0.5, 1.0]: + self._check_alpha(getattr(ImageEnhance, op)(original).enhance(amount), original, op, amount) - + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 662a3bfb0..5311b899f 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -24,11 +24,11 @@ class TestImageFile(PillowTestCase): if format in ("MSP", "XBM"): im = im.convert("1") - file = BytesIO() + test_file = BytesIO() - im.save(file, format) + im.save(test_file, format) - data = file.getvalue() + data = test_file.getvalue() parser = ImageFile.Parser() parser.feed(data) @@ -58,7 +58,7 @@ class TestImageFile(PillowTestCase): # This test fails on Ubuntu 12.04, PPC (Bigendian) It # appears to be a ghostscript 9.05 bug, since the # ghostscript rendering is wonky and the file is identical - # to that written on ubuntu 12.04 x64 + # to that written on ubuntu 12.04 x64 # md5sum: ba974835ff2d6f3f2fd0053a23521d4a # EPS comes back in RGB: diff --git a/Tests/test_imagefileio.py b/Tests/test_imagefileio.py index f18474403..b06178437 100644 --- a/Tests/test_imagefileio.py +++ b/Tests/test_imagefileio.py @@ -8,7 +8,7 @@ class TestImageFileIo(PillowTestCase): def test_fileio(self): - class DumbFile: + class DumbFile(object): def __init__(self, data): self.data = data diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index ed2439e7c..88858c717 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -4,6 +4,8 @@ from PIL import Image from PIL import ImageDraw from io import BytesIO import os +import sys +import copy FONT_PATH = "Tests/fonts/FreeMono.ttf" FONT_SIZE = 20 @@ -13,12 +15,53 @@ try: from PIL import ImageFont ImageFont.core.getfont # check if freetype is available + class SimplePatcher(object): + def __init__(self, parent_obj, attr_name, value): + self._parent_obj = parent_obj + self._attr_name = attr_name + self._saved = None + self._is_saved = False + self._value = value + + def __enter__(self): + # Patch the attr on the object + if hasattr(self._parent_obj, self._attr_name): + self._saved = getattr(self._parent_obj, self._attr_name) + setattr(self._parent_obj, self._attr_name, self._value) + self._is_saved = True + else: + setattr(self._parent_obj, self._attr_name, self._value) + self._is_saved = False + + def __exit__(self, type, value, traceback): + # Restore the original value + if self._is_saved: + setattr(self._parent_obj, self._attr_name, self._saved) + else: + delattr(self._parent_obj, self._attr_name) + class TestImageFont(PillowTestCase): def test_sanity(self): self.assertRegexpMatches( ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$") + def test_font_properties(self): + ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) + self.assertEqual(ttf.path, FONT_PATH) + self.assertEqual(ttf.size, FONT_SIZE) + + ttf_copy = ttf.font_variant() + self.assertEqual(ttf_copy.path, FONT_PATH) + self.assertEqual(ttf_copy.size, FONT_SIZE) + + ttf_copy = ttf.font_variant(size=FONT_SIZE+1) + self.assertEqual(ttf_copy.size, FONT_SIZE+1) + + second_font_path = "Tests/fonts/DejaVuSans.ttf" + ttf_copy = ttf.font_variant(font=second_font_path) + self.assertEqual(ttf_copy.path, second_font_path) + def test_font_with_name(self): ImageFont.truetype(FONT_PATH, FONT_SIZE) self._render(FONT_PATH) @@ -51,7 +94,8 @@ try: def _render(self, font): txt = "Hello World!" ttf = ImageFont.truetype(font, FONT_SIZE) - w, h = ttf.getsize(txt) + ttf.getsize(txt) + img = Image.new("RGB", (256, 64), "white") d = ImageDraw.Draw(img) d.text((10, 10), txt, font=ttf, fill='black') @@ -83,7 +127,7 @@ try: target = 'Tests/images/rectangle_surrounding_text.png' target_img = Image.open(target) - self.assert_image_equal(im, target_img) + self.assert_image_similar(im, target_img, .5) def test_render_multiline(self): im = Image.new(mode='RGB', size=(300, 100)) @@ -192,6 +236,79 @@ try: # Assert self.assert_image_equal(im, target_img) + def _test_fake_loading_font(self, path_to_fake, fontname): + # Make a copy of FreeTypeFont so we can patch the original + free_type_font = copy.deepcopy(ImageFont.FreeTypeFont) + with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font): + def loadable_font(filepath, size, index, encoding): + if filepath == path_to_fake: + return ImageFont._FreeTypeFont(FONT_PATH, size, index, + encoding) + return ImageFont._FreeTypeFont(filepath, size, index, + encoding) + with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font): + font = ImageFont.truetype(fontname) + # Make sure it's loaded + name = font.getname() + self.assertEqual(('FreeMono', 'Regular'), name) + + @unittest.skipIf(sys.platform.startswith('win32'), + "requires Unix or MacOS") + def test_find_linux_font(self): + # A lot of mocking here - this is more for hitting code and + # catching syntax like errors + font_directory = '/usr/local/share/fonts' + with SimplePatcher(sys, 'platform', 'linux'): + patched_env = copy.deepcopy(os.environ) + patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/' + with SimplePatcher(os, 'environ', patched_env): + def fake_walker(path): + if path == font_directory: + return [(path, [], [ + 'Arial.ttf', 'Single.otf', 'Duplicate.otf', + 'Duplicate.ttf'], )] + return [(path, [], ['some_random_font.ttf'], )] + with SimplePatcher(os, 'walk', fake_walker): + # Test that the font loads both with and without the + # extension + self._test_fake_loading_font( + font_directory+'/Arial.ttf', 'Arial.ttf') + self._test_fake_loading_font( + font_directory+'/Arial.ttf', 'Arial') + + # Test that non-ttf fonts can be found without the + # extension + self._test_fake_loading_font( + font_directory+'/Single.otf', 'Single') + + # Test that ttf fonts are preferred if the extension is + # not specified + self._test_fake_loading_font( + font_directory+'/Duplicate.ttf', 'Duplicate') + + @unittest.skipIf(sys.platform.startswith('win32'), + "requires Unix or MacOS") + def test_find_osx_font(self): + # Like the linux test, more cover hitting code rather than testing + # correctness. + font_directory = '/System/Library/Fonts' + with SimplePatcher(sys, 'platform', 'darwin'): + def fake_walker(path): + if path == font_directory: + return [(path, [], + ['Arial.ttf', 'Single.otf', + 'Duplicate.otf', 'Duplicate.ttf'], )] + return [(path, [], ['some_random_font.ttf'], )] + with SimplePatcher(os, 'walk', fake_walker): + self._test_fake_loading_font( + font_directory+'/Arial.ttf', 'Arial.ttf') + self._test_fake_loading_font( + font_directory+'/Arial.ttf', 'Arial') + self._test_fake_loading_font( + font_directory+'/Single.otf', 'Single') + self._test_fake_loading_font( + font_directory+'/Duplicate.ttf', 'Duplicate') + except ImportError: class TestImageFont(PillowTestCase): diff --git a/Tests/test_imagefont_bitmap.py b/Tests/test_imagefont_bitmap.py new file mode 100644 index 000000000..27141f4b3 --- /dev/null +++ b/Tests/test_imagefont_bitmap.py @@ -0,0 +1,25 @@ +from helper import unittest, PillowTestCase +from PIL import Image, ImageFont, ImageDraw + + +class TestImageFontBitmap(PillowTestCase): + def test_similar(self): + text = 'EmbeddedBitmap' + font_outline = ImageFont.truetype(font='Tests/fonts/DejaVuSans.ttf', size=24) + font_bitmap = ImageFont.truetype(font='Tests/fonts/DejaVuSans-bitmap.ttf', size=24) + size_outline, size_bitmap = font_outline.getsize(text), font_bitmap.getsize(text) + size_final = max(size_outline[0], size_bitmap[0]), max(size_outline[1], size_bitmap[1]) + im_bitmap = Image.new('RGB', size_final, (255, 255, 255)) + im_outline = im_bitmap.copy() + draw_bitmap, draw_outline = ImageDraw.Draw(im_bitmap), ImageDraw.Draw(im_outline) + + # Metrics are different on the bitmap and ttf fonts, more so on some platforms + # and versions of freetype than others. Mac has a 1px difference, linux doesn't. + draw_bitmap.text((0, size_final[1] - size_bitmap[1]), + text, fill=(0, 0, 0), font=font_bitmap) + draw_outline.text((0, size_final[1] - size_outline[1]), + text, fill=(0, 0, 0), font=font_outline) + self.assert_image_similar(im_bitmap, im_outline, 20) + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py index 5d87c0229..4ca4e2c6c 100644 --- a/Tests/test_imagemath.py +++ b/Tests/test_imagemath.py @@ -1,3 +1,4 @@ +from __future__ import print_function from helper import unittest, PillowTestCase from PIL import Image diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index 3bc5ce85a..bbb3ae190 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -63,7 +63,7 @@ class MorphTests(PillowTestCase): 'corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'): lb = ImageMorph.LutBuilder(op_name=op) - lut = lb.build_lut(self) + lut = lb.build_lut() with open('Tests/images/%s.lut' % op, 'wb') as f: f.write(lut) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 0ffb14bfe..396f0da6c 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -5,7 +5,7 @@ from PIL import ImageOps class TestImageOps(PillowTestCase): - class Deformer: + class Deformer(object): def getmesh(self, im): x, y = im.size return [((0, 0, x, y), (0, 0, x, 0, x, y, y, 0))] diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index ba65f211b..f6eae640b 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -5,6 +5,7 @@ from PIL import ImageOps from PIL import ImageFilter im = Image.open("Tests/images/hopper.ppm") +snakes = Image.open("Tests/images/color_snakes.png") class TestImageOpsUsm(PillowTestCase): @@ -16,7 +17,7 @@ class TestImageOpsUsm(PillowTestCase): self.assertEqual(i.size, (128, 128)) # i.save("blur.bmp") - i = ImageOps.usm(im, 2.0, 125, 8) + i = ImageOps.unsharp_mask(im, 2.0, 125, 8) self.assertEqual(i.mode, "RGB") self.assertEqual(i.size, (128, 128)) # i.save("usm.bmp") @@ -33,7 +34,7 @@ class TestImageOpsUsm(PillowTestCase): self.assertEqual(i.mode, "RGB") self.assertEqual(i.size, (128, 128)) - def test_usm(self): + def test_usm_formats(self): usm = ImageOps.unsharp_mask self.assertRaises(ValueError, lambda: usm(im.convert("1"))) @@ -45,7 +46,7 @@ class TestImageOpsUsm(PillowTestCase): usm(im.convert("CMYK")) self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr"))) - def test_blur(self): + def test_blur_formats(self): blur = ImageOps.gaussian_blur self.assertRaises(ValueError, lambda: blur(im.convert("1"))) @@ -57,6 +58,32 @@ class TestImageOpsUsm(PillowTestCase): blur(im.convert("CMYK")) self.assertRaises(ValueError, lambda: blur(im.convert("YCbCr"))) + def test_usm_accuracy(self): + + src = snakes.convert('RGB') + i = src._new(ImageOps.unsharp_mask(src, 5, 1024, 0)) + # Image should not be changed because it have only 0 and 255 levels. + self.assertEqual(i.tobytes(), src.tobytes()) + + def test_blur_accuracy(self): + + i = snakes._new(ImageOps.gaussian_blur(snakes, .4)) + # These pixels surrounded with pixels with 255 intensity. + # They must be very close to 255. + for x, y, c in [(1, 0, 1), (2, 0, 1), (7, 8, 1), (8, 8, 1), (2, 9, 1), + (7, 3, 0), (8, 3, 0), (5, 8, 0), (5, 9, 0), (1, 3, 0), + (4, 3, 2), (4, 2, 2)]: + self.assertGreaterEqual(i.im.getpixel((x, y))[c], 250) + # Fuzzy match. + gp = lambda x, y: i.im.getpixel((x, y)) + self.assertTrue(236 <= gp(7, 4)[0] <= 239) + self.assertTrue(236 <= gp(7, 5)[2] <= 239) + self.assertTrue(236 <= gp(7, 6)[2] <= 239) + self.assertTrue(236 <= gp(7, 7)[1] <= 239) + self.assertTrue(236 <= gp(8, 4)[0] <= 239) + self.assertTrue(236 <= gp(8, 5)[2] <= 239) + self.assertTrue(236 <= gp(8, 6)[2] <= 239) + self.assertTrue(236 <= gp(8, 7)[1] <= 239) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index e56c61390..707ab4080 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -17,11 +17,11 @@ class TestImagePalette(PillowTestCase): palette = ImagePalette() - map = {} + test_map = {} for i in range(256): - map[palette.getcolor((i, i, i))] = i + test_map[palette.getcolor((i, i, i))] = i - self.assertEqual(len(map), 256) + self.assertEqual(len(test_map), 256) self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3))) def test_file(self): @@ -117,7 +117,7 @@ class TestImagePalette(PillowTestCase): palette = raw("RGB", list(range(256))*3) # Act / Assert - self.assertRaises(ValueError, lambda: palette.tobytes()) + self.assertRaises(ValueError, palette.tobytes) self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3))) f = self.tempfile("temp.lut") self.assertRaises(ValueError, lambda: palette.save(f)) diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index cd26e0038..7d57ed1d2 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -7,8 +7,11 @@ except: try: from PyQt4.QtGui import QImage, qRgb, qRgba except: - # Will be skipped in setUp - pass + try: + from PySide.QtGui import QImage, qRgb, qRgba + except: + # Will be skipped in setUp + pass class TestImageQt(PillowTestCase): @@ -20,7 +23,10 @@ class TestImageQt(PillowTestCase): try: from PyQt4.QtGui import QImage, qRgb, qRgba except: - self.skipTest('PyQt4 or 5 not installed') + try: + from PySide.QtGui import QImage, qRgb, qRgba + except: + self.skipTest('PyQt4 or 5 or PySide not installed') def test_rgb(self): # from https://qt-project.org/doc/qt-4.8/qcolor.html diff --git a/Tests/test_imagesequence.py b/Tests/test_imagesequence.py index 62a83cda6..1b4bb3c02 100644 --- a/Tests/test_imagesequence.py +++ b/Tests/test_imagesequence.py @@ -7,10 +7,10 @@ class TestImageSequence(PillowTestCase): def test_sanity(self): - file = self.tempfile("temp.im") + test_file = self.tempfile("temp.im") im = hopper("RGB") - im.save(file) + im.save(test_file) seq = ImageSequence.Iterator(im) @@ -24,16 +24,16 @@ class TestImageSequence(PillowTestCase): def _test_multipage_tiff(self, dbg=False): # debug had side effect of calling fp.tell. - Image.DEBUG=dbg + Image.DEBUG = dbg im = Image.open('Tests/images/multipage.tiff') for index, frame in enumerate(ImageSequence.Iterator(im)): frame.load() self.assertEqual(index, im.tell()) frame.convert('RGB') - Image.DEBUG=False + Image.DEBUG = False def test_tiff(self): - #self._test_multipage_tiff(True) + # self._test_multipage_tiff(True) self._test_multipage_tiff(False) def test_libtiff(self): @@ -43,7 +43,7 @@ class TestImageSequence(PillowTestCase): self.skipTest("tiff support not available") TiffImagePlugin.READ_LIBTIFF = True - #self._test_multipage_tiff(True) + # self._test_multipage_tiff(True) self._test_multipage_tiff(False) TiffImagePlugin.READ_LIBTIFF = False diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index e94ae2d0a..236d6e224 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -9,7 +9,6 @@ class TestImageShow(PillowTestCase): def test_sanity(self): dir(Image) dir(ImageShow) - pass if __name__ == '__main__': diff --git a/Tests/test_imagewin.py b/Tests/test_imagewin.py index 6fb58a0dc..7ceea86ee 100644 --- a/Tests/test_imagewin.py +++ b/Tests/test_imagewin.py @@ -8,7 +8,6 @@ class TestImageWin(PillowTestCase): def test_sanity(self): dir(ImageWin) - pass def test_hdc(self): # Arrange @@ -101,8 +100,8 @@ class TestImageWinDib(PillowTestCase): # Act # Make one the same as the using tobytes()/frombytes() - buffer = dib1.tobytes() - dib2.frombytes(buffer) + test_buffer = dib1.tobytes() + dib2.frombytes(test_buffer) # Assert # Confirm they're the same @@ -112,11 +111,11 @@ class TestImageWinDib(PillowTestCase): # Arrange im = hopper() dib = ImageWin.Dib(im) - buffer = dib.tobytes() + test_buffer = dib.tobytes() # Act/Assert - self.assert_warning(DeprecationWarning, lambda: dib.tostring()) - self.assert_warning(DeprecationWarning, lambda: dib.fromstring(buffer)) + self.assert_warning(DeprecationWarning, dib.tostring) + self.assert_warning(DeprecationWarning, lambda: dib.fromstring(test_buffer)) if __name__ == '__main__': diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index 102835b58..4d40b4100 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -48,6 +48,10 @@ class TestLibPack(PillowTestCase): self.assertEqual(pack("RGBA", "RGBA"), [1, 2, 3, 4]) + self.assertEqual(pack("RGBa", "RGBa"), [1, 2, 3, 4]) + self.assertEqual(pack("RGBa", "BGRa"), [3, 2, 1, 4]) + self.assertEqual(pack("RGBa", "aBGR"), [4, 3, 2, 1]) + self.assertEqual(pack("CMYK", "CMYK"), [1, 2, 3, 4]) self.assertEqual(pack("YCbCr", "YCbCr"), [1, 2, 3]) @@ -125,6 +129,11 @@ class TestLibPack(PillowTestCase): self.assertEqual(unpack("RGBA", "BGRA;15", 2), (0, 131, 8, 0)) self.assertEqual(unpack("RGBA", "RGBA;4B", 2), (17, 0, 34, 0)) + self.assertEqual(unpack("RGBa", "RGBa", 4), (1, 2, 3, 4)) + self.assertEqual(unpack("RGBa", "BGRa", 4), (3, 2, 1, 4)) + self.assertEqual(unpack("RGBa", "aRGB", 4), (2, 3, 4, 1)) + self.assertEqual(unpack("RGBa", "aBGR", 4), (4, 3, 2, 1)) + self.assertEqual(unpack("RGBX", "RGBX", 4), (1, 2, 3, 4)) # 4->255? self.assertEqual(unpack("RGBX", "BGRX", 4), (3, 2, 1, 255)) self.assertEqual(unpack("RGBX", "XRGB", 4), (2, 3, 4, 255)) diff --git a/Tests/test_mode_i16.py b/Tests/test_mode_i16.py index 20e39b235..0827e218b 100644 --- a/Tests/test_mode_i16.py +++ b/Tests/test_mode_i16.py @@ -64,15 +64,15 @@ class TestModeI16(PillowTestCase): self.assertEqual(imIn.getpixel((0, 0)), 2) if mode == "L": - max = 255 + maximum = 255 else: - max = 32767 + maximum = 32767 imIn = Image.new(mode, (1, 1), 256) - self.assertEqual(imIn.getpixel((0, 0)), min(256, max)) + self.assertEqual(imIn.getpixel((0, 0)), min(256, maximum)) imIn.putpixel((0, 0), 512) - self.assertEqual(imIn.getpixel((0, 0)), min(512, max)) + self.assertEqual(imIn.getpixel((0, 0)), min(512, maximum)) basic("L") diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index b00b8b36b..19b9a2014 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -1,3 +1,4 @@ +from __future__ import print_function from helper import unittest, PillowTestCase, hopper from PIL import Image @@ -121,6 +122,16 @@ class TestNumpy(PillowTestCase): im.point(lut) + def test_putdata(self): + # shouldn't segfault + # see https://github.com/python-pillow/Pillow/issues/1008 + + im = Image.new('F', (150, 100)) + arr = numpy.zeros((15000,), numpy.float32) + im.putdata(arr) + + self.assertEqual(len(im.getdata()), len(arr)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_pickle.py b/Tests/test_pickle.py index 59dfd5948..52d64acee 100644 --- a/Tests/test_pickle.py +++ b/Tests/test_pickle.py @@ -5,10 +5,12 @@ from PIL import Image class TestPickle(PillowTestCase): - def helper_pickle_file(self, pickle, protocol=0): + def helper_pickle_file(self, pickle, protocol=0, mode=None): # Arrange im = Image.open('Tests/images/hopper.jpg') filename = self.tempfile('temp.pkl') + if mode: + im = im.convert(mode) # Act with open(filename, 'wb') as f: @@ -19,9 +21,11 @@ class TestPickle(PillowTestCase): # Assert self.assertEqual(im, loaded_im) - def helper_pickle_string( - self, pickle, protocol=0, file='Tests/images/hopper.jpg'): + def helper_pickle_string(self, pickle, protocol=0, + file='Tests/images/hopper.jpg', mode=None): im = Image.open(file) + if mode: + im = im.convert(mode) # Act dumped_string = pickle.dumps(im, protocol) @@ -56,7 +60,7 @@ class TestPickle(PillowTestCase): import pickle # Act / Assert - for file in [ + for test_file in [ "Tests/images/test-card.png", "Tests/images/zero_bb.png", "Tests/images/zero_bb_scale2.png", @@ -65,8 +69,28 @@ class TestPickle(PillowTestCase): "Tests/images/p_trns_single.png", "Tests/images/pil123p.png" ]: - self.helper_pickle_string(pickle, file=file) + self.helper_pickle_string(pickle, file=test_file) + def test_pickle_l_mode(self): + # Arrange + import pickle + + # Act / Assert + for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1): + self.helper_pickle_string(pickle, protocol, mode="L") + self.helper_pickle_file(pickle, protocol, mode="L") + + def test_cpickle_l_mode(self): + # Arrange + try: + import cPickle + except ImportError: + return + + # Act / Assert + for protocol in range(0, cPickle.HIGHEST_PROTOCOL + 1): + self.helper_pickle_string(cPickle, protocol, mode="L") + self.helper_pickle_file(cPickle, protocol, mode="L") if __name__ == '__main__': unittest.main() diff --git a/Tests/test_psdraw.py b/Tests/test_psdraw.py new file mode 100644 index 000000000..9606a4392 --- /dev/null +++ b/Tests/test_psdraw.py @@ -0,0 +1,50 @@ +from helper import unittest, PillowTestCase + + +class TestPsDraw(PillowTestCase): + + def test_draw_postscript(self): + + # Based on Pillow tutorial, but there is no textsize: + # http://pillow.readthedocs.org/en/latest/handbook/tutorial.html + + # Arrange + from PIL import Image + from PIL import PSDraw + tempfile = self.tempfile('temp.ps') + fp = open(tempfile, "wb") + + im = Image.open("Tests/images/hopper.ppm") + title = "hopper" + box = (1*72, 2*72, 7*72, 10*72) # in points + + # Act + ps = PSDraw.PSDraw(fp) + ps.begin_document(title) + + # draw diagonal lines in a cross + ps.line((1*72, 2*72), (7*72, 10*72)) + ps.line((7*72, 2*72), (1*72, 10*72)) + + # draw the image (75 dpi) + ps.image(box, im, 75) + ps.rectangle(box) + + # draw title + ps.setfont("Courier", 36) + ps.text((3*72, 4*72), title) + + ps.end_document() + fp.close() + + # Assert + # Check non-zero file was created + import os + self.assertTrue(os.path.isfile(tempfile)) + self.assertGreater(os.path.getsize(tempfile), 0) + + +if __name__ == '__main__': + unittest.main() + +# End of file diff --git a/Tests/test_pyroma.py b/Tests/test_pyroma.py index 59aa3810e..0875cc972 100644 --- a/Tests/test_pyroma.py +++ b/Tests/test_pyroma.py @@ -14,6 +14,7 @@ class TestPyroma(PillowTestCase): def setUp(self): try: import pyroma + assert pyroma # Ignore warning except ImportError: self.skipTest("ImportError") @@ -26,9 +27,10 @@ class TestPyroma(PillowTestCase): # Assert if 'rc' in PILLOW_VERSION: - #Pyroma needs to chill about RC versions and not kill all our tests. - self.assertEqual(rating, (9, - ['The packages version number does not comply with PEP-386.'])) + # Pyroma needs to chill about RC versions + # and not kill all our tests. + self.assertEqual(rating, (9, [ + 'The packages version number does not comply with PEP-386.'])) else: # Should have a perfect score diff --git a/Tests/test_scipy.py b/Tests/test_scipy.py index e3d1d97cd..60f1a1b1e 100644 --- a/Tests/test_scipy.py +++ b/Tests/test_scipy.py @@ -8,35 +8,36 @@ try: HAS_SCIPY = True except: HAS_SCIPY = False - + class Test_scipy_resize(PillowTestCase): - """ Tests for scipy regression in 2.6.0 """ + """ Tests for scipy regression in 2.6.0 + + Tests from https://github.com/scipy/scipy/blob/master/scipy/misc/pilutil.py + """ def setUp(self): if not HAS_SCIPY: self.skipTest("Scipy Required") def test_imresize(self): - im = np.random.random((10,20)) + im = np.random.random((10, 20)) for T in np.sctypes['float'] + [float]: # 1.1 rounds to below 1.1 for float16, 1.101 works - im1 = misc.imresize(im,T(1.101)) - self.assertEqual(im1.shape,(11,22)) + im1 = misc.imresize(im, T(1.101)) + self.assertEqual(im1.shape, (11, 22)) def test_imresize4(self): - im = np.array([[1,2], - [3,4]]) - res = np.array([[ 1. , 1. , 1.5, 2. ], - [ 1. , 1. , 1.5, 2. ], - [ 2. , 2. , 2.5, 3. ], - [ 3. , 3. , 3.5, 4. ]], dtype=np.float32) + im = np.array([[1, 2], + [3, 4]]) + res = np.array([[1. , 1.25, 1.75, 2. ], + [1.5, 1.75, 2.25, 2.5], + [2.5, 2.75, 3.25, 3.5], + [3. , 3.25, 3.75, 4. ]], dtype=np.float32) # Check that resizing by target size, float and int are the same - im2 = misc.imresize(im, (4,4), mode='F') # output size + im2 = misc.imresize(im, (4, 4), mode='F') # output size im3 = misc.imresize(im, 2., mode='F') # fraction im4 = misc.imresize(im, 200, mode='F') # percentage assert_equal(im2, res) assert_equal(im3, res) assert_equal(im4, res) - - diff --git a/Tests/threaded_save.py b/Tests/threaded_save.py index 376abe7bd..ba8b17dbc 100644 --- a/Tests/threaded_save.py +++ b/Tests/threaded_save.py @@ -1,3 +1,4 @@ +from __future__ import print_function from PIL import Image import io @@ -6,10 +7,7 @@ import sys import threading import time -try: - format = sys.argv[1] -except: - format = "PNG" +test_format = sys.argv[1] if len(sys.argv) > 1 else "PNG" im = Image.open("Tests/images/hopper.ppm") im.load() @@ -28,7 +26,7 @@ class Worker(threading.Thread): sys.stdout.write("x") break f = io.BytesIO() - im.save(f, format, optimize=1) + im.save(f, test_format, optimize=1) data = f.getvalue() result.append(len(data)) im = Image.open(io.BytesIO(data)) diff --git a/Tests/versions.py b/Tests/versions.py index e367ae46a..89be1d7c8 100644 --- a/Tests/versions.py +++ b/Tests/versions.py @@ -1,3 +1,4 @@ +from __future__ import print_function from PIL import Image diff --git a/_imaging.c b/_imaging.c index 1759d4c8d..09345c0dd 100644 --- a/_imaging.c +++ b/_imaging.c @@ -71,7 +71,7 @@ * See the README file for information on usage and redistribution. */ -#define PILLOW_VERSION "2.6.0" +#define PILLOW_VERSION "2.9.0.dev0" #include "Python.h" @@ -421,6 +421,7 @@ getlist(PyObject* arg, int* length, const char* wrong_length, int type) *length = n; PyErr_Clear(); + Py_DECREF(seq); return list; } @@ -863,7 +864,8 @@ _gaussian_blur(ImagingObject* self, PyObject* args) Imaging imOut; float radius = 0; - if (!PyArg_ParseTuple(args, "f", &radius)) + int passes = 3; + if (!PyArg_ParseTuple(args, "f|i", &radius, &passes)) return NULL; imIn = self->image; @@ -871,7 +873,7 @@ _gaussian_blur(ImagingObject* self, PyObject* args) if (!imOut) return NULL; - if (!ImagingGaussianBlur(imIn, imOut, radius)) + if (!ImagingGaussianBlur(imOut, imIn, radius, passes)) return NULL; return PyImagingNew(imOut); @@ -1220,7 +1222,7 @@ _putdata(ImagingObject* self, PyObject* args) Py_ssize_t n, i, x, y; PyObject* data; - PyObject* seq; + PyObject* seq = NULL; PyObject* op; double scale = 1.0; double offset = 0.0; @@ -1269,7 +1271,7 @@ _putdata(ImagingObject* self, PyObject* args) if (scale == 1.0 && offset == 0.0) { /* Clipped data */ for (i = x = y = 0; i < n; i++) { - op = PySequence_Fast_GET_ITEM(data, i); + op = PySequence_Fast_GET_ITEM(seq, i); image->image8[y][x] = (UINT8) CLIP(PyInt_AsLong(op)); if (++x >= (int) image->xsize){ x = 0, y++; @@ -1279,7 +1281,7 @@ _putdata(ImagingObject* self, PyObject* args) } else { /* Scaled and clipped data */ for (i = x = y = 0; i < n; i++) { - PyObject *op = PySequence_Fast_GET_ITEM(data, i); + PyObject *op = PySequence_Fast_GET_ITEM(seq, i); image->image8[y][x] = CLIP( (int) (PyFloat_AsDouble(op) * scale + offset)); if (++x >= (int) image->xsize){ @@ -1299,7 +1301,7 @@ _putdata(ImagingObject* self, PyObject* args) switch (image->type) { case IMAGING_TYPE_INT32: for (i = x = y = 0; i < n; i++) { - op = PySequence_Fast_GET_ITEM(data, i); + op = PySequence_Fast_GET_ITEM(seq, i); IMAGING_PIXEL_INT32(image, x, y) = (INT32) (PyFloat_AsDouble(op) * scale + offset); if (++x >= (int) image->xsize){ @@ -1310,7 +1312,7 @@ _putdata(ImagingObject* self, PyObject* args) break; case IMAGING_TYPE_FLOAT32: for (i = x = y = 0; i < n; i++) { - op = PySequence_Fast_GET_ITEM(data, i); + op = PySequence_Fast_GET_ITEM(seq, i); IMAGING_PIXEL_FLOAT32(image, x, y) = (FLOAT32) (PyFloat_AsDouble(op) * scale + offset); if (++x >= (int) image->xsize){ @@ -1326,8 +1328,9 @@ _putdata(ImagingObject* self, PyObject* args) INT32 inkint; } u; - op = PySequence_Fast_GET_ITEM(data, i); + op = PySequence_Fast_GET_ITEM(seq, i); if (!op || !getink(op, image, u.ink)) { + Py_DECREF(seq); return NULL; } /* FIXME: what about scale and offset? */ @@ -1341,6 +1344,8 @@ _putdata(ImagingObject* self, PyObject* args) } } + Py_XDECREF(seq); + Py_INCREF(Py_None); return Py_None; } @@ -1513,9 +1518,26 @@ _resize(ImagingObject* self, PyObject* args) imIn = self->image; - imOut = ImagingNew(imIn->mode, xsize, ysize); - if (imOut) - (void) ImagingResize(imOut, imIn, filter); + if (imIn->xsize == xsize && imIn->ysize == ysize) { + imOut = ImagingCopy(imIn); + } + else if ( ! filter) { + double a[6]; + + memset(a, 0, sizeof a); + a[1] = (double) imIn->xsize / xsize; + a[5] = (double) imIn->ysize / ysize; + + imOut = ImagingNew(imIn->mode, xsize, ysize); + + imOut = ImagingTransformAffine( + imOut, imIn, + 0, 0, xsize, ysize, + a, filter, 1); + } + else { + imOut = ImagingResample(imIn, xsize, ysize, filter); + } return PyImagingNew(imOut); } @@ -1609,51 +1631,6 @@ im_setmode(ImagingObject* self, PyObject* args) return Py_None; } -static PyObject* -_stretch(ImagingObject* self, PyObject* args) -{ - Imaging imIn; - Imaging imTemp; - Imaging imOut; - - int xsize, ysize; - int filter = IMAGING_TRANSFORM_NEAREST; - if (!PyArg_ParseTuple(args, "(ii)|i", &xsize, &ysize, &filter)) - return NULL; - - imIn = self->image; - - /* two-pass resize: minimize size of intermediate image */ - if ((Py_ssize_t) imIn->xsize * ysize < (Py_ssize_t) xsize * imIn->ysize) - imTemp = ImagingNew(imIn->mode, imIn->xsize, ysize); - else - imTemp = ImagingNew(imIn->mode, xsize, imIn->ysize); - if (!imTemp) - return NULL; - - /* first pass */ - if (!ImagingStretch(imTemp, imIn, filter)) { - ImagingDelete(imTemp); - return NULL; - } - - imOut = ImagingNew(imIn->mode, xsize, ysize); - if (!imOut) { - ImagingDelete(imTemp); - return NULL; - } - - /* second pass */ - if (!ImagingStretch(imOut, imTemp, filter)) { - ImagingDelete(imOut); - ImagingDelete(imTemp); - return NULL; - } - - ImagingDelete(imTemp); - - return PyImagingNew(imOut); -} static PyObject* _transform2(ImagingObject* self, PyObject* args) @@ -1750,6 +1727,7 @@ _transpose(ImagingObject* self, PyObject* args) break; case 2: /* rotate 90 */ case 4: /* rotate 270 */ + case 5: /* transpose */ imOut = ImagingNew(imIn->mode, imIn->ysize, imIn->xsize); break; default: @@ -1774,6 +1752,9 @@ _transpose(ImagingObject* self, PyObject* args) case 4: (void) ImagingRotate270(imOut, imIn); break; + case 5: + (void) ImagingTranspose(imOut, imIn); + break; } return PyImagingNew(imOut); @@ -1791,18 +1772,39 @@ _unsharp_mask(ImagingObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "fii", &radius, &percent, &threshold)) return NULL; + imIn = self->image; + imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize); + if (!imOut) + return NULL; + + if (!ImagingUnsharpMask(imOut, imIn, radius, percent, threshold)) + return NULL; + + return PyImagingNew(imOut); +} +#endif + +static PyObject* +_box_blur(ImagingObject* self, PyObject* args) +{ + Imaging imIn; + Imaging imOut; + + float radius; + int n = 1; + if (!PyArg_ParseTuple(args, "f|i", &radius, &n)) + return NULL; imIn = self->image; imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize); if (!imOut) return NULL; - if (!ImagingUnsharpMask(imIn, imOut, radius, percent, threshold)) + if (!ImagingBoxBlur(imOut, imIn, radius, n)) return NULL; return PyImagingNew(imOut); } -#endif /* -------------------------------------------------------------------- */ @@ -3031,8 +3033,10 @@ static struct PyMethodDef methods[] = { {"rankfilter", (PyCFunction)_rankfilter, 1}, #endif {"resize", (PyCFunction)_resize, 1}, + // There were two methods for image resize before. + // Starting from Pillow 2.7.0 stretch is depreciated. + {"stretch", (PyCFunction)_resize, 1}, {"rotate", (PyCFunction)_rotate, 1}, - {"stretch", (PyCFunction)_stretch, 1}, {"transpose", (PyCFunction)_transpose, 1}, {"transform2", (PyCFunction)_transform2, 1}, @@ -3078,6 +3082,8 @@ static struct PyMethodDef methods[] = { {"unsharp_mask", (PyCFunction)_unsharp_mask, 1}, #endif + {"box_blur", (PyCFunction)_box_blur, 1}, + #ifdef WITH_EFFECTS /* Special effects */ {"effect_spread", (PyCFunction)_effect_spread, 1}, diff --git a/_imagingcms.c b/_imagingcms.c index 3b822006a..cda7c5f1f 100644 --- a/_imagingcms.c +++ b/_imagingcms.c @@ -278,7 +278,7 @@ findLCMStype(char* PILmode) return TYPE_YCbCr_8; } else if (strcmp(PILmode, "LAB") == 0) { - // LabX equvalent like ALab, but not reversed -- no #define in lcms2 + // LabX equivalent like ALab, but not reversed -- no #define in lcms2 return (COLORSPACE_SH(PT_LabV2)|CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)); } diff --git a/_imagingft.c b/_imagingft.c index eb6313704..d8f6d6338 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -243,7 +243,11 @@ font_getsize(FontObject* self, PyObject* args) &delta); x += delta.x; } - error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT); + + /* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960 + * Yifu Yu, 2014-10-15 + */ + error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_NO_BITMAP); if (error) return geterror(error); if (i == 0) @@ -316,7 +320,8 @@ font_getabc(FontObject* self, PyObject* args) int index, error; face = self->face; index = FT_Get_Char_Index(face, ch); - error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT); + /* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960 */ + error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_NO_BITMAP); if (error) return geterror(error); a = face->glyph->metrics.horiBearingX / 64.0; @@ -363,8 +368,8 @@ font_render(FontObject* self, PyObject* args) } im = (Imaging) id; - - load_flags = FT_LOAD_RENDER; + /* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960 */ + load_flags = FT_LOAD_RENDER|FT_LOAD_NO_BITMAP; if (mask) load_flags |= FT_LOAD_TARGET_MONO; @@ -567,7 +572,7 @@ setup_module(PyObject* m) { PyType_Ready(&Font_Type); if (FT_Init_FreeType(&library)) - return 0; /* leave it uninitalized */ + return 0; /* leave it uninitialized */ FT_Library_Version(library, &major, &minor, &patch); diff --git a/_webp.c b/_webp.c index c201813d7..a8c6d40af 100644 --- a/_webp.c +++ b/_webp.c @@ -136,9 +136,9 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args) PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args) { PyBytesObject *webp_string; - uint8_t *webp; + const uint8_t *webp; Py_ssize_t size; - PyObject *ret, *bytes, *pymode, *icc_profile = Py_None, *exif = Py_None; + PyObject *ret = Py_None, *bytes = NULL, *pymode = NULL, *icc_profile = NULL, *exif = NULL; WebPDecoderConfig config; VP8StatusCode vp8_status_code = VP8_STATUS_OK; char* mode = "RGB"; @@ -173,31 +173,34 @@ PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args) WebPData exif_data = {0}; WebPMux* mux = WebPMuxCreate(&data, copy_data); - WebPMuxGetFrame(mux, 1, &image); - webp = (uint8_t*)image.bitstream.bytes; + if (NULL == mux) + goto end; + + if (WEBP_MUX_OK != WebPMuxGetFrame(mux, 1, &image)) + { + WebPMuxDelete(mux); + goto end; + } + + webp = image.bitstream.bytes; size = image.bitstream.size; vp8_status_code = WebPDecode(webp, size, &config); - WebPMuxGetChunk(mux, "ICCP", &icc_profile_data); - if (icc_profile_data.size > 0) { + if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "ICCP", &icc_profile_data)) icc_profile = PyBytes_FromStringAndSize((const char*)icc_profile_data.bytes, icc_profile_data.size); - } - WebPMuxGetChunk(mux, "EXIF", &exif_data); - if (exif_data.size > 0) { + if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "EXIF", &exif_data)) exif = PyBytes_FromStringAndSize((const char*)exif_data.bytes, exif_data.size); - } + WebPDataClear(&image.bitstream); WebPMuxDelete(mux); } #endif } - if (vp8_status_code != VP8_STATUS_OK) { - WebPFreeDecBuffer(&config.output); - Py_RETURN_NONE; - } + if (vp8_status_code != VP8_STATUS_OK) + goto end; if (config.output.colorspace < MODE_YUV) { bytes = PyBytes_FromStringAndSize((char *)config.output.u.RGBA.rgba, @@ -215,8 +218,21 @@ PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args) pymode = PyString_FromString(mode); #endif ret = Py_BuildValue("SiiSSS", bytes, config.output.width, - config.output.height, pymode, icc_profile, exif); + config.output.height, pymode, + NULL == icc_profile ? Py_None : icc_profile, + NULL == exif ? Py_None : exif); + +end: WebPFreeDecBuffer(&config.output); + + Py_XDECREF(bytes); + Py_XDECREF(pymode); + Py_XDECREF(icc_profile); + Py_XDECREF(exif); + + if (Py_None == ret) + Py_RETURN_NONE; + return ret; } diff --git a/build_children.sh b/build_children.sh new file mode 100755 index 000000000..c4ed4ebfa --- /dev/null +++ b/build_children.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Get last child project build number from branch named "latest" +BUILD_NUM=$(curl -s 'https://api.travis-ci.org/repos/python-pillow/pillow-wheels/branches/latest' | grep -o '^{"branch":{"id":[0-9]*,' | grep -o '[0-9]' | tr -d '\n') + +# Restart last child project build +curl -X POST https://api.travis-ci.org/builds/$BUILD_NUM/restart --header "Authorization: token "$AUTH_TOKEN diff --git a/decode.c b/decode.c index c56f42592..6299d9124 100644 --- a/decode.c +++ b/decode.c @@ -116,12 +116,17 @@ _decode(ImagingDecoderObject* decoder, PyObject* args) { UINT8* buffer; int bufsize, status; + ImagingSectionCookie cookie; if (!PyArg_ParseTuple(args, PY_ARG_BYTES_LENGTH, &buffer, &bufsize)) return NULL; + ImagingSectionEnter(&cookie); + status = decoder->decode(decoder->im, &decoder->state, buffer, bufsize); + ImagingSectionLeave(&cookie); + return Py_BuildValue("ii", status, decoder->state.errcode); } diff --git a/depends/install_webp.sh b/depends/install_webp.sh index 7810159cc..97edf2bcf 100755 --- a/depends/install_webp.sh +++ b/depends/install_webp.sh @@ -1,14 +1,14 @@ #!/bin/bash # install webp -if [ ! -f libwebp-0.4.1.tar.gz ]; then - wget 'http://downloads.webmproject.org/releases/webp/libwebp-0.4.1.tar.gz' +if [ ! -f libwebp-0.4.3.tar.gz ]; then + wget 'http://downloads.webmproject.org/releases/webp/libwebp-0.4.3.tar.gz' fi -rm -r libwebp-0.4.1 -tar -xvzf libwebp-0.4.1.tar.gz +rm -r libwebp-0.4.3 +tar -xvzf libwebp-0.4.3.tar.gz -pushd libwebp-0.4.1 +pushd libwebp-0.4.3 ./configure --prefix=/usr --enable-libwebpmux --enable-libwebpdemux && make -j4 && sudo make -j4 install diff --git a/docs/PIL.rst b/docs/PIL.rst index 8bf89c685..53a61872b 100644 --- a/docs/PIL.rst +++ b/docs/PIL.rst @@ -113,6 +113,29 @@ can be found here. :undoc-members: :show-inheritance: +:class:`PngImagePlugin.iTXt` Class +---------------------------------- + +.. autoclass:: PIL.PngImagePlugin.iTXt + :members: + :undoc-members: + :show-inheritance: + + .. method:: __new__(cls, text, lang, tkey) + + :param value: value for this key + :param lang: language code + :param tkey: UTF-8 version of the key name + +:class:`PngImagePlugin.PngInfo` Class +------------------------------------- + +.. autoclass:: PIL.PngImagePlugin.PngInfo + :members: + :undoc-members: + :show-inheritance: + + :mod:`TarIO` Module ------------------- diff --git a/docs/_build/.gitignore b/docs/_build/.gitignore deleted file mode 100644 index b1f9a2ade..000000000 --- a/docs/_build/.gitignore +++ /dev/null @@ -1 +0,0 @@ -# Empty file, to make the directory available in the repository diff --git a/docs/about.rst b/docs/about.rst index 919b2918c..67bf20481 100644 --- a/docs/about.rst +++ b/docs/about.rst @@ -4,7 +4,7 @@ About Pillow Goals ----- -The fork authors' goal is to foster active development of PIL through: +The fork author's goal is to foster and support active development of PIL through: - Continuous integration testing via `Travis CI`_ - Publicized development activity on `GitHub`_ @@ -17,7 +17,7 @@ The fork authors' goal is to foster active development of PIL through: License ------- -like PIL itself, Pillow is licensed under the MIT-like `PIL Software License `:: +Like PIL, Pillow is licensed under the MIT-like open source `PIL Software License `_:: Software License @@ -35,10 +35,7 @@ like PIL itself, Pillow is licensed under the MIT-like `PIL Software License v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] -html_theme_options = {} +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +#html_extra_path = [] -html_sidebars = { - '**': ['localtoc.html', 'sourcelink.html', 'sidebarhelp.html', - 'searchbox.html'], - 'index': ['globaltoc.html', 'sidebarhelp.html', 'searchbox.html'], -} +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' +#html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# Now only 'ja' uses this config value +#html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +#html_search_scorer = 'scorer.js' # Output file base name for HTML help builder. -htmlhelp_basename = 'Pillowdoc' +htmlhelp_basename = 'PillowPILForkdoc' +# -- Options for LaTeX output --------------------------------------------- -### LaTeX output (RtD PDF output as well) ### +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', -latex_elements = {} +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', +# Additional stuff for the LaTeX preamble. +#'preamble': '', + +# Latex figure (float) alignment +#'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). latex_documents = [ - ('index', 'Pillow.tex', u'Pillow (PIL fork) Documentation', u'Author', - 'manual'), + (master_doc, 'PillowPILFork.tex', u'Pillow (PIL Fork) Documentation', + u'Alex Clark', 'manual'), ] +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None -# skip_api_docs setting will skip PIL.rst if True. Used for working on the -# guides; makes livereload basically instantaneous. -def setup(app): - app.add_config_value('skip_api_docs', False, True) +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False -skip_api_docs = False +# If true, show page references after internal links. +#latex_show_pagerefs = False -if skip_api_docs: - exclude_patterns += ['PIL.rst'] +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, 'pillowpilfork', u'Pillow (PIL Fork) Documentation', + [author], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'PillowPILFork', u'Pillow (PIL Fork) Documentation', + author, 'PillowPILFork', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +#texinfo_no_detailmenu = False diff --git a/docs/handbook/appendices.rst b/docs/handbook/appendices.rst index f5482ffcd..e6c415cc6 100644 --- a/docs/handbook/appendices.rst +++ b/docs/handbook/appendices.rst @@ -1,6 +1,8 @@ Appendices ========== +.. note:: Contributors please include appendices as needed or appropriate with your bug fixes, feature additions and tests. + .. toctree:: :maxdepth: 2 diff --git a/docs/handbook/concepts.rst b/docs/handbook/concepts.rst index b5e5e44c1..fb97fe098 100644 --- a/docs/handbook/concepts.rst +++ b/docs/handbook/concepts.rst @@ -14,8 +14,10 @@ same dimensions and depth. To get the number and names of bands in an image, use the :py:meth:`~PIL.Image.Image.getbands` method. -Mode ----- +.. _concept-modes: + +Modes +----- The :term:`mode` of an image defines the type and depth of a pixel in the image. The current release supports the following standard modes: @@ -87,25 +89,21 @@ pixel, the Python Imaging Library provides four different resampling *filters*. Pick the nearest pixel from the input image. Ignore all other input pixels. ``BILINEAR`` - Use linear interpolation over a 2x2 environment in the input image. Note - that in the current version of PIL, this filter uses a fixed input - environment when downsampling. + For resize calculate the output pixel value using linear interpolation + on all pixels that may contribute to the output value. + For other transformations linear interpolation over a 2x2 environment + in the input image is used. ``BICUBIC`` - Use cubic interpolation over a 4x4 environment in the input image. Note - that in the current version of PIL, this filter uses a fixed input - environment when downsampling. + For resize calculate the output pixel value using cubic interpolation + on all pixels that may contribute to the output value. + For other transformations cubic interpolation over a 4x4 environment + in the input image is used. -``ANTIALIAS`` - Calculate the output pixel value using a high-quality resampling filter (a +``LANCZOS`` + Calculate the output pixel value using a high-quality Lanczos filter (a truncated sinc) on all pixels that may contribute to the output value. In the current version of PIL, this filter can only be used with the resize and thumbnail methods. .. versionadded:: 1.1.3 - -Note that in the current version of PIL, the ``ANTIALIAS`` filter is the only -filter that behaves properly when downsampling (that is, when converting a -large image to a small one). The ``BILINEAR`` and ``BICUBIC`` filters use a -fixed input environment, and are best used for scale-preserving geometric -transforms and upsamping. diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 50eecd9da..a85a917b8 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -125,7 +125,7 @@ The :py:meth:`~PIL.Image.Image.open` method may set the following not present. **jfif_version** - A tuple representing the jfif version, (major version, minor version). + A tuple representing the jfif version, (major version, minor version). **jfif_density** A tuple representing the pixel density of the image, in units specified @@ -139,8 +139,8 @@ The :py:meth:`~PIL.Image.Image.open` method may set the following * 2 - Pixels per Centimeter **dpi** - A tuple representing the reported pixel density in pixels per inch, if - the file is a jfif file and the units are in inches. + A tuple representing the reported pixel density in pixels per inch, if + the file is a jfif file and the units are in inches. **adobe** Adobe application marker found. If the file is not an Adobe JPEG file, this @@ -153,10 +153,10 @@ The :py:meth:`~PIL.Image.Image.open` method may set the following Indicates that this is a progressive JPEG file. **icc-profile** - The ICC color profile for the image. + The ICC color profile for the image. **exif** - Raw EXIF data from the image. + Raw EXIF data from the image. The :py:meth:`~PIL.Image.Image.save` method supports the following options: @@ -178,7 +178,7 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: **dpi** A tuple of integers representing the pixel density, ``(x,y)``. -**icc-profile** +**icc-profile** If present, the image is stored with the provided ICC profile. If this parameter is not provided, the image will be saved with no profile attached. To preserve the existing profile:: @@ -186,11 +186,11 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: im.save(filename, 'jpeg', icc_profile=im.info.get('icc_profile')) **exif** - If present, the image will be stored with the provided raw EXIF data. + If present, the image will be stored with the provided raw EXIF data. **subsampling** - If present, sets the subsampling for the encoder. - + If present, sets the subsampling for the encoder. + * ``keep``: Only valid for JPEG files, will retain the original image setting. * ``4:4:4``, ``4:2:2``, ``4:1:1``: Specific sampling values * ``-1``: equivalent to ``keep`` @@ -206,7 +206,7 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: * a string, naming a preset, e.g. ``keep``, ``web_low``, or ``web_high`` * a list, tuple, or dictionary (with integer keys = range(len(keys))) of lists of 64 integers. There must be - between 2 and 4 tables. + between 2 and 4 tables. .. versionadded:: 2.5.0 @@ -332,6 +332,14 @@ The :py:meth:`~PIL.Image.Image.open` method sets the following Transparency color index. This key is omitted if the image is not a transparent palette image. +``Open`` also sets ``Image.text`` to a list of the values of the +``tEXt``, ``zTXt``, and ``iTXt`` chunks of the PNG image. Individual +compressed chunks are limited to a decompressed size of +``PngImagePlugin.MAX_TEXT_CHUNK``, by default 1MB, to prevent +decompression bombs. Additionally, the total size of all of the text +chunks is limited to ``PngImagePlugin.MAX_TEXT_MEMORY``, defaulting to +64MB. + The :py:meth:`~PIL.Image.Image.save` method supports the following options: **optimize** @@ -339,10 +347,22 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: possible. This includes extra processing in order to find optimal encoder settings. -**transparency** +**transparency** For ``P``, ``L``, and ``RGB`` images, this option controls what color image to mark as transparent. +**dpi** + A tuple of two numbers corresponding to the desired dpi in each direction. + +**pnginfo** + A :py:class:`PIL.PngImagePlugin.PngInfo` instance containing text tags. + +**compress_level** + ZLIB compression level, a number between 0 and 9: 1 gives best speed, + 9 gives best compression, 0 gives no compression at all. Default is 6. + When ``optimize`` option is True ``compress_level`` has no effect + (it is set to 9 regardless of a value passed). + **bits (experimental)** For ``P`` images, this option controls how many bits to store. If omitted, the PNG writer uses 8 bits (256 colors). @@ -436,7 +456,7 @@ Saving Tiff Images The :py:meth:`~PIL.Image.Image.save` method can take the following keyword arguments: -**tiffinfo** +**tiffinfo** A :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory` object or dict object containing tiff tags and values. The TIFF field type is autodetected for Numeric and string values, any other types @@ -445,7 +465,7 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum :py:attr:`~PIL.TiffImagePlugin.ImageFileDirectory.tagtype` with the appropriate numerical value from ``TiffTags.TYPES``. - + .. versionadded:: 2.3.0 **compression** @@ -457,25 +477,25 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum These arguments to set the tiff header fields are an alternative to using the general tags available through tiffinfo. -**description** +**description** **software** -**date time** +**date_time** **artist** **copyright** Strings -**resolution unit** - A string of "inch", "centimeter" or "cm" +**resolution_unit** + A string of "inch", "centimeter" or "cm" **resolution** -**x resolution** +**x_resolution** -**y resolution** +**y_resolution** **dpi** Either a Float, Integer, or 2 tuple of (numerator, @@ -589,6 +609,14 @@ ICO ICO is used to store icons on Windows. The largest available icon is read. +The :py:meth:`~PIL.Image.Image.save` method supports the following options: + +**sizes** + A list of sizes including in this ico file; these are a 2-tuple, + ``(width, height)``; Default to ``[(16, 16), (24, 24), (32, 32), (48, 48), + (64, 64), (128, 128), (255, 255)]``. Any size is bigger then the original + size or 255 will be ignored. + ICNS ^^^^ diff --git a/docs/handbook/tutorial.rst b/docs/handbook/tutorial.rst index c6d2bf9e4..365c8e7a8 100644 --- a/docs/handbook/tutorial.rst +++ b/docs/handbook/tutorial.rst @@ -445,10 +445,9 @@ Drawing Postscript ps.image(box, im, 75) ps.rectangle(box) - # draw centered title + # draw title ps.setfont("HelveticaNarrow-Bold", 36) - w, h, b = ps.textsize(title) - ps.text((4*72-w/2, 1*72-h), title) + ps.text((3*72, 4*72), title) ps.end_document() diff --git a/docs/handbook/writing-your-own-file-decoder.rst b/docs/handbook/writing-your-own-file-decoder.rst index 10833a53e..0af4007be 100644 --- a/docs/handbook/writing-your-own-file-decoder.rst +++ b/docs/handbook/writing-your-own-file-decoder.rst @@ -1,4 +1,6 @@ -Writing your own file decoder +.. _file-decoders: + +Writing Your Own File Decoder ============================= The Python Imaging Library uses a plug-in model which allows you to @@ -7,7 +9,7 @@ library itself. Such plug-ins usually have names like :file:`XxxImagePlugin.py`, where ``Xxx`` is a unique format name (usually an abbreviation). -.. warning:: Pillow >= 2.1.0 no longer automatically imports any file in the Python path with a name ending in :file:`ImagePlugin.py`. You will need to import your decoder manually. +.. warning:: Pillow >= 2.1.0 no longer automatically imports any file in the Python path with a name ending in :file:`ImagePlugin.py`. You will need to import your decoder manually. A decoder plug-in should contain a decoder class, based on the :py:class:`PIL.ImageFile.ImageFile` base class. This class should provide an @@ -68,7 +70,7 @@ true color. ] Image.register_open("SPAM", SpamImageFile) - + Image.register_extension("SPAM", ".spam") Image.register_extension("SPAM", ".spa") # dos version @@ -116,7 +118,8 @@ The fields are used as follows: Note that the :py:attr:`tile` attribute contains a list of tile descriptors, not just a single descriptor. -The ``raw`` decoder +The raw decoder +--------------- The ``raw`` decoder is used to read uncompressed data from an image file. It can be used with most uncompressed file formats, such as PPM, BMP, uncompressed @@ -124,7 +127,7 @@ TIFF, and many others. To use the raw decoder with the :py:func:`PIL.Image.fromstring` function, use the following syntax:: image = Image.fromstring( - mode, size, data, "raw", + mode, size, data, "raw", raw mode, stride, orientation ) diff --git a/docs/index.rst b/docs/index.rst index 16e450856..0c7365c64 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,11 +1,11 @@ Pillow ====== -Pillow is the 'friendly' PIL fork by Alex Clark and Contributors. PIL is the Python Imaging Library by Fredrik Lundh and Contributors. +Pillow is the "friendly PIL fork" by `Alex Clark and Contributors `_. PIL is the Python Imaging Library by Fredrik Lundh and Contributors. .. image:: https://travis-ci.org/python-pillow/Pillow.svg?branch=master :target: https://travis-ci.org/python-pillow/Pillow - :alt: Travis CI build status + :alt: Travis CI build status (Linux) .. image:: https://pypip.in/v/Pillow/badge.png :target: https://pypi.python.org/pypi/Pillow/ @@ -16,20 +16,23 @@ Pillow is the 'friendly' PIL fork by Alex Clark and Contributors. PIL is the Pyt :alt: Number of PyPI downloads .. image:: https://coveralls.io/repos/python-pillow/Pillow/badge.png?branch=master - :target: https://coveralls.io/r/python-pillow/Pillow?branch=master - :alt: Test coverage + :target: https://coveralls.io/r/python-pillow/Pillow?branch=master + :alt: Code coverage -To install Pillow, please follow the :doc:`installation instructions `. To download source and/or contribute to development of Pillow please see: https://github.com/python-pillow/Pillow. +.. image:: https://landscape.io/github/python-pillow/Pillow/master/landscape.png + :target: https://landscape.io/github/python-pillow/Pillow/master + :alt: Code health .. toctree:: :maxdepth: 2 installation - about guides reference/index.rst handbook/appendices - original-readme + releasenotes/index.rst + about + pre-fork-readme Indices and tables ================== diff --git a/docs/installation.rst b/docs/installation.rst index a61213e15..e94afa892 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -1,39 +1,38 @@ Installation ============ -.. warning:: Pillow >= 2.1.0 no longer supports "import _imaging". Please use "from PIL.Image import core as _imaging" instead. +Warnings +-------- + +.. warning:: Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL. .. warning:: Pillow >= 1.0 no longer supports "import Image". Please use "from PIL import Image" instead. -.. warning:: PIL and Pillow currently cannot co-exist in the same environment. - If you want to use Pillow, please remove PIL first. +.. warning:: Pillow >= 2.1.0 no longer supports "import _imaging". Please use "from PIL.Image import core as _imaging" instead. -.. note:: Pillow >= 2.0.0 supports Python versions 2.6, 2.7, 3.2, 3.3, 3.4 +Notes +----- .. note:: Pillow < 2.0.0 supports Python versions 2.4, 2.5, 2.6, 2.7. -Simple installation -------------------- +.. note:: Pillow >= 2.0.0 supports Python versions 2.6, 2.7, 3.2, 3.3, 3.4 + +Basic Installation +------------------ .. note:: - The following instructions will install Pillow with support for most formats. - See :ref:`external-libraries` for the features you would gain by installing - the external libraries first. This page probably also include specific - instructions for your platform. + The following instructions will install Pillow with support for most common image formats. See :ref:`external-libraries` for a full list of external libraries supported. -You can install Pillow with :command:`pip`:: +Install Pillow with :command:`pip`:: $ pip install Pillow -Or :command:`easy_install` (for installing `Python Eggs -`_, as :command:`pip` does -not support them):: +Or use :command:`easy_install` for installing `Python Eggs `_ as :command:`pip` does not support them:: $ easy_install Pillow -Or download the `compressed archive from PyPI`_, extract it, and inside it -run:: +Or download and extract the `compressed archive from PyPI`_ and inside it run:: $ python setup.py install @@ -41,22 +40,22 @@ run:: .. _external-libraries: -External libraries +External Libraries ------------------ .. note:: - You *do not* need to install all of the external libraries to use Pillow's basic features. + You **do not need to install all external libraries supported** to use Pillow's basic features. Many of Pillow's features require external libraries: * **libjpeg** provides JPEG functionality. - * Pillow has been tested with libjpeg versions **6b**, **8**, and **9** + * Pillow has been tested with libjpeg versions **6b**, **8**, and **9** and libjpeg-turbo version **8**. * **zlib** provides access to compressed PNGs -* **libtiff** provides group4 tiff functionality +* **libtiff** provides compressed TIFF functionality * Pillow has been tested with libtiff versions **3.x** and **4.0** @@ -67,10 +66,10 @@ Many of Pillow's features require external libraries: * Pillow version 2.2.1 and below uses liblcms1, Pillow 2.3.0 and above uses liblcms2. Tested with **1.19** and **2.2**. -* **libwebp** provides the Webp format. +* **libwebp** provides the WebP format. * Pillow has been tested with version **0.1.3**, which does not read - transparent webp files. Versions **0.3.0** and **0.4.0** support + transparent WebP files. Versions **0.3.0** and **0.4.0** support transparency. * **tcl/tk** provides support for tkinter bitmap and photo images. @@ -93,50 +92,37 @@ line:: $ CFLAGS="-I/usr/pkg/include" pip install pillow +Build Options +------------- -Linux installation ------------------- +* Environment Variable: ``MAX_CONCURRENCY=n``. By default, Pillow will + use multiprocessing to build the extension on all available CPUs, + but not more than 4. Setting ``MAX_CONCURRENCY`` to 1 will disable + parallel building. -.. note:: +* Build flags: ``--disable-zlib``, ``--disable-jpeg``, + ``--disable-tiff``, ``--disable-freetype``, ``--disable-tcl``, + ``--disable-tk``, ``--disable-lcms``, ``--disable-webp``, + ``--disable-webpmux``, ``--disable-jpeg2000``. Disable building the + corresponding feature even if the development libraries are present + on the building machine. - Fedora, Debian/Ubuntu, and ArchLinux include Pillow (instead of PIL) with - their distributions. Consider using those instead of installing manually. +* Build flags: ``--enable-zlib``, ``--enable-jpeg``, + ``--enable-tiff``, ``--enable-freetype``, ``--enable-tcl``, + ``--enable-tk``, ``--enable-lcms``, ``--enable-webp``, + ``--enable-webpmux``, ``--enable-jpeg2000``. Require that the + corresponding feature is built. The build will raise an exception if + the libraries are not found. Webpmux (WebP metadata) relies on WebP + support. Tcl and Tk also must be used together. -**We do not provide binaries for Linux.** If you didn't build Python from -source, make sure you have Python's development libraries installed. In Debian -or Ubuntu:: +Sample Usage:: - $ sudo apt-get install python-dev python-setuptools + $ MAX_CONCURRENCY=1 python setup.py build_ext --enable-[feature] install -Or for Python 3:: +OS X Installation +----------------- - $ sudo apt-get install python3-dev python3-setuptools - -In Fedora, the command is:: - - $ sudo yum install python-devel - -Prerequisites are installed on **Ubuntu 10.04 LTS** with:: - - $ sudo apt-get install libtiff4-dev libjpeg62-dev zlib1g-dev \ - libfreetype6-dev tcl8.5-dev tk8.5-dev python-tk - -Prerequisites are installed with on **Ubuntu 12.04 LTS** or **Raspian Wheezy -7.0** with:: - - $ sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev \ - libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk - -Prerequisites are installed on **Fedora 20** with:: - - $ sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel \ - lcms2-devel libwebp-devel tcl-devel tk-devel - - -Mac OS X installation ---------------------- - -We provide binaries for OS X in the form of `Python Wheels `_. Alternatively you can compile Pillow with with XCode. +We provide binaries for OS X in the form of `Python Wheels `_. Alternatively you can compile Pillow from soure with XCode. The easiest way to install external libraries is via `Homebrew `_. After you install Homebrew, run:: @@ -146,7 +132,7 @@ Install Pillow with:: $ pip install Pillow -Windows installation +Windows Installation -------------------- We provide binaries for Windows in the form of Python Eggs and `Python Wheels @@ -167,11 +153,11 @@ Python Eggs Python Wheels ^^^^^^^^^^^^^ -.. Note:: Experimental. Requires setuptools >=0.8 and pip >=1.4.1 +.. Note:: Requires setuptools >=0.8 and pip >=1.4.1. Some older versions of pip required the ``--use-wheel`` flag. :: - $ pip install --use-wheel Pillow + $ pip install Pillow If the above does not work, it's likely because we haven't uploaded a wheel for the latest version of Pillow. In that case, try pinning it @@ -179,14 +165,13 @@ to a specific version: :: - $ pip install --use-wheel Pillow==2.3.0 + $ pip install Pillow==2.6.1 -FreeBSD installation ---------------------- +FreeBSD Installation +-------------------- .. Note:: Only FreeBSD 10 tested - Make sure you have Python's development libraries installed.:: $ sudo pkg install python2 @@ -199,9 +184,48 @@ Prerequisites are installed on **FreeBSD 10** with:: $ sudo pkg install jpeg tiff webp lcms2 freetype2 +Linux Installation +------------------ + +.. note:: + + Most major Linux distributions, including Fedora, Debian/Ubuntu and ArchLinux include Pillow in packages that previously contained PIL e.g. ``python-imaging``. Please consider using native operating system packages first to avoid installation problems and/or missing library support later. + +**We do not provide binaries for Linux.** If you didn't build Python from +source, make sure you have Python's development libraries installed. In Debian +or Ubuntu:: + + $ sudo apt-get install python-dev python-setuptools + +Or for Python 3:: + + $ sudo apt-get install python3-dev python3-setuptools + +In Fedora, the command is:: + + $ sudo yum install python-devel + +Prerequisites are installed on **Ubuntu 12.04 LTS** or **Raspian Wheezy +7.0** with:: + + $ sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev \ + libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk + +Prerequisites are installed on **Ubuntu 14.04 LTS** with:: + + $ sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev \ + libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk + +Prerequisites are installed on **Fedora 20** with:: + + $ sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel \ + lcms2-devel libwebp-devel tcl-devel tk-devel -Platform support + + + +Platform Support ---------------- Current platform support for Pillow. Binary distributions are contributed for @@ -211,15 +235,16 @@ current versions of Linux, OS X, and Windows. .. note:: - Contributors please test on your platform, edit this document, and send a - pull request. + Contributors please test Pillow on your platform then update this document and send a pull request. +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ |**Operating system** |**Supported**|**Tested Python versions** |**Tested Pillow versions** |**Tested processors** | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ -| Mac OS X 10.8 Mountain Lion |Yes | 2.6,2.7,3.2,3.3 | |x86-64 | +| Mac OS X 10.10 Yosemite |Yes | 2.7,3.3,3.4 | 2.8.1 |x86-64 | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ -| Mac OS X 10.7 Lion |Yes | 2.6,2.7,3.2,3.3 | 2.2.0 |x86-64 | +| Mac OS X 10.9 Mavericks |Yes | 2.7,3.4 | 2.6.1 |x86-64 | ++----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ +| Mac OS X 10.8 Mountain Lion |Yes | 2.6,2.7,3.2,3.3 | |x86-64 | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ | Redhat Linux 6 |Yes | 2.6 | |x86 | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ @@ -229,11 +254,12 @@ current versions of Linux, OS X, and Windows. +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ | Ubuntu Linux 10.04 LTS |Yes | 2.6 | 2.3.0 |x86,x86-64 | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ -| Ubuntu Linux 12.04 LTS |Yes | 2.6,2.7,3.2,3.3,PyPy2.1 | 2.3.0 |x86,x86-64 | +| Ubuntu Linux 12.04 LTS |Yes | 2.6,2.7,3.2,3.3,PyPy2.4, | 2.6.1 |x86,x86-64 | +| | | PyPy3,v2.3 | | | | | | | | | -| | | 2.7,3.2 | 2.3.0 |ppc | +| | | 2.7,3.2 | 2.6.1 |ppc | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ -| Ubuntu Linux 13.10 |Yes | 2.7,3.2,3.3 | 2.3.0 |x86 | +| Ubuntu Linux 14.04 LTS |Yes | 2.7,3.2,3.3,3.4 | 2.3.0 |x86 | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ | Raspian Wheezy |Yes | 2.7,3.2 | 2.3.0 |arm | +----------------------------------+-------------+------------------------------+------------------------------+-----------------------+ diff --git a/docs/porting-pil-to-pillow.rst b/docs/porting-pil-to-pillow.rst index 88a6768e9..7ad781186 100644 --- a/docs/porting-pil-to-pillow.rst +++ b/docs/porting-pil-to-pillow.rst @@ -21,3 +21,7 @@ automatically imports any file in the Python path with a name ending in :file:`ImagePlugin.py`. You will need to import your image plugin manually. +Pillow will raise an exception if the core extension can't be loaded +for any reason, including a version mismatch between the Python and +extension code. Previously PIL allowed Python only code to run if the +core extension was not available. diff --git a/docs/original-readme.rst b/docs/pre-fork-readme.rst similarity index 99% rename from docs/original-readme.rst rename to docs/pre-fork-readme.rst index 73b941f37..f50e2ef85 100644 --- a/docs/original-readme.rst +++ b/docs/pre-fork-readme.rst @@ -1,7 +1,7 @@ -Original PIL README -=================== +Pre-fork README +=============== -What follows is the original PIL 1.1.7 README file contents. +What follows is the untouched, original pre-fork PIL 1.1.7 README file contents. :: diff --git a/docs/reference/Image.rst b/docs/reference/Image.rst index 249543004..974d84a6e 100644 --- a/docs/reference/Image.rst +++ b/docs/reference/Image.rst @@ -41,7 +41,7 @@ Create thumbnails for infile in glob.glob("*.jpg"): file, ext = os.path.splitext(infile) im = Image.open(infile) - im.thumbnail(size, Image.ANTIALIAS) + im.thumbnail(size) im.save(file + ".thumbnail", "JPEG") Functions @@ -49,7 +49,14 @@ Functions .. autofunction:: open - .. warning:: To protect against potential DOS attacks caused by "`decompression bombs`_" (i.e. malicious files which decompress into a huge amount of data and are designed to crash or cause disruption by using up a lot of memory), Pillow will issue a `DecompressionBombWarning` if the image is over a certain limit. If desired, the warning can be turned into an error with `warnings.simplefilter('error', Image.DecompressionBombWarning)` or suppressed entirely with `warnings.simplefilter('ignore', Image.DecompressionBombWarning)`. See also `the logging documentation`_ to have warnings output to the logging facility instead of stderr. + .. warning:: + To protect against potential DOS attacks caused by "`decompression bombs`_" (i.e. malicious files + which decompress into a huge amount of data and are designed to crash or cause disruption by using up + a lot of memory), Pillow will issue a `DecompressionBombWarning` if the image is over a certain + limit. If desired, the warning can be turned into an error with + ``warnings.simplefilter('error', Image.DecompressionBombWarning)`` or suppressed entirely with + ``warnings.simplefilter('ignore', Image.DecompressionBombWarning)``. See also `the logging + documentation`_ to have warnings output to the logging facility instead of stderr. .. _decompression bombs: https://en.wikipedia.org/wiki/Zip_bomb .. _the logging documentation: https://docs.python.org/2/library/logging.html?highlight=logging#integration-with-the-warnings-module @@ -116,6 +123,7 @@ ITU-R 709, using the D65 luminant) to the CIE XYZ color space: .. automethod:: PIL.Image.Image.getcolors .. automethod:: PIL.Image.Image.getdata .. automethod:: PIL.Image.Image.getextrema +.. automethod:: PIL.Image.Image.getpalette .. automethod:: PIL.Image.Image.getpixel .. automethod:: PIL.Image.Image.histogram .. automethod:: PIL.Image.Image.offset @@ -163,7 +171,7 @@ Instances of the :py:class:`Image` class have the following attributes: Image mode. This is a string specifying the pixel format used by the image. Typical values are “1”, “L”, “RGB”, or “CMYK.” See - :doc:`../handbook/concepts` for a full list. + :ref:`concept-modes` for a full list. :type: :py:class:`string` @@ -192,4 +200,6 @@ Instances of the :py:class:`Image` class have the following attributes: operation affects the dictionary. If you need the information later on, keep a reference to the info dictionary returned from the open method. + Unless noted elsewhere, this dictionary does not affect saving files. + :type: :py:class:`dict` diff --git a/docs/reference/ImageDraw.rst b/docs/reference/ImageDraw.rst index c24a9dd99..e6d5c36ee 100644 --- a/docs/reference/ImageDraw.rst +++ b/docs/reference/ImageDraw.rst @@ -184,8 +184,11 @@ Methods :param xy: Four points to define the bounding box. Sequence of ``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``. - :param outline: Color to use for the outline. + :param start: Starting angle, in degrees. Angles are measured from + 3 o'clock, increasing clockwise. + :param end: Ending angle, in degrees. :param fill: Color to use for the fill. + :param outline: Color to use for the outline. .. py:method:: PIL.ImageDraw.Draw.point(xy, fill=None) diff --git a/docs/reference/ImageMath.rst b/docs/reference/ImageMath.rst index e3f9ed8d6..00249a601 100644 --- a/docs/reference/ImageMath.rst +++ b/docs/reference/ImageMath.rst @@ -13,7 +13,7 @@ Example: Using the :py:mod:`~PIL.ImageMath` module .. code-block:: python - import Image, ImageMath + from PIL import Image, ImageMath im1 = Image.open("image1.jpg") im2 = Image.open("image2.jpg") diff --git a/docs/reference/ImagePath.rst b/docs/reference/ImagePath.rst index 700464144..03aa39811 100644 --- a/docs/reference/ImagePath.rst +++ b/docs/reference/ImagePath.rst @@ -53,7 +53,7 @@ vector data. Path objects can be passed to the methods on the Converts the path to a Python list [(x, y), …]. :param flat: By default, this function returns a list of 2-tuples - [(x, y), ...]. If this argument is :keyword:`True`, it + [(x, y), ...]. If this argument is `True`, it returns a flat list [x, y, ...] instead. :return: A list of coordinates. See **flat**. diff --git a/docs/reference/PixelAccess.rst b/docs/reference/PixelAccess.rst new file mode 100644 index 000000000..55ab8c578 --- /dev/null +++ b/docs/reference/PixelAccess.rst @@ -0,0 +1,74 @@ +.. _PixelAccess: + +:py:class:`PixelAccess` Class +============================= + +The PixelAccess class provides read and write access to +:py:class:`PIL.Image` data at a pixel level. + +.. note:: Accessing individual pixels is fairly slow. If you are looping over all of the pixels in an image, there is likely a faster way using other parts of the Pillow API. + +Example +------- + +The following script loads an image, accesses one pixel from it, then +changes it. + +.. code-block:: python + + from PIL import Image + im = Image.open('hopper.jpg') + px = im.load() + print (px[4,4]) + px[4,4] = (0,0,0) + print (px[4,4]) + +Results in the following:: + + (23, 24, 68) + (0, 0, 0) + + + +:py:class:`PixelAccess` Class +----------------------------------- + +.. class:: PixelAccess + + .. method:: __setitem__(self, xy, color): + + Modifies the pixel at x,y. The color is given as a single + numerical value for single band images, and a tuple for + multi-band images + + :param xy: The pixel coordinate, given as (x, y). + :param value: The pixel value. + + .. method:: __getitem__(self, xy): + + Returns the pixel at x,y. The pixel is returned as a single + value for single band images or a tuple for multiple band + images + + :param xy: The pixel coordinate, given as (x, y). + :returns: a pixel value for single band images, a tuple of + pixel values for multiband images. + + .. method:: putpixel(self, xy, color): + + Modifies the pixel at x,y. The color is given as a single + numerical value for single band images, and a tuple for + multi-band images + + :param xy: The pixel coordinate, given as (x, y). + :param value: The pixel value. + + .. method:: getpixel(self, xy): + + Returns the pixel at x,y. The pixel is returned as a single + value for single band images or a tuple for multiple band + images + + :param xy: The pixel coordinate, given as (x, y). + :returns: a pixel value for single band images, a tuple of + pixel values for multiband images. diff --git a/docs/reference/PyAccess.rst b/docs/reference/PyAccess.rst new file mode 100644 index 000000000..cb853f89e --- /dev/null +++ b/docs/reference/PyAccess.rst @@ -0,0 +1,38 @@ +.. py:module:: PIL.PyAccess +.. py:currentmodule:: PIL.PyAccess + +:py:mod:`PyAccess` Module +========================= + +The :py:mod:`PyAccess` module provides a CFFI/Python implementation of the :ref:`PixelAccess`. This implementation is far faster on PyPy than the PixelAccess version. + +.. note:: Accessing individual pixels is fairly slow. If you are + looping over all of the pixels in an image, there is likely + a faster way using other parts of the Pillow API. + +Example +------- + +The following script loads an image, accesses one pixel from it, then changes it. + +.. code-block:: python + + from PIL import Image + im = Image.open('hopper.jpg') + px = im.load() + print (px[4,4]) + px[4,4] = (0,0,0) + print (px[4,4]) + +Results in the following:: + + (23, 24, 68) + (0, 0, 0) + + + +:py:class:`PyAccess` Class +-------------------------- + +.. autoclass:: PIL.PyAccess.PyAccess() + :members: diff --git a/docs/reference/index.rst b/docs/reference/index.rst index 2f10b861d..73a3ecfed 100644 --- a/docs/reference/index.rst +++ b/docs/reference/index.rst @@ -28,4 +28,6 @@ Reference ExifTags OleFileIO PSDraw + PixelAccess + PyAccess ../PIL diff --git a/docs/releasenotes/2.7.0.rst b/docs/releasenotes/2.7.0.rst new file mode 100644 index 000000000..65a8f2d11 --- /dev/null +++ b/docs/releasenotes/2.7.0.rst @@ -0,0 +1,175 @@ +2.7.0 +===== + +Sane Plugin +----------- + +The Sane plugin has now been split into its own repo: +https://github.com/python-pillow/Sane . + + +Png text chunk size limits +-------------------------- + +To prevent potential denial of service attacks using compressed text +chunks, there are now limits to the decompressed size of text chunks +decoded from PNG images. If the limits are exceeded when opening a PNG +image a ``ValueError`` will be raised. + +Individual text chunks are limited to +:py:attr:`PIL.PngImagePlugin.MAX_TEXT_CHUNK`, set to 1MB by +default. The total decompressed size of all text chunks is limited to +:py:attr:`PIL.PngImagePlugin.MAX_TEXT_MEMORY`, which defaults to +64MB. These values can be changed prior to opening PNG images if you +know that there are large text blocks that are desired. + +Image resizing filters +---------------------- + +Image resizing methods :py:meth:`~PIL.Image.Image.resize` and +:py:meth:`~PIL.Image.Image.thumbnail` take a `resample` argument, which tells +which filter should be used for resampling. Possible values are: +:py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BILINEAR`, +:py:attr:`PIL.Image.BICUBIC` and :py:attr:`PIL.Image.ANTIALIAS`. +Almost all of them were changed in this version. + +Bicubic and bilinear downscaling +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +From the beginning :py:attr:`~PIL.Image.BILINEAR` and +:py:attr:`~PIL.Image.BICUBIC` filters were based on affine transformations +and used a fixed number of pixels from the source image for every destination +pixel (2x2 pixels for :py:attr:`~PIL.Image.BILINEAR` and 4x4 for +:py:attr:`~PIL.Image.BICUBIC`). This gave an unsatisfactory result for +downscaling. At the same time, a high quality convolutions-based algorithm with +flexible kernel was used for :py:attr:`~PIL.Image.ANTIALIAS` filter. + +Starting from Pillow 2.7.0, a high quality convolutions-based algorithm is used +for all of these three filters. + +If you have previously used any tricks to maintain quality when downscaling with +:py:attr:`~PIL.Image.BILINEAR` and :py:attr:`~PIL.Image.BICUBIC` filters +(for example, reducing within several steps), they are unnecessary now. + +Antialias renamed to Lanczos +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A new :py:attr:`PIL.Image.LANCZOS` constant was added instead of +:py:attr:`~PIL.Image.ANTIALIAS`. + +When :py:attr:`~PIL.Image.ANTIALIAS` was initially added, it was the only +high-quality filter based on convolutions. It's name was supposed to reflect +this. Starting from Pillow 2.7.0 all resize method are based on convolutions. +All of them are antialias from now on. And the real name of the +:py:attr:`~PIL.Image.ANTIALIAS` filter is Lanczos filter. + +The :py:attr:`~PIL.Image.ANTIALIAS` constant is left for backward compatibility +and is an alias for :py:attr:`~PIL.Image.LANCZOS`. + +Lanczos upscaling quality +^^^^^^^^^^^^^^^^^^^^^^^^^ + +The image upscaling quality with :py:attr:`~PIL.Image.LANCZOS` filter was +almost the same as :py:attr:`~PIL.Image.BILINEAR` due to bug. This has been fixed. + +Bicubic upscaling quality +^^^^^^^^^^^^^^^^^^^^^^^^^ + +The :py:attr:`~PIL.Image.BICUBIC` filter for affine transformations produced +sharp, slightly pixelated image for upscaling. Bicubic for convolutions is +more soft. + +Resize performance +^^^^^^^^^^^^^^^^^^ + +In most cases, convolution is more a expensive algorithm for downscaling +because it takes into account all the pixels of source image. Therefore +:py:attr:`~PIL.Image.BILINEAR` and :py:attr:`~PIL.Image.BICUBIC` filters' +performance can be lower than before. On the other hand the quality of +:py:attr:`~PIL.Image.BILINEAR` and :py:attr:`~PIL.Image.BICUBIC` was close to +:py:attr:`~PIL.Image.NEAREST`. So if such quality is suitable for your tasks +you can switch to :py:attr:`~PIL.Image.NEAREST` filter for downscaling, +which will give a huge improvement in performance. + +At the same time performance of convolution resampling for downscaling has been +improved by around a factor of two compared to the previous version. +The upscaling performance of the :py:attr:`~PIL.Image.LANCZOS` filter has +remained the same. For :py:attr:`~PIL.Image.BILINEAR` filter it has improved by +1.5 times and for :py:attr:`~PIL.Image.BICUBIC` by four times. + +Default filter for thumbnails +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +In Pillow 2.5 the default filter for :py:meth:`~PIL.Image.Image.thumbnail` was +changed from :py:attr:`~PIL.Image.NEAREST` to :py:attr:`~PIL.Image.ANTIALIAS`. +Antialias was chosen because all the other filters gave poor quality for +reduction. Starting from Pillow 2.7.0, :py:attr:`~PIL.Image.ANTIALIAS` has been +replaced with :py:attr:`~PIL.Image.BICUBIC`, because it's faster and +:py:attr:`~PIL.Image.ANTIALIAS` doesn't give any advantages after +downscaling with libjpeg, which uses supersampling internally, not convolutions. + +Image transposition +------------------- + +A new method :py:attr:`PIL.Image.TRANSPOSE` has been added for the +:py:meth:`~PIL.Image.Image.transpose` operation in addition to +:py:attr:`~PIL.Image.FLIP_LEFT_RIGHT`, :py:attr:`~PIL.Image.FLIP_TOP_BOTTOM`, +:py:attr:`~PIL.Image.ROTATE_90`, :py:attr:`~PIL.Image.ROTATE_180`, +:py:attr:`~PIL.Image.ROTATE_270`. :py:attr:`~PIL.Image.TRANSPOSE` is an algebra +transpose, with an image reflected across its main diagonal. + +The speed of :py:attr:`~PIL.Image.ROTATE_90`, :py:attr:`~PIL.Image.ROTATE_270` +and :py:attr:`~PIL.Image.TRANSPOSE` has been significantly improved for large +images which don't fit in the processor cache. + +Gaussian blur and unsharp mask +------------------------------ + +The :py:meth:`~PIL.ImageFilter.GaussianBlur` implementation has been replaced +with a sequential application of box filters. The new implementation is based on +"Theoretical foundations of Gaussian convolution by extended box filtering" from +the Mathematical Image Analysis Group. As :py:meth:`~PIL.ImageFilter.UnsharpMask` +implementations use Gaussian blur internally, all changes from this chapter +are also applicable to it. + +Blur radius +^^^^^^^^^^^ + +There was an error in the previous version of Pillow, where blur radius (the +standard deviation of Gaussian) actually meant blur diameter. For example, to +blur an image with actual radius 5 you were forced to use value 10. This has +been fixed. Now the meaning of the radius is the same as in other software. + +If you used a Gaussian blur with some radius value, you need to divide this +value by two. + +Blur performance +^^^^^^^^^^^^^^^^ + +Box filter computation time is constant relative to the radius and depends +on source image size only. Because the new Gaussian blur implementation +is based on box filter, its computation time also doesn't depends on the blur +radius. + +For example, previously, if the execution time for a given test image was 1 +second for radius 1, 3.6 seconds for radius 10 and 17 seconds for 50, now blur +with any radius on same image is executed for 0.2 seconds. + +Blur quality +^^^^^^^^^^^^ + +The previous implementation takes into account only source pixels within +2 * standard deviation radius for every destination pixel. This was not enough, +so the quality was worse compared to other Gaussian blur software. + +The new implementation does not have this drawback. + +TIFF Parameter Changes +---------------------- + +Several kwarg parameters for saving TIFF images were previously +specified as strings with included spaces (e.g. 'x resolution'). This +was difficult to use as kwargs without constructing and passing a +dictionary. These parameters now use the underscore character instead +of space. (e.g. 'x_resolution') + diff --git a/docs/releasenotes/2.8.0.rst b/docs/releasenotes/2.8.0.rst new file mode 100644 index 000000000..85235d72a --- /dev/null +++ b/docs/releasenotes/2.8.0.rst @@ -0,0 +1,22 @@ +2.8.0 +===== + +Open HTTP response objects with Image.open +------------------------------------------ + +HTTP response objects returned from `urllib2.urlopen(url)` or `requests.get(url, stream=True).raw` are 'file-like' but do not support `.seek()` operations. As a result PIL was unable to open them as images, requiring a wrap in `cStringIO` or `BytesIO`. + +Now new functionality has been added to `Image.open()` by way of an `.seek(0)` check and catch on exception `AttributeError` or `io.UnsupportedOperation`. If this is caught we attempt to wrap the object using `io.BytesIO` (which will only work on buffer-file-like objects). + +This allows opening of files using both `urllib2` and `requests`, e.g.:: + + Image.open(urllib2.urlopen(url)) + Image.open(requests.get(url, stream=True).raw) + +If the response uses content-encoding (compression, either gzip or deflate) then this will fail as both the urllib2 and requests raw file object will produce compressed data in that case. Using Content-Encoding on images is rather non-sensical as most images are already compressed, but it can still happen. + +For requests the work-around is to set the decode_content attribute on the raw object to True:: + + response = requests.get(url, stream=True) + response.raw.decode_content = True + image = Image.open(response.raw) diff --git a/docs/releasenotes/index.rst b/docs/releasenotes/index.rst new file mode 100644 index 000000000..d70edc54d --- /dev/null +++ b/docs/releasenotes/index.rst @@ -0,0 +1,10 @@ +Release Notes +============= + +.. note:: Contributors please include release notes as needed or appropriate with your bug fixes, feature additions and tests. + +.. toctree:: + :maxdepth: 2 + + 2.7.0 + 2.8.0 diff --git a/docs/requirements.txt b/docs/requirements.txt index f08f7633e..6b104fb01 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,19 +1,2 @@ -sphinx-better-theme - -## requirements for working on docs -# -## install pillow from master if you're into that, but RtD needs this -#pillow>=2.4.0 -# -#Jinja2==2.7.1 -#MarkupSafe==0.18 -#Pygments==1.6 -#Sphinx==1.1.3 -#docopt==0.6.1 -#docutils==0.11 -#wsgiref==0.1.2 -#sphinx-better-theme==0.1.5 -# -## livereload not strictly necessary but really useful (make livehtml) -#tornado==3.1.1 -#livereload==1.0.1 +Sphinx +sphinx-rtd-theme diff --git a/encode.c b/encode.c index fef102176..6bdb8c71a 100644 --- a/encode.c +++ b/encode.c @@ -737,7 +737,7 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args) return NULL; } - // While failes on 64 bit machines, complains that pos is an int instead of a Py_ssize_t + // While fails on 64 bit machines, complains that pos is an int instead of a Py_ssize_t // while (PyDict_Next(dir, &pos, &key, &value)) { for (pos=0;pos - -/* resampling filters (from antialias.py) */ - -struct filter { - float (*filter)(float x); - float support; -}; - -static inline float sinc_filter(float x) -{ - if (x == 0.0) - return 1.0; - x = x * M_PI; - return sin(x) / x; -} - -static inline float antialias_filter(float x) -{ - /* lanczos (truncated sinc) */ - if (-3.0 <= x && x < 3.0) - return sinc_filter(x) * sinc_filter(x/3); - return 0.0; -} - -static struct filter ANTIALIAS = { antialias_filter, 3.0 }; - -static inline float nearest_filter(float x) -{ - if (-0.5 <= x && x < 0.5) - return 1.0; - return 0.0; -} - -static struct filter NEAREST = { nearest_filter, 0.5 }; - -static inline float bilinear_filter(float x) -{ - if (x < 0.0) - x = -x; - if (x < 1.0) - return 1.0-x; - return 0.0; -} - -static struct filter BILINEAR = { bilinear_filter, 1.0 }; - -static inline float bicubic_filter(float x) -{ - /* FIXME: double-check this algorithm */ - /* FIXME: for best results, "a" should be -0.5 to -1.0, but we'll - set it to zero for now, to match the 1.1 magnifying filter */ -#define a 0.0 - if (x < 0.0) - x = -x; - if (x < 1.0) - return (((a + 2.0) * x) - (a + 3.0)) * x*x + 1; - if (x < 2.0) - return (((a * x) - 5*a) * x + 8) * x - 4*a; - return 0.0; -#undef a -} - -static struct filter BICUBIC = { bicubic_filter, 2.0 }; - -Imaging -ImagingStretch(Imaging imOut, Imaging imIn, int filter) -{ - /* FIXME: this is a quick and straightforward translation from a - python prototype. might need some further C-ification... */ - - ImagingSectionCookie cookie; - struct filter *filterp; - float support, scale, filterscale; - float center, ww, ss, ymin, ymax, xmin, xmax; - int xx, yy, x, y, b; - float *k; - - /* check modes */ - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); - - /* check filter */ - switch (filter) { - case IMAGING_TRANSFORM_NEAREST: - filterp = &NEAREST; - break; - case IMAGING_TRANSFORM_ANTIALIAS: - filterp = &ANTIALIAS; - break; - case IMAGING_TRANSFORM_BILINEAR: - filterp = &BILINEAR; - break; - case IMAGING_TRANSFORM_BICUBIC: - filterp = &BICUBIC; - break; - default: - return (Imaging) ImagingError_ValueError( - "unsupported resampling filter" - ); - } - - if (imIn->ysize == imOut->ysize) { - /* prepare for horizontal stretch */ - filterscale = scale = (float) imIn->xsize / imOut->xsize; - } else if (imIn->xsize == imOut->xsize) { - /* prepare for vertical stretch */ - filterscale = scale = (float) imIn->ysize / imOut->ysize; - } else - return (Imaging) ImagingError_Mismatch(); - - /* determine support size (length of resampling filter) */ - support = filterp->support; - - if (filterscale < 1.0) { - filterscale = 1.0; - support = 0.5; - } - - support = support * filterscale; - - /* coefficient buffer (with rounding safety margin) */ - k = malloc(((int) support * 2 + 10) * sizeof(float)); - if (!k) - return (Imaging) ImagingError_MemoryError(); - - ImagingSectionEnter(&cookie); - if (imIn->xsize == imOut->xsize) { - /* vertical stretch */ - for (yy = 0; yy < imOut->ysize; yy++) { - center = (yy + 0.5) * scale; - ww = 0.0; - ss = 1.0 / filterscale; - /* calculate filter weights */ - ymin = floor(center - support); - if (ymin < 0.0) - ymin = 0.0; - ymax = ceil(center + support); - if (ymax > (float) imIn->ysize) - ymax = (float) imIn->ysize; - for (y = (int) ymin; y < (int) ymax; y++) { - float w = filterp->filter((y - center + 0.5) * ss) * ss; - k[y - (int) ymin] = w; - ww = ww + w; - } - if (ww == 0.0) - ww = 1.0; - else - ww = 1.0 / ww; - if (imIn->image8) { - /* 8-bit grayscale */ - for (xx = 0; xx < imOut->xsize; xx++) { - ss = 0.0; - for (y = (int) ymin; y < (int) ymax; y++) - ss = ss + imIn->image8[y][xx] * k[y - (int) ymin]; - ss = ss * ww + 0.5; - if (ss < 0.5) - imOut->image8[yy][xx] = 0; - else if (ss >= 255.0) - imOut->image8[yy][xx] = 255; - else - imOut->image8[yy][xx] = (UINT8) ss; - } - } else - switch(imIn->type) { - case IMAGING_TYPE_UINT8: - /* n-bit grayscale */ - for (xx = 0; xx < imOut->xsize*4; xx++) { - /* FIXME: skip over unused pixels */ - ss = 0.0; - for (y = (int) ymin; y < (int) ymax; y++) - ss = ss + (UINT8) imIn->image[y][xx] * k[y-(int) ymin]; - ss = ss * ww + 0.5; - if (ss < 0.5) - imOut->image[yy][xx] = (UINT8) 0; - else if (ss >= 255.0) - imOut->image[yy][xx] = (UINT8) 255; - else - imOut->image[yy][xx] = (UINT8) ss; - } - break; - case IMAGING_TYPE_INT32: - /* 32-bit integer */ - for (xx = 0; xx < imOut->xsize; xx++) { - ss = 0.0; - for (y = (int) ymin; y < (int) ymax; y++) - ss = ss + IMAGING_PIXEL_I(imIn, xx, y) * k[y - (int) ymin]; - IMAGING_PIXEL_I(imOut, xx, yy) = (int) ss * ww; - } - break; - case IMAGING_TYPE_FLOAT32: - /* 32-bit float */ - for (xx = 0; xx < imOut->xsize; xx++) { - ss = 0.0; - for (y = (int) ymin; y < (int) ymax; y++) - ss = ss + IMAGING_PIXEL_F(imIn, xx, y) * k[y - (int) ymin]; - IMAGING_PIXEL_F(imOut, xx, yy) = ss * ww; - } - break; - default: - ImagingSectionLeave(&cookie); - return (Imaging) ImagingError_ModeError(); - } - } - } else { - /* horizontal stretch */ - for (xx = 0; xx < imOut->xsize; xx++) { - center = (xx + 0.5) * scale; - ww = 0.0; - ss = 1.0 / filterscale; - xmin = floor(center - support); - if (xmin < 0.0) - xmin = 0.0; - xmax = ceil(center + support); - if (xmax > (float) imIn->xsize) - xmax = (float) imIn->xsize; - for (x = (int) xmin; x < (int) xmax; x++) { - float w = filterp->filter((x - center + 0.5) * ss) * ss; - k[x - (int) xmin] = w; - ww = ww + w; - } - if (ww == 0.0) - ww = 1.0; - else - ww = 1.0 / ww; - if (imIn->image8) { - /* 8-bit grayscale */ - for (yy = 0; yy < imOut->ysize; yy++) { - ss = 0.0; - for (x = (int) xmin; x < (int) xmax; x++) - ss = ss + imIn->image8[yy][x] * k[x - (int) xmin]; - ss = ss * ww + 0.5; - if (ss < 0.5) - imOut->image8[yy][xx] = (UINT8) 0; - else if (ss >= 255.0) - imOut->image8[yy][xx] = (UINT8) 255; - else - imOut->image8[yy][xx] = (UINT8) ss; - } - } else - switch(imIn->type) { - case IMAGING_TYPE_UINT8: - /* n-bit grayscale */ - for (yy = 0; yy < imOut->ysize; yy++) { - for (b = 0; b < imIn->bands; b++) { - if (imIn->bands == 2 && b) - b = 3; /* hack to deal with LA images */ - ss = 0.0; - for (x = (int) xmin; x < (int) xmax; x++) - ss = ss + (UINT8) imIn->image[yy][x*4+b] * k[x - (int) xmin]; - ss = ss * ww + 0.5; - if (ss < 0.5) - imOut->image[yy][xx*4+b] = (UINT8) 0; - else if (ss >= 255.0) - imOut->image[yy][xx*4+b] = (UINT8) 255; - else - imOut->image[yy][xx*4+b] = (UINT8) ss; - } - } - break; - case IMAGING_TYPE_INT32: - /* 32-bit integer */ - for (yy = 0; yy < imOut->ysize; yy++) { - ss = 0.0; - for (x = (int) xmin; x < (int) xmax; x++) - ss = ss + IMAGING_PIXEL_I(imIn, x, yy) * k[x - (int) xmin]; - IMAGING_PIXEL_I(imOut, xx, yy) = (int) ss * ww; - } - break; - case IMAGING_TYPE_FLOAT32: - /* 32-bit float */ - for (yy = 0; yy < imOut->ysize; yy++) { - ss = 0.0; - for (x = (int) xmin; x < (int) xmax; x++) - ss = ss + IMAGING_PIXEL_F(imIn, x, yy) * k[x - (int) xmin]; - IMAGING_PIXEL_F(imOut, xx, yy) = ss * ww; - } - break; - default: - ImagingSectionLeave(&cookie); - return (Imaging) ImagingError_ModeError(); - } - } - } - ImagingSectionLeave(&cookie); - - free(k); - - return imOut; -} diff --git a/libImaging/BoxBlur.c b/libImaging/BoxBlur.c new file mode 100644 index 000000000..0fe3b7c35 --- /dev/null +++ b/libImaging/BoxBlur.c @@ -0,0 +1,309 @@ +#include "Python.h" +#include "Imaging.h" + + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + + +typedef UINT8 pixel[4]; + +void static inline +ImagingLineBoxBlur32(pixel *lineOut, pixel *lineIn, int lastx, int radius, int edgeA, + int edgeB, UINT32 ww, UINT32 fw) +{ + int x; + UINT32 acc[4]; + UINT32 bulk[4]; + + #define MOVE_ACC(acc, subtract, add) \ + acc[0] += lineIn[add][0] - lineIn[subtract][0]; \ + acc[1] += lineIn[add][1] - lineIn[subtract][1]; \ + acc[2] += lineIn[add][2] - lineIn[subtract][2]; \ + acc[3] += lineIn[add][3] - lineIn[subtract][3]; + + #define ADD_FAR(bulk, acc, left, right) \ + bulk[0] = (acc[0] * ww) + (lineIn[left][0] + lineIn[right][0]) * fw; \ + bulk[1] = (acc[1] * ww) + (lineIn[left][1] + lineIn[right][1]) * fw; \ + bulk[2] = (acc[2] * ww) + (lineIn[left][2] + lineIn[right][2]) * fw; \ + bulk[3] = (acc[3] * ww) + (lineIn[left][3] + lineIn[right][3]) * fw; + + #define SAVE(x, bulk) \ + lineOut[x][0] = (UINT8)((bulk[0] + (1 << 23)) >> 24); \ + lineOut[x][1] = (UINT8)((bulk[1] + (1 << 23)) >> 24); \ + lineOut[x][2] = (UINT8)((bulk[2] + (1 << 23)) >> 24); \ + lineOut[x][3] = (UINT8)((bulk[3] + (1 << 23)) >> 24); + + /* Compute acc for -1 pixel (outside of image): + From "-radius-1" to "-1" get first pixel, + then from "0" to "radius-1". */ + acc[0] = lineIn[0][0] * (radius + 1); + acc[1] = lineIn[0][1] * (radius + 1); + acc[2] = lineIn[0][2] * (radius + 1); + acc[3] = lineIn[0][3] * (radius + 1); + /* As radius can be bigger than xsize, iterate to edgeA -1. */ + for (x = 0; x < edgeA - 1; x++) { + acc[0] += lineIn[x][0]; + acc[1] += lineIn[x][1]; + acc[2] += lineIn[x][2]; + acc[3] += lineIn[x][3]; + } + /* Then multiply remainder to last x. */ + acc[0] += lineIn[lastx][0] * (radius - edgeA + 1); + acc[1] += lineIn[lastx][1] * (radius - edgeA + 1); + acc[2] += lineIn[lastx][2] * (radius - edgeA + 1); + acc[3] += lineIn[lastx][3] * (radius - edgeA + 1); + + if (edgeA <= edgeB) + { + /* Subtract pixel from left ("0"). + Add pixels from radius. */ + for (x = 0; x < edgeA; x++) { + MOVE_ACC(acc, 0, x + radius); + ADD_FAR(bulk, acc, 0, x + radius + 1); + SAVE(x, bulk); + } + /* Subtract previous pixel from "-radius". + Add pixels from radius. */ + for (x = edgeA; x < edgeB; x++) { + MOVE_ACC(acc, x - radius - 1, x + radius); + ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1); + SAVE(x, bulk); + } + /* Subtract previous pixel from "-radius". + Add last pixel. */ + for (x = edgeB; x <= lastx; x++) { + MOVE_ACC(acc, x - radius - 1, lastx); + ADD_FAR(bulk, acc, x - radius - 1, lastx); + SAVE(x, bulk); + } + } + else + { + for (x = 0; x < edgeB; x++) { + MOVE_ACC(acc, 0, x + radius); + ADD_FAR(bulk, acc, 0, x + radius + 1); + SAVE(x, bulk); + } + for (x = edgeB; x < edgeA; x++) { + MOVE_ACC(acc, 0, lastx); + ADD_FAR(bulk, acc, 0, lastx); + SAVE(x, bulk); + } + for (x = edgeA; x <= lastx; x++) { + MOVE_ACC(acc, x - radius - 1, lastx); + ADD_FAR(bulk, acc, x - radius - 1, lastx); + SAVE(x, bulk); + } + } + + #undef MOVE_ACC + #undef ADD_FAR + #undef SAVE +} + + +void static inline +ImagingLineBoxBlur8(UINT8 *lineOut, UINT8 *lineIn, int lastx, int radius, int edgeA, + int edgeB, UINT32 ww, UINT32 fw) +{ + int x; + UINT32 acc; + UINT32 bulk; + + #define MOVE_ACC(acc, subtract, add) \ + acc += lineIn[add] - lineIn[subtract]; + + #define ADD_FAR(bulk, acc, left, right) \ + bulk = (acc * ww) + (lineIn[left] + lineIn[right]) * fw; + + #define SAVE(x, bulk) \ + lineOut[x] = (UINT8)((bulk + (1 << 23)) >> 24) + + acc = lineIn[0] * (radius + 1); + for (x = 0; x < edgeA - 1; x++) { + acc += lineIn[x]; + } + acc += lineIn[lastx] * (radius - edgeA + 1); + + if (edgeA <= edgeB) + { + for (x = 0; x < edgeA; x++) { + MOVE_ACC(acc, 0, x + radius); + ADD_FAR(bulk, acc, 0, x + radius + 1); + SAVE(x, bulk); + } + for (x = edgeA; x < edgeB; x++) { + MOVE_ACC(acc, x - radius - 1, x + radius); + ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1); + SAVE(x, bulk); + } + for (x = edgeB; x <= lastx; x++) { + MOVE_ACC(acc, x - radius - 1, lastx); + ADD_FAR(bulk, acc, x - radius - 1, lastx); + SAVE(x, bulk); + } + } + else + { + for (x = 0; x < edgeB; x++) { + MOVE_ACC(acc, 0, x + radius); + ADD_FAR(bulk, acc, 0, x + radius + 1); + SAVE(x, bulk); + } + for (x = edgeB; x < edgeA; x++) { + MOVE_ACC(acc, 0, lastx); + ADD_FAR(bulk, acc, 0, lastx); + SAVE(x, bulk); + } + for (x = edgeA; x <= lastx; x++) { + MOVE_ACC(acc, x - radius - 1, lastx); + ADD_FAR(bulk, acc, x - radius - 1, lastx); + SAVE(x, bulk); + } + } + + #undef MOVE_ACC + #undef ADD_FAR + #undef SAVE +} + + + +Imaging +ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) +{ + ImagingSectionCookie cookie; + + int y; + + int radius = (int) floatRadius; + UINT32 ww = (UINT32) (1 << 24) / (floatRadius * 2 + 1); + UINT32 fw = ((1 << 24) - (radius * 2 + 1) * ww) / 2; + + int edgeA = MIN(radius + 1, imIn->xsize); + int edgeB = MAX(imIn->xsize - radius - 1, 0); + + UINT32 *lineOut = calloc(imIn->xsize, sizeof(UINT32)); + if (lineOut == NULL) + return ImagingError_MemoryError(); + + // printf(">>> %d %d %d\n", radius, ww, fw); + + ImagingSectionEnter(&cookie); + + if (imIn->image8) + { + for (y = 0; y < imIn->ysize; y++) { + ImagingLineBoxBlur8( + (imIn == imOut ? (UINT8 *) lineOut : imOut->image8[y]), + imIn->image8[y], + imIn->xsize - 1, + radius, edgeA, edgeB, + ww, fw + ); + if (imIn == imOut) { + // Commit. + memcpy(imOut->image8[y], lineOut, imIn->xsize); + } + } + } + else + { + for (y = 0; y < imIn->ysize; y++) { + ImagingLineBoxBlur32( + imIn == imOut ? (pixel *) lineOut : (pixel *) imOut->image32[y], + (pixel *) imIn->image32[y], + imIn->xsize - 1, + radius, edgeA, edgeB, + ww, fw + ); + if (imIn == imOut) { + // Commit. + memcpy(imOut->image32[y], lineOut, imIn->xsize * 4); + } + } + } + + ImagingSectionLeave(&cookie); + + free(lineOut); + + return imOut; +} + + +Imaging +ImagingBoxBlur(Imaging imOut, Imaging imIn, float radius, int n) +{ + int i; + Imaging imTransposed; + + if (n < 1) { + return ImagingError_ValueError( + "number of passes must be greater than zero" + ); + } + + if (strcmp(imIn->mode, imOut->mode) || + imIn->type != imOut->type || + imIn->bands != imOut->bands || + imIn->xsize != imOut->xsize || + imIn->ysize != imOut->ysize) + return ImagingError_Mismatch(); + + if (imIn->type != IMAGING_TYPE_UINT8) + return ImagingError_ModeError(); + + if (!(strcmp(imIn->mode, "RGB") == 0 || + strcmp(imIn->mode, "RGBA") == 0 || + strcmp(imIn->mode, "RGBX") == 0 || + strcmp(imIn->mode, "CMYK") == 0 || + strcmp(imIn->mode, "L") == 0 || + strcmp(imIn->mode, "LA") == 0)) + return ImagingError_ModeError(); + + imTransposed = ImagingNew(imIn->mode, imIn->ysize, imIn->xsize); + if (!imTransposed) + return NULL; + + /* Apply blur in one dimension. + Use imOut as a destination at first pass, + then use imOut as a source too. */ + ImagingHorizontalBoxBlur(imOut, imIn, radius); + for (i = 1; i < n; i ++) { + ImagingHorizontalBoxBlur(imOut, imOut, radius); + } + /* Transpose result for blur in another direction. */ + ImagingTranspose(imTransposed, imOut); + + /* Reuse imTransposed as a source and destination there. */ + for (i = 0; i < n; i ++) { + ImagingHorizontalBoxBlur(imTransposed, imTransposed, radius); + } + /* Restore original orientation. */ + ImagingTranspose(imOut, imTransposed); + + ImagingDelete(imTransposed); + + return imOut; +} + + +Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius, + int passes) +{ + float sigma2, L, l, a; + + sigma2 = radius * radius / passes; + // from http://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf + // [7] Box length. + L = sqrt(12.0 * sigma2 + 1.0); + // [11] Integer part of box radius. + l = floor((L - 1.0) / 2.0); + // [14], [Fig. 2] Fractional part of box radius. + a = (2 * l + 1) * (l * (l + 1) - 3 * sigma2); + a /= 6 * (sigma2 - (l + 1) * (l + 1)); + + return ImagingBoxBlur(imOut, imIn, l + a, passes); +} diff --git a/libImaging/Dib.c b/libImaging/Dib.c index 8e138bd6b..55de7d9a5 100644 --- a/libImaging/Dib.c +++ b/libImaging/Dib.c @@ -204,19 +204,6 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) #endif -#if 0 - { - /* DEBUG: dump palette to file */ - FILE *err = fopen("dib.pal", "w"); - for (i = 0; i < 256; i++) - fprintf(err, "%d: %d/%d/%d\n", i, - pal->palPalEntry[i].peRed, - pal->palPalEntry[i].peGreen, - pal->palPalEntry[i].peBlue); - fclose(err); - } -#endif - dib->palette = CreatePalette(pal); } diff --git a/libImaging/Effects.c b/libImaging/Effects.c index eb598d968..a3f5e7cd9 100644 --- a/libImaging/Effects.c +++ b/libImaging/Effects.c @@ -98,8 +98,8 @@ ImagingEffectNoise(int xsize, int ysize, float sigma) /* after numerical recipes */ double v1, v2, radius, factor; do { - v1 = rand()*(2.0/32767.0) - 1.0; - v2 = rand()*(2.0/32767.0) - 1.0; + v1 = rand()*(2.0/RAND_MAX) - 1.0; + v2 = rand()*(2.0/RAND_MAX) - 1.0; radius= v1*v1 + v2*v2; } while (radius >= 1.0); factor = sqrt(-2.0*log(radius)/radius); diff --git a/libImaging/Geometry.c b/libImaging/Geometry.c index 0f59ee0d5..97cbcf2ed 100644 --- a/libImaging/Geometry.c +++ b/libImaging/Geometry.c @@ -30,11 +30,18 @@ /* Undef if you don't need resampling filters */ #define WITH_FILTERS +/* For large images rotation is an inefficient operation in terms of CPU cache. + One row in the source image affects each column in destination. + Rotating in chunks that fit in the cache can speed up rotation + 8x on a modern CPU. A chunk size of 128 requires only 65k and is large enough + that the overhead from the extra loops are not apparent. */ +#define ROTATE_CHUNK 128 + #define COORD(v) ((v) < 0.0 ? -1 : ((int)(v))) #define FLOOR(v) ((v) < 0.0 ? ((int)floor(v)) : ((int)(v))) /* -------------------------------------------------------------------- */ -/* Transpose operations */ +/* Transpose operations */ Imaging ImagingFlipLeftRight(Imaging imOut, Imaging imIn) @@ -43,25 +50,25 @@ ImagingFlipLeftRight(Imaging imOut, Imaging imIn) int x, y, xr; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) - return (Imaging) ImagingError_Mismatch(); + return (Imaging) ImagingError_Mismatch(); ImagingCopyInfo(imOut, imIn); -#define FLIP_HORIZ(image)\ +#define FLIP_HORIZ(image)\ for (y = 0; y < imIn->ysize; y++) {\ - xr = imIn->xsize-1;\ - for (x = 0; x < imIn->xsize; x++, xr--)\ - imOut->image[y][x] = imIn->image[y][xr];\ + xr = imIn->xsize-1;\ + for (x = 0; x < imIn->xsize; x++, xr--)\ + imOut->image[y][x] = imIn->image[y][xr];\ } ImagingSectionEnter(&cookie); if (imIn->image8) - FLIP_HORIZ(image8) + FLIP_HORIZ(image8) else - FLIP_HORIZ(image32) + FLIP_HORIZ(image32) ImagingSectionLeave(&cookie); @@ -76,9 +83,9 @@ ImagingFlipTopBottom(Imaging imOut, Imaging imIn) int y, yr; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) - return (Imaging) ImagingError_Mismatch(); + return (Imaging) ImagingError_Mismatch(); ImagingCopyInfo(imOut, imIn); @@ -86,7 +93,7 @@ ImagingFlipTopBottom(Imaging imOut, Imaging imIn) yr = imIn->ysize-1; for (y = 0; y < imIn->ysize; y++, yr--) - memcpy(imOut->image[yr], imIn->image[y], imIn->linesize); + memcpy(imOut->image[yr], imIn->image[y], imIn->linesize); ImagingSectionLeave(&cookie); @@ -98,28 +105,35 @@ Imaging ImagingRotate90(Imaging imOut, Imaging imIn) { ImagingSectionCookie cookie; - int x, y, xr; + int x, y, xx, yy, xr, xxsize, yysize; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) - return (Imaging) ImagingError_Mismatch(); + return (Imaging) ImagingError_Mismatch(); ImagingCopyInfo(imOut, imIn); -#define ROTATE_90(image)\ - for (y = 0; y < imIn->ysize; y++) {\ - xr = imIn->xsize-1;\ - for (x = 0; x < imIn->xsize; x++, xr--)\ - imOut->image[xr][y] = imIn->image[y][x];\ +#define ROTATE_90(image) \ + for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \ + for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \ + yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \ + xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \ + for (yy = y; yy < yysize; yy++) { \ + xr = imIn->xsize - 1 - x; \ + for (xx = x; xx < xxsize; xx++, xr--) { \ + imOut->image[xr][yy] = imIn->image[yy][xx]; \ + } \ + } \ + } \ } ImagingSectionEnter(&cookie); if (imIn->image8) - ROTATE_90(image8) + ROTATE_90(image8) else - ROTATE_90(image32) + ROTATE_90(image32) ImagingSectionLeave(&cookie); @@ -127,6 +141,58 @@ ImagingRotate90(Imaging imOut, Imaging imIn) } +Imaging +ImagingTranspose(Imaging imOut, Imaging imIn) +{ + ImagingSectionCookie cookie; + int x, y, xx, yy, xxsize, yysize; + + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + return (Imaging) ImagingError_ModeError(); + if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) + return (Imaging) ImagingError_Mismatch(); + +#define TRANSPOSE(image) \ + for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \ + for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \ + yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \ + xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \ + for (yy = y; yy < yysize; yy++) { \ + for (xx = x; xx < xxsize; xx++) { \ + imOut->image[xx][yy] = imIn->image[yy][xx]; \ + } \ + } \ + } \ + } + + ImagingCopyInfo(imOut, imIn); + + ImagingSectionEnter(&cookie); + if (imIn->image8) + TRANSPOSE(image8) + else + TRANSPOSE(image32) + ImagingSectionLeave(&cookie); + + return imOut; +} + + +Imaging +ImagingTransposeToNew(Imaging imIn) +{ + Imaging imTemp = ImagingNew(imIn->mode, imIn->ysize, imIn->xsize); + if ( ! imTemp) + return NULL; + + if ( ! ImagingTranspose(imTemp, imIn)) { + ImagingDelete(imTemp); + return NULL; + } + return imTemp; +} + + Imaging ImagingRotate180(Imaging imOut, Imaging imIn) { @@ -134,27 +200,27 @@ ImagingRotate180(Imaging imOut, Imaging imIn) int x, y, xr, yr; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) - return (Imaging) ImagingError_Mismatch(); + return (Imaging) ImagingError_Mismatch(); ImagingCopyInfo(imOut, imIn); yr = imIn->ysize-1; -#define ROTATE_180(image)\ +#define ROTATE_180(image)\ for (y = 0; y < imIn->ysize; y++, yr--) {\ - xr = imIn->xsize-1;\ - for (x = 0; x < imIn->xsize; x++, xr--)\ - imOut->image[y][x] = imIn->image[yr][xr];\ + xr = imIn->xsize-1;\ + for (x = 0; x < imIn->xsize; x++, xr--)\ + imOut->image[y][x] = imIn->image[yr][xr];\ } ImagingSectionEnter(&cookie); if (imIn->image8) - ROTATE_180(image8) + ROTATE_180(image8) else - ROTATE_180(image32) + ROTATE_180(image32) ImagingSectionLeave(&cookie); @@ -166,28 +232,35 @@ Imaging ImagingRotate270(Imaging imOut, Imaging imIn) { ImagingSectionCookie cookie; - int x, y, yr; + int x, y, xx, yy, yr, xxsize, yysize; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) - return (Imaging) ImagingError_Mismatch(); + return (Imaging) ImagingError_Mismatch(); ImagingCopyInfo(imOut, imIn); - yr = imIn->ysize - 1; - -#define ROTATE_270(image)\ - for (y = 0; y < imIn->ysize; y++, yr--)\ - for (x = 0; x < imIn->xsize; x++)\ - imOut->image[x][y] = imIn->image[yr][x]; +#define ROTATE_270(image) \ + for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \ + for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \ + yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \ + xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \ + yr = imIn->ysize - 1 - y; \ + for (yy = y; yy < yysize; yy++, yr--) { \ + for (xx = x; xx < xxsize; xx++) { \ + imOut->image[xx][yr] = imIn->image[yy][xx]; \ + } \ + } \ + } \ + } ImagingSectionEnter(&cookie); if (imIn->image8) - ROTATE_270(image8) + ROTATE_270(image8) else - ROTATE_270(image32) + ROTATE_270(image32) ImagingSectionLeave(&cookie); @@ -196,7 +269,7 @@ ImagingRotate270(Imaging imOut, Imaging imIn) /* -------------------------------------------------------------------- */ -/* Transforms */ +/* Transforms */ /* transform primitives (ImagingTransformMap) */ @@ -231,23 +304,6 @@ perspective_transform(double* xin, double* yin, int x, int y, void* data) return 1; } -#if 0 -static int -quadratic_transform(double* xin, double* yin, int x, int y, void* data) -{ - double* a = (double*) data; - - double a0 = a[0]; double a1 = a[1]; double a2 = a[2]; double a3 = a[3]; - double a4 = a[4]; double a5 = a[5]; double a6 = a[6]; double a7 = a[7]; - double a8 = a[8]; double a9 = a[9]; double a10 = a[10]; double a11 = a[11]; - - xin[0] = a0 + a1*x + a2*y + a3*x*x + a4*x*y + a5*y*y; - yin[0] = a6 + a7*x + a8*y + a9*x*x + a10*x*y + a11*y*y; - - return 1; -} -#endif - static int quad_transform(double* xin, double* yin, int x, int y, void* data) { @@ -597,7 +653,7 @@ ImagingTransform( double xx, yy; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); ImagingCopyInfo(imOut, imIn); @@ -613,15 +669,15 @@ ImagingTransform( y1 = imOut->ysize; for (y = y0; y < y1; y++) { - out = imOut->image[y] + x0*imOut->pixelsize; - for (x = x0; x < x1; x++) { - if (!transform(&xx, &yy, x-x0, y-y0, transform_data) || + out = imOut->image[y] + x0*imOut->pixelsize; + for (x = x0; x < x1; x++) { + if (!transform(&xx, &yy, x-x0, y-y0, transform_data) || !filter(out, imIn, xx, yy, filter_data)) { if (fill) memset(out, 0, imOut->pixelsize); } out += imOut->pixelsize; - } + } } ImagingSectionLeave(&cookie); @@ -644,7 +700,7 @@ ImagingScaleAffine(Imaging imOut, Imaging imIn, int *xintab; if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); ImagingCopyInfo(imOut, imIn); @@ -659,8 +715,8 @@ ImagingScaleAffine(Imaging imOut, Imaging imIn, xintab = (int*) malloc(imOut->xsize * sizeof(int)); if (!xintab) { - ImagingDelete(imOut); - return (Imaging) ImagingError_MemoryError(); + ImagingDelete(imOut); + return (Imaging) ImagingError_MemoryError(); } xo = a[0]; @@ -671,29 +727,29 @@ ImagingScaleAffine(Imaging imOut, Imaging imIn, /* Pretabulate horizontal pixel positions */ for (x = x0; x < x1; x++) { - xin = COORD(xo); - if (xin >= 0 && xin < (int) imIn->xsize) { - xmax = x+1; - if (x < xmin) - xmin = x; - xintab[x] = xin; - } - xo += a[1]; + xin = COORD(xo); + if (xin >= 0 && xin < (int) imIn->xsize) { + xmax = x+1; + if (x < xmin) + xmin = x; + xintab[x] = xin; + } + xo += a[1]; } -#define AFFINE_SCALE(pixel, image)\ +#define AFFINE_SCALE(pixel, image)\ for (y = y0; y < y1; y++) {\ - int yi = COORD(yo);\ - pixel *in, *out;\ - out = imOut->image[y];\ + int yi = COORD(yo);\ + pixel *in, *out;\ + out = imOut->image[y];\ if (fill && x1 > x0)\ memset(out+x0, 0, (x1-x0)*sizeof(pixel));\ - if (yi >= 0 && yi < imIn->ysize) {\ - in = imIn->image[yi];\ - for (x = xmin; x < xmax; x++)\ - out[x] = in[xintab[x]];\ - }\ - yo += a[5];\ + if (yi >= 0 && yi < imIn->ysize) {\ + in = imIn->image[yi];\ + for (x = xmin; x < xmax; x++)\ + out[x] = in[xintab[x]];\ + }\ + yo += a[5];\ } ImagingSectionEnter(&cookie); @@ -743,32 +799,32 @@ affine_fixed(Imaging imOut, Imaging imIn, a0 = FIX(a[0]); a1 = FIX(a[1]); a2 = FIX(a[2]); a3 = FIX(a[3]); a4 = FIX(a[4]); a5 = FIX(a[5]); -#define AFFINE_TRANSFORM_FIXED(pixel, image)\ +#define AFFINE_TRANSFORM_FIXED(pixel, image)\ for (y = y0; y < y1; y++) {\ - pixel *out;\ - xx = a0;\ - yy = a3;\ - out = imOut->image[y];\ + pixel *out;\ + xx = a0;\ + yy = a3;\ + out = imOut->image[y];\ if (fill && x1 > x0)\ memset(out+x0, 0, (x1-x0)*sizeof(pixel));\ for (x = x0; x < x1; x++, out++) {\ - xin = xx >> 16;\ - if (xin >= 0 && xin < xsize) {\ - yin = yy >> 16;\ - if (yin >= 0 && yin < ysize)\ + xin = xx >> 16;\ + if (xin >= 0 && xin < xsize) {\ + yin = yy >> 16;\ + if (yin >= 0 && yin < ysize)\ *out = imIn->image[yin][xin];\ }\ - xx += a1;\ - yy += a4;\ - }\ - a0 += a2;\ - a3 += a5;\ + xx += a1;\ + yy += a4;\ + }\ + a0 += a2;\ + a3 += a5;\ } if (imIn->image8) - AFFINE_TRANSFORM_FIXED(UINT8, image8) + AFFINE_TRANSFORM_FIXED(UINT8, image8) else - AFFINE_TRANSFORM_FIXED(INT32, image32) + AFFINE_TRANSFORM_FIXED(INT32, image32) return imOut; } @@ -801,11 +857,11 @@ ImagingTransformAffine(Imaging imOut, Imaging imIn, } if (a[2] == 0 && a[4] == 0) - /* Scaling */ - return ImagingScaleAffine(imOut, imIn, x0, y0, x1, y1, a, fill); + /* Scaling */ + return ImagingScaleAffine(imOut, imIn, x0, y0, x1, y1, a, fill); if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) - return (Imaging) ImagingError_ModeError(); + return (Imaging) ImagingError_ModeError(); if (x0 < 0) x0 = 0; @@ -835,34 +891,34 @@ ImagingTransformAffine(Imaging imOut, Imaging imIn, xo = a[0]; yo = a[3]; -#define AFFINE_TRANSFORM(pixel, image)\ +#define AFFINE_TRANSFORM(pixel, image)\ for (y = y0; y < y1; y++) {\ - pixel *out;\ - xx = xo;\ - yy = yo;\ - out = imOut->image[y];\ + pixel *out;\ + xx = xo;\ + yy = yo;\ + out = imOut->image[y];\ if (fill && x1 > x0)\ memset(out+x0, 0, (x1-x0)*sizeof(pixel));\ for (x = x0; x < x1; x++, out++) {\ - xin = COORD(xx);\ - if (xin >= 0 && xin < xsize) {\ - yin = COORD(yy);\ - if (yin >= 0 && yin < ysize)\ + xin = COORD(xx);\ + if (xin >= 0 && xin < xsize) {\ + yin = COORD(yy);\ + if (yin >= 0 && yin < ysize)\ *out = imIn->image[yin][xin];\ }\ - xx += a[1];\ - yy += a[4];\ - }\ - xo += a[2];\ - yo += a[5];\ + xx += a[1];\ + yy += a[4];\ + }\ + xo += a[2];\ + yo += a[5];\ } ImagingSectionEnter(&cookie); if (imIn->image8) - AFFINE_TRANSFORM(UINT8, image8) + AFFINE_TRANSFORM(UINT8, image8) else - AFFINE_TRANSFORM(INT32, image32) + AFFINE_TRANSFORM(INT32, image32) ImagingSectionLeave(&cookie); @@ -906,30 +962,6 @@ ImagingTransformQuad(Imaging imOut, Imaging imIn, /* -------------------------------------------------------------------- */ /* Convenience functions */ -Imaging -ImagingResize(Imaging imOut, Imaging imIn, int filterid) -{ - double a[6]; - - if (imOut->xsize == imIn->xsize && imOut->ysize == imIn->ysize) - return ImagingCopy2(imOut, imIn); - - memset(a, 0, sizeof a); - a[1] = (double) imIn->xsize / imOut->xsize; - a[5] = (double) imIn->ysize / imOut->ysize; - - if (!filterid && imIn->type != IMAGING_TYPE_SPECIAL) - return ImagingScaleAffine( - imOut, imIn, - 0, 0, imOut->xsize, imOut->ysize, - a, 1); - - return ImagingTransformAffine( - imOut, imIn, - 0, 0, imOut->xsize, imOut->ysize, - a, filterid, 1); -} - Imaging ImagingRotate(Imaging imOut, Imaging imIn, double theta, int filterid) { diff --git a/libImaging/ImPlatform.h b/libImaging/ImPlatform.h index 70ee63119..43c17cc85 100644 --- a/libImaging/ImPlatform.h +++ b/libImaging/ImPlatform.h @@ -72,3 +72,9 @@ #ifdef _MSC_VER typedef signed __int64 int64_t; #endif + +#ifdef __GNUC__ + #define GCC_VERSION (__GNUC__ * 10000 \ + + __GNUC_MINOR__ * 100 \ + + __GNUC_PATCHLEVEL__) +#endif diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index d958387c9..c341ac84e 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -229,7 +229,7 @@ extern void ImagingError_Clear(void); /* standard filters */ #define IMAGING_TRANSFORM_NEAREST 0 -#define IMAGING_TRANSFORM_ANTIALIAS 1 +#define IMAGING_TRANSFORM_LANCZOS 1 #define IMAGING_TRANSFORM_BILINEAR 2 #define IMAGING_TRANSFORM_BICUBIC 3 @@ -263,7 +263,8 @@ extern Imaging ImagingFilter( FLOAT32 offset, FLOAT32 divisor); extern Imaging ImagingFlipLeftRight(Imaging imOut, Imaging imIn); extern Imaging ImagingFlipTopBottom(Imaging imOut, Imaging imIn); -extern Imaging ImagingGaussianBlur(Imaging im, Imaging imOut, float radius); +extern Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius, + int passes); extern Imaging ImagingGetBand(Imaging im, int band); extern int ImagingGetBBox(Imaging im, int bbox[4]); typedef struct { int x, y; INT32 count; INT32 pixel; } ImagingColorItem; @@ -285,13 +286,14 @@ extern Imaging ImagingPointTransform( Imaging imIn, double scale, double offset); extern Imaging ImagingPutBand(Imaging im, Imaging imIn, int band); extern Imaging ImagingRankFilter(Imaging im, int size, int rank); -extern Imaging ImagingResize(Imaging imOut, Imaging imIn, int filter); extern Imaging ImagingRotate( Imaging imOut, Imaging imIn, double theta, int filter); extern Imaging ImagingRotate90(Imaging imOut, Imaging imIn); extern Imaging ImagingRotate180(Imaging imOut, Imaging imIn); extern Imaging ImagingRotate270(Imaging imOut, Imaging imIn); -extern Imaging ImagingStretch(Imaging imOut, Imaging imIn, int filter); +extern Imaging ImagingResample(Imaging imIn, int xsize, int ysize, int filter); +extern Imaging ImagingTranspose(Imaging imOut, Imaging imIn); +extern Imaging ImagingTransposeToNew(Imaging imIn); extern Imaging ImagingTransformPerspective( Imaging imOut, Imaging imIn, int x0, int y0, int x1, int y1, double a[8], int filter, int fill); @@ -307,7 +309,8 @@ extern Imaging ImagingTransform( ImagingTransformFilter filter, void* filter_data, int fill); extern Imaging ImagingUnsharpMask( - Imaging im, Imaging imOut, float radius, int percent, int threshold); + Imaging imOut, Imaging im, float radius, int percent, int threshold); +extern Imaging ImagingBoxBlur(Imaging imOut, Imaging imIn, float radius, int n); extern Imaging ImagingCopy2(Imaging imOut, Imaging imIn); extern Imaging ImagingConvert2(Imaging imOut, Imaging imIn); @@ -525,9 +528,9 @@ enum { extern ImagingIncrementalCodec ImagingIncrementalCodecCreate(ImagingIncrementalCodecEntry codec_entry, Imaging im, ImagingCodecState state, int read_or_write, int seekable, int fd); extern void ImagingIncrementalCodecDestroy(ImagingIncrementalCodec codec); extern int ImagingIncrementalCodecPushBuffer(ImagingIncrementalCodec codec, UINT8 *buf, int bytes); -extern ssize_t ImagingIncrementalCodecRead(ImagingIncrementalCodec codec, void *buffer, size_t bytes); +extern Py_ssize_t ImagingIncrementalCodecRead(ImagingIncrementalCodec codec, void *buffer, size_t bytes); extern off_t ImagingIncrementalCodecSkip(ImagingIncrementalCodec codec, off_t bytes); -extern ssize_t ImagingIncrementalCodecWrite(ImagingIncrementalCodec codec, const void *buffer, size_t bytes); +extern Py_ssize_t ImagingIncrementalCodecWrite(ImagingIncrementalCodec codec, const void *buffer, size_t bytes); extern off_t ImagingIncrementalCodecSeek(ImagingIncrementalCodec codec, off_t bytes); extern size_t ImagingIncrementalCodecBytesInBuffer(ImagingIncrementalCodec codec); diff --git a/libImaging/Incremental.c b/libImaging/Incremental.c index 206c8130b..84e20acf6 100644 --- a/libImaging/Incremental.c +++ b/libImaging/Incremental.c @@ -413,7 +413,7 @@ ImagingIncrementalCodecBytesInBuffer(ImagingIncrementalCodec codec) return codec->stream.ptr - codec->stream.buffer; } -ssize_t +Py_ssize_t ImagingIncrementalCodecRead(ImagingIncrementalCodec codec, void *buffer, size_t bytes) { @@ -428,7 +428,7 @@ ImagingIncrementalCodecRead(ImagingIncrementalCodec codec, DEBUG("reading (want %llu bytes)\n", (unsigned long long)bytes); if (codec->stream.fd >= 0) { - ssize_t ret = read(codec->stream.fd, buffer, bytes); + Py_ssize_t ret = read(codec->stream.fd, buffer, bytes); DEBUG("read %lld bytes from fd\n", (long long)ret); return ret; } @@ -497,7 +497,7 @@ ImagingIncrementalCodecSkip(ImagingIncrementalCodec codec, off_t done = 0; while (bytes) { size_t todo = (size_t)(bytes > 256 ? 256 : bytes); - ssize_t written = ImagingIncrementalCodecWrite(codec, zeroes, todo); + Py_ssize_t written = ImagingIncrementalCodecWrite(codec, zeroes, todo); if (written <= 0) break; done += written; @@ -554,7 +554,7 @@ ImagingIncrementalCodecSkip(ImagingIncrementalCodec codec, return done; } -ssize_t +Py_ssize_t ImagingIncrementalCodecWrite(ImagingIncrementalCodec codec, const void *buffer, size_t bytes) { diff --git a/libImaging/Jpeg2KEncode.c b/libImaging/Jpeg2KEncode.c index ea4bca2f2..868cfdb41 100644 --- a/libImaging/Jpeg2KEncode.c +++ b/libImaging/Jpeg2KEncode.c @@ -257,7 +257,7 @@ j2k_encode_entry(Imaging im, ImagingCodecState state, opj_image_cmptparm_t image_params[4]; unsigned xsiz, ysiz; unsigned tile_width, tile_height; - unsigned tiles_x, tiles_y, num_tiles; + unsigned tiles_x, tiles_y; unsigned x, y, tile_ndx; unsigned n; j2k_pack_tile_t pack; @@ -471,8 +471,6 @@ j2k_encode_entry(Imaging im, ImagingCodecState state, tiles_y = (im->ysize + (params.image_offset_y0 - params.cp_ty0) + tile_height - 1) / tile_height; - num_tiles = tiles_x * tiles_y; - state->buffer = malloc (tile_width * tile_height * components * prec / 8); tile_ndx = 0; diff --git a/libImaging/Pack.c b/libImaging/Pack.c index fecafbde4..d174d7d0d 100644 --- a/libImaging/Pack.c +++ b/libImaging/Pack.c @@ -530,6 +530,11 @@ static struct { {"RGBA", "B", 8, band2}, {"RGBA", "A", 8, band3}, + /* true colour w. alpha premultiplied */ + {"RGBa", "RGBa", 32, copy4}, + {"RGBa", "BGRa", 32, ImagingPackBGRA}, + {"RGBa", "aBGR", 32, ImagingPackABGR}, + /* true colour w. padding */ {"RGBX", "RGBX", 32, copy4}, {"RGBX", "RGBX;L", 32, packRGBXL}, diff --git a/libImaging/QuantHash.c b/libImaging/QuantHash.c index a1f99ed3a..48b7a973e 100644 --- a/libImaging/QuantHash.c +++ b/libImaging/QuantHash.c @@ -44,9 +44,6 @@ struct _HashTable { #define RESIZE_FACTOR 3 static int _hashtable_insert_node(HashTable *,HashNode *,int,int,CollisionFunc); -#if 0 -static int _hashtable_test(HashTable *); -#endif HashTable *hashtable_new(HashFunc hf,HashCmpFunc cf) { HashTable *h; @@ -132,22 +129,6 @@ static void _hashtable_resize(HashTable *h) { } } -#if 0 -static int _hashtable_test(HashTable *h) { - uint32_t i; - int j; - HashNode *n; - for (i=0;ilength;i++) { - for (n=h->table[i];n&&n->next;n=n->next) { - j=h->cmpFunc(h,n->key,n->next->key); - printf ("%c",j?(j<0?'-':'+'):'='); - } - printf ("\n"); - } - return 0; -} -#endif - static int _hashtable_insert_node(HashTable *h,HashNode *node,int resize,int update,CollisionFunc cf) { uint32_t hash=h->hashFunc(h,node->key)%h->length; HashNode **n,*nv; diff --git a/libImaging/QuantOctree.c b/libImaging/QuantOctree.c index b233ccd2e..cd0d7fbd1 100644 --- a/libImaging/QuantOctree.c +++ b/libImaging/QuantOctree.c @@ -345,7 +345,7 @@ int quantize_octree(Pixel *pixelData, /* Create two color cubes, one fine grained with 8x16x8=1024 colors buckets and a coarse with 4x4x4=64 color buckets. - The coarse one guarantes that there are color buckets available for + The coarse one guarantees that there are color buckets available for the whole color range (assuming nQuantPixels > 64). For a quantization to 256 colors all 64 coarse colors will be used @@ -421,7 +421,7 @@ int quantize_octree(Pixel *pixelData, /* add fine colors to the lookup cube */ add_lookup_buckets(lookupCube, paletteBuckets, nFineColors, nCoarseColors); - /* create result pixles and map palatte indices */ + /* create result pixels and map palette indices */ qp = malloc(sizeof(Pixel)*nPixels); if (!qp) goto error; map_image_pixels(pixelData, nPixels, lookupCube, qp); diff --git a/libImaging/Resample.c b/libImaging/Resample.c new file mode 100644 index 000000000..597fca3e9 --- /dev/null +++ b/libImaging/Resample.c @@ -0,0 +1,318 @@ +/* + * The Python Imaging Library + * $Id$ + * + * Pillow image resampling support + * + * history: + * 2002-03-09 fl Created (for PIL 1.1.3) + * 2002-03-10 fl Added support for mode "F" + * + * Copyright (c) 1997-2002 by Secret Labs AB + * + * See the README file for information on usage and redistribution. + */ + +#include "Imaging.h" + +#include + +struct filter { + float (*filter)(float x); + float support; +}; + +static inline float sinc_filter(float x) +{ + if (x == 0.0) + return 1.0; + x = x * M_PI; + return sin(x) / x; +} + +static inline float lanczos_filter(float x) +{ + /* truncated sinc */ + if (-3.0 <= x && x < 3.0) + return sinc_filter(x) * sinc_filter(x/3); + return 0.0; +} + +static struct filter LANCZOS = { lanczos_filter, 3.0 }; + +static inline float bilinear_filter(float x) +{ + if (x < 0.0) + x = -x; + if (x < 1.0) + return 1.0-x; + return 0.0; +} + +static struct filter BILINEAR = { bilinear_filter, 1.0 }; + +static inline float bicubic_filter(float x) +{ + /* http://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm */ +#define a -0.5 + if (x < 0.0) + x = -x; + if (x < 1.0) + return ((a + 2.0) * x - (a + 3.0)) * x*x + 1; + if (x < 2.0) + return (((x - 5) * x + 8) * x - 4) * a; + return 0.0; +#undef a +} + +static struct filter BICUBIC = { bicubic_filter, 2.0 }; + + +static inline UINT8 clip8(float in) +{ + int out = (int) in; + if (out >= 255) + return 255; + if (out <= 0) + return 0; + return (UINT8) out; +} + + +/* This is work around bug in GCC prior 4.9 in 64 bit mode. + GCC generates code with partial dependency which 3 times slower. + See: http://stackoverflow.com/a/26588074/253146 */ +#if defined(__x86_64__) && defined(__SSE__) && ! defined(__NO_INLINE__) && \ + ! defined(__clang__) && defined(GCC_VERSION) && (GCC_VERSION < 40900) +static float __attribute__((always_inline)) i2f(int v) { + float x; + __asm__("xorps %0, %0; cvtsi2ss %1, %0" : "=X"(x) : "r"(v) ); + return x; +} +#else +static float inline i2f(int v) { return (float) v; } +#endif + + +Imaging +ImagingResampleHorizontal(Imaging imIn, int xsize, int filter) +{ + ImagingSectionCookie cookie; + Imaging imOut; + struct filter *filterp; + float support, scale, filterscale; + float center, ww, ss, ss0, ss1, ss2, ss3; + int xx, yy, x, kmax, xmin, xmax; + int *xbounds; + float *k, *kk; + + /* check filter */ + switch (filter) { + case IMAGING_TRANSFORM_LANCZOS: + filterp = &LANCZOS; + break; + case IMAGING_TRANSFORM_BILINEAR: + filterp = &BILINEAR; + break; + case IMAGING_TRANSFORM_BICUBIC: + filterp = &BICUBIC; + break; + default: + return (Imaging) ImagingError_ValueError( + "unsupported resampling filter" + ); + } + + /* prepare for horizontal stretch */ + filterscale = scale = (float) imIn->xsize / xsize; + + /* determine support size (length of resampling filter) */ + support = filterp->support; + + if (filterscale < 1.0) { + filterscale = 1.0; + } + + support = support * filterscale; + + /* maximum number of coofs */ + kmax = (int) ceil(support) * 2 + 1; + + /* coefficient buffer */ + kk = malloc(xsize * kmax * sizeof(float)); + if ( ! kk) + return (Imaging) ImagingError_MemoryError(); + + xbounds = malloc(xsize * 2 * sizeof(int)); + if ( ! xbounds) { + free(kk); + return (Imaging) ImagingError_MemoryError(); + } + + for (xx = 0; xx < xsize; xx++) { + k = &kk[xx * kmax]; + center = (xx + 0.5) * scale; + ww = 0.0; + ss = 1.0 / filterscale; + xmin = (int) floor(center - support); + if (xmin < 0) + xmin = 0; + xmax = (int) ceil(center + support); + if (xmax > imIn->xsize) + xmax = imIn->xsize; + for (x = xmin; x < xmax; x++) { + float w = filterp->filter((x - center + 0.5) * ss) * ss; + k[x - xmin] = w; + ww += w; + } + for (x = 0; x < xmax - xmin; x++) { + if (ww != 0.0) + k[x] /= ww; + } + xbounds[xx * 2 + 0] = xmin; + xbounds[xx * 2 + 1] = xmax; + } + + imOut = ImagingNew(imIn->mode, xsize, imIn->ysize); + if ( ! imOut) { + free(kk); + free(xbounds); + return NULL; + } + + ImagingSectionEnter(&cookie); + /* horizontal stretch */ + for (yy = 0; yy < imOut->ysize; yy++) { + if (imIn->image8) { + /* 8-bit grayscale */ + for (xx = 0; xx < xsize; xx++) { + xmin = xbounds[xx * 2 + 0]; + xmax = xbounds[xx * 2 + 1]; + k = &kk[xx * kmax]; + ss = 0.5; + for (x = xmin; x < xmax; x++) + ss += i2f(imIn->image8[yy][x]) * k[x - xmin]; + imOut->image8[yy][xx] = clip8(ss); + } + } else { + switch(imIn->type) { + case IMAGING_TYPE_UINT8: + /* n-bit grayscale */ + if (imIn->bands == 2) { + for (xx = 0; xx < xsize; xx++) { + xmin = xbounds[xx * 2 + 0]; + xmax = xbounds[xx * 2 + 1]; + k = &kk[xx * kmax]; + ss0 = ss1 = 0.5; + for (x = xmin; x < xmax; x++) { + ss0 += i2f((UINT8) imIn->image[yy][x*4 + 0]) * k[x - xmin]; + ss1 += i2f((UINT8) imIn->image[yy][x*4 + 3]) * k[x - xmin]; + } + imOut->image[yy][xx*4 + 0] = clip8(ss0); + imOut->image[yy][xx*4 + 3] = clip8(ss1); + } + } else if (imIn->bands == 3) { + for (xx = 0; xx < xsize; xx++) { + xmin = xbounds[xx * 2 + 0]; + xmax = xbounds[xx * 2 + 1]; + k = &kk[xx * kmax]; + ss0 = ss1 = ss2 = 0.5; + for (x = xmin; x < xmax; x++) { + ss0 += i2f((UINT8) imIn->image[yy][x*4 + 0]) * k[x - xmin]; + ss1 += i2f((UINT8) imIn->image[yy][x*4 + 1]) * k[x - xmin]; + ss2 += i2f((UINT8) imIn->image[yy][x*4 + 2]) * k[x - xmin]; + } + imOut->image[yy][xx*4 + 0] = clip8(ss0); + imOut->image[yy][xx*4 + 1] = clip8(ss1); + imOut->image[yy][xx*4 + 2] = clip8(ss2); + } + } else { + for (xx = 0; xx < xsize; xx++) { + xmin = xbounds[xx * 2 + 0]; + xmax = xbounds[xx * 2 + 1]; + k = &kk[xx * kmax]; + ss0 = ss1 = ss2 = ss3 = 0.5; + for (x = xmin; x < xmax; x++) { + ss0 += i2f((UINT8) imIn->image[yy][x*4 + 0]) * k[x - xmin]; + ss1 += i2f((UINT8) imIn->image[yy][x*4 + 1]) * k[x - xmin]; + ss2 += i2f((UINT8) imIn->image[yy][x*4 + 2]) * k[x - xmin]; + ss3 += i2f((UINT8) imIn->image[yy][x*4 + 3]) * k[x - xmin]; + } + imOut->image[yy][xx*4 + 0] = clip8(ss0); + imOut->image[yy][xx*4 + 1] = clip8(ss1); + imOut->image[yy][xx*4 + 2] = clip8(ss2); + imOut->image[yy][xx*4 + 3] = clip8(ss3); + } + } + break; + case IMAGING_TYPE_INT32: + /* 32-bit integer */ + for (xx = 0; xx < xsize; xx++) { + xmin = xbounds[xx * 2 + 0]; + xmax = xbounds[xx * 2 + 1]; + k = &kk[xx * kmax]; + ss = 0.0; + for (x = xmin; x < xmax; x++) + ss += i2f(IMAGING_PIXEL_I(imIn, x, yy)) * k[x - xmin]; + IMAGING_PIXEL_I(imOut, xx, yy) = (int) ss; + } + break; + case IMAGING_TYPE_FLOAT32: + /* 32-bit float */ + for (xx = 0; xx < xsize; xx++) { + xmin = xbounds[xx * 2 + 0]; + xmax = xbounds[xx * 2 + 1]; + k = &kk[xx * kmax]; + ss = 0.0; + for (x = xmin; x < xmax; x++) + ss += IMAGING_PIXEL_F(imIn, x, yy) * k[x - xmin]; + IMAGING_PIXEL_F(imOut, xx, yy) = ss; + } + break; + } + } + } + ImagingSectionLeave(&cookie); + free(kk); + free(xbounds); + return imOut; +} + + +Imaging +ImagingResample(Imaging imIn, int xsize, int ysize, int filter) +{ + Imaging imTemp1, imTemp2, imTemp3; + Imaging imOut; + + if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0) + return (Imaging) ImagingError_ModeError(); + + if (imIn->type == IMAGING_TYPE_SPECIAL) + return (Imaging) ImagingError_ModeError(); + + /* two-pass resize, first pass */ + imTemp1 = ImagingResampleHorizontal(imIn, xsize, filter); + if ( ! imTemp1) + return NULL; + + /* transpose image once */ + imTemp2 = ImagingTransposeToNew(imTemp1); + ImagingDelete(imTemp1); + if ( ! imTemp2) + return NULL; + + /* second pass */ + imTemp3 = ImagingResampleHorizontal(imTemp2, ysize, filter); + ImagingDelete(imTemp2); + if ( ! imTemp3) + return NULL; + + /* transpose result */ + imOut = ImagingTransposeToNew(imTemp3); + ImagingDelete(imTemp3); + if ( ! imOut) + return NULL; + + return imOut; +} diff --git a/libImaging/TiffDecode.c b/libImaging/TiffDecode.c index 76bd887a7..25336e7fa 100644 --- a/libImaging/TiffDecode.c +++ b/libImaging/TiffDecode.c @@ -245,7 +245,7 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int // back in. Can't use read encoded stripe. // This thing pretty much requires that I have the whole image in one shot. - // Prehaps a stub version would work better??? + // Perhaps a stub version would work better??? while(state->y < state->ysize){ if (TIFFReadScanline(tiff, (tdata_t)state->buffer, (uint32)state->y, 0) == -1) { TRACE(("Decode Error, row %d\n", state->y)); diff --git a/libImaging/Unpack.c b/libImaging/Unpack.c index 7c453dbfd..522e9b04c 100644 --- a/libImaging/Unpack.c +++ b/libImaging/Unpack.c @@ -638,7 +638,12 @@ unpackRGBa(UINT8* out, const UINT8* in, int pixels) int a = in[3]; if (!a) out[R] = out[G] = out[B] = out[A] = 0; - else { + else if (a == 255) { + out[R] = in[0]; + out[G] = in[1]; + out[B] = in[2]; + out[A] = a; + } else { out[R] = CLIP(in[0] * 255 / a); out[G] = CLIP(in[1] * 255 / a); out[B] = CLIP(in[2] * 255 / a); @@ -803,7 +808,7 @@ unpackI12_I16(UINT8* out, const UINT8* in, int pixels){ FillOrder = 2 should be used only when BitsPerSample = 1 and the data is either uncompressed or compressed using CCITT 1D - or 2D compression, to avoid potentially ambigous situations. + or 2D compression, to avoid potentially ambiguous situations. Yeah. I thought so. We'll see how well people read the spec. We've got several fillorder=2 modes in TiffImagePlugin.py @@ -1114,6 +1119,12 @@ static struct { {"RGBA", "B", 8, band2}, {"RGBA", "A", 8, band3}, + /* true colour w. alpha premultiplied */ + {"RGBa", "RGBa", 32, copy4}, + {"RGBa", "BGRa", 32, unpackBGRA}, + {"RGBa", "aRGB", 32, unpackARGB}, + {"RGBa", "aBGR", 32, unpackABGR}, + /* true colour w. padding */ {"RGBX", "RGB", 24, ImagingUnpackRGB}, {"RGBX", "RGB;L", 24, unpackRGBL}, diff --git a/libImaging/UnsharpMask.c b/libImaging/UnsharpMask.c index 231826245..ec3bb23cc 100644 --- a/libImaging/UnsharpMask.c +++ b/libImaging/UnsharpMask.c @@ -9,387 +9,85 @@ #include "Python.h" #include "Imaging.h" -#define PILUSMVERSION "0.6.1" -/* version history +typedef UINT8 pixel[4]; -0.6.1 converted to C and added to PIL 1.1.7 -0.6.0 fixed/improved float radius support (oops!) - now that radius can be a float (properly), changed radius value to - be an actual radius (instead of diameter). So, you should get - similar results from PIL_usm as from other paint programs when - using the SAME values (no doubling of radius required any more). - Be careful, this may "break" software if you had it set for 2x - or 5x the radius as was recommended with earlier versions. - made PILusm thread-friendly (release GIL before lengthly operations, - and re-acquire it before returning to Python). This makes a huge - difference with multi-threaded applications on dual-processor - or "Hyperthreading"-enabled systems (Pentium4, Xeon, etc.) - -0.5.0 added support for float radius values! - -0.4.0 tweaked gaussian curve calculation to be closer to consistent shape - across a wide range of radius values - -0.3.0 changed deviation calculation in gausian algorithm to be dynamic - _gblur now adds 1 to the user-supplied radius before using it so - that a value of "0" returns the original image instead of a - black one. - fixed handling of alpha channel in RGBX, RGBA images - improved speed of gblur by reducing unnecessary checks and assignments - -0.2.0 fixed L-mode image support - -0.1.0 initial release - -*/ - -static inline UINT8 clip(double in) +static inline UINT8 clip8(int in) { - if (in >= 255.0) - return (UINT8) 255; - if (in <= 0.0) - return (UINT8) 0; + if (in >= 255) + return 255; + if (in <= 0) + return 0; return (UINT8) in; } -static Imaging -gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding) -{ - ImagingSectionCookie cookie; - - float *maskData = NULL; - int y = 0; - int x = 0; - float z = 0; - float sum = 0.0; - float dev = 0.0; - - float *buffer = NULL; - - int *line = NULL; - UINT8 *line8 = NULL; - - int pix = 0; - float newPixel[4]; - int channel = 0; - int offset = 0; - INT32 newPixelFinals; - - int radius = 0; - float remainder = 0.0; - - int i; - - /* Do the gaussian blur */ - - /* For a symmetrical gaussian blur, instead of doing a radius*radius - matrix lookup, you get the EXACT same results by doing a radius*1 - transform, followed by a 1*radius transform. This reduces the - number of lookups exponentially (10 lookups per pixel for a - radius of 5 instead of 25 lookups). So, we blur the lines first, - then we blur the resulting columns. */ - - /* first, round radius off to the next higher integer and hold the - remainder this is used so we can support float radius values - properly. */ - - remainder = floatRadius - ((int) floatRadius); - floatRadius = ceil(floatRadius); - - /* Next, double the radius and offset by 2.0... that way "0" returns - the original image instead of a black one. We multiply it by 2.0 - so that it is a true "radius", not a diameter (the results match - other paint programs closer that way too). */ - radius = (int) ((floatRadius * 2.0) + 2.0); - - /* create the maskData for the gaussian curve */ - maskData = malloc(radius * sizeof(float)); - /* FIXME: error checking */ - for (x = 0; x < radius; x++) { - z = ((float) (x + 2) / ((float) radius)); - dev = 0.5 + (((float) (radius * radius)) * 0.001); - /* you can adjust this factor to change the shape/center-weighting - of the gaussian */ - maskData[x] = (float) pow((1.0 / sqrt(2.0 * 3.14159265359 * dev)), - ((-(z - 1.0) * -(x - 1.0)) / - (2.0 * dev))); - } - - /* if there's any remainder, multiply the first/last values in - MaskData it. this allows us to support float radius values. */ - if (remainder > 0.0) { - maskData[0] *= remainder; - maskData[radius - 1] *= remainder; - } - - for (x = 0; x < radius; x++) { - /* this is done separately now due to the correction for float - radius values above */ - sum += maskData[x]; - } - - for (i = 0; i < radius; i++) { - maskData[i] *= (1.0 / sum); - /* printf("%f\n", maskData[i]); */ - } - - /* create a temporary memory buffer for the data for the first pass - memset the buffer to 0 so we can use it directly with += */ - - /* don't bother about alpha/padding */ - buffer = calloc((size_t) (im->xsize * im->ysize * channels), - sizeof(float)); - if (buffer == NULL) - return ImagingError_MemoryError(); - - /* be nice to other threads while you go off to lala land */ - ImagingSectionEnter(&cookie); - - /* memset(buffer, 0, sizeof(buffer)); */ - - newPixel[0] = newPixel[1] = newPixel[2] = newPixel[3] = 0; - - /* perform a blur on each line, and place in the temporary storage buffer */ - for (y = 0; y < im->ysize; y++) { - if (channels == 1 && im->image8 != NULL) { - line8 = (UINT8 *) im->image8[y]; - } else { - line = im->image32[y]; - } - for (x = 0; x < im->xsize; x++) { - newPixel[0] = newPixel[1] = newPixel[2] = newPixel[3] = 0; - /* for each neighbor pixel, factor in its value/weighting to the - current pixel */ - for (pix = 0; pix < radius; pix++) { - /* figure the offset of this neighbor pixel */ - offset = - (int) ((-((float) radius / 2.0) + (float) pix) + 0.5); - if (x + offset < 0) - offset = -x; - else if (x + offset >= im->xsize) - offset = im->xsize - x - 1; - - /* add (neighbor pixel value * maskData[pix]) to the current - pixel value */ - if (channels == 1) { - buffer[(y * im->xsize) + x] += - ((float) ((UINT8 *) & line8[x + offset])[0]) * - (maskData[pix]); - } else { - for (channel = 0; channel < channels; channel++) { - buffer[(y * im->xsize * channels) + - (x * channels) + channel] += - ((float) ((UINT8 *) & line[x + offset]) - [channel]) * (maskData[pix]); - } - } - } - } - } - - /* perform a blur on each column in the buffer, and place in the - output image */ - for (x = 0; x < im->xsize; x++) { - for (y = 0; y < im->ysize; y++) { - newPixel[0] = newPixel[1] = newPixel[2] = newPixel[3] = 0; - /* for each neighbor pixel, factor in its value/weighting to the - current pixel */ - for (pix = 0; pix < radius; pix++) { - /* figure the offset of this neighbor pixel */ - offset = - (int) (-((float) radius / 2.0) + (float) pix + 0.5); - if (y + offset < 0) - offset = -y; - else if (y + offset >= im->ysize) - offset = im->ysize - y - 1; - /* add (neighbor pixel value * maskData[pix]) to the current - pixel value */ - for (channel = 0; channel < channels; channel++) { - newPixel[channel] += - (buffer - [((y + offset) * im->xsize * channels) + - (x * channels) + channel]) * (maskData[pix]); - } - } - /* if the image is RGBX or RGBA, copy the 4th channel data to - newPixel, so it gets put in imOut */ - if (strcmp(im->mode, "RGBX") == 0 - || strcmp(im->mode, "RGBA") == 0) { - newPixel[3] = (float) ((UINT8 *) & line[x + offset])[3]; - } - - /* pack the channels into an INT32 so we can put them back in - the PIL image */ - newPixelFinals = 0; - if (channels == 1) { - newPixelFinals = clip(newPixel[0]); - } else { - /* for RGB, the fourth channel isn't used anyways, so just - pack a 0 in there, this saves checking the mode for each - pixel. */ - /* this doesn't work on little-endian machines... fix it! */ - newPixelFinals = - clip(newPixel[0]) | clip(newPixel[1]) << 8 | - clip(newPixel[2]) << 16 | clip(newPixel[3]) << 24; - } - /* set the resulting pixel in imOut */ - if (channels == 1) { - imOut->image8[y][x] = (UINT8) newPixelFinals; - } else { - imOut->image32[y][x] = newPixelFinals; - } - } - } - - /* free the buffer */ - free(buffer); - - /* get the GIL back so Python knows who you are */ - ImagingSectionLeave(&cookie); - - return imOut; -} - -Imaging ImagingGaussianBlur(Imaging im, Imaging imOut, float radius) -{ - int channels = 0; - int padding = 0; - - if (strcmp(im->mode, "RGB") == 0) { - channels = 3; - padding = 1; - } else if (strcmp(im->mode, "RGBA") == 0) { - channels = 3; - padding = 1; - } else if (strcmp(im->mode, "RGBX") == 0) { - channels = 3; - padding = 1; - } else if (strcmp(im->mode, "CMYK") == 0) { - channels = 4; - padding = 0; - } else if (strcmp(im->mode, "L") == 0) { - channels = 1; - padding = 0; - } else - return ImagingError_ModeError(); - - return gblur(im, imOut, radius, channels, padding); -} Imaging -ImagingUnsharpMask(Imaging im, Imaging imOut, float radius, int percent, - int threshold) +ImagingUnsharpMask(Imaging imOut, Imaging imIn, float radius, int percent, + int threshold) { ImagingSectionCookie cookie; - Imaging result; - int channel = 0; - int channels = 0; - int padding = 0; - int x = 0; - int y = 0; + int x, y, diff; - int *lineIn = NULL; - int *lineOut = NULL; + pixel *lineIn = NULL; + pixel *lineOut = NULL; UINT8 *lineIn8 = NULL; UINT8 *lineOut8 = NULL; - int diff = 0; - - INT32 newPixel = 0; - - if (strcmp(im->mode, "RGB") == 0) { - channels = 3; - padding = 1; - } else if (strcmp(im->mode, "RGBA") == 0) { - channels = 3; - padding = 1; - } else if (strcmp(im->mode, "RGBX") == 0) { - channels = 3; - padding = 1; - } else if (strcmp(im->mode, "CMYK") == 0) { - channels = 4; - padding = 0; - } else if (strcmp(im->mode, "L") == 0) { - channels = 1; - padding = 0; - } else - return ImagingError_ModeError(); - - /* first, do a gaussian blur on the image, putting results in imOut - temporarily */ - result = gblur(im, imOut, radius, channels, padding); + /* First, do a gaussian blur on the image, putting results in imOut + temporarily. All format checks are in gaussian blur. */ + result = ImagingGaussianBlur(imOut, imIn, radius, 3); if (!result) - return NULL; + return NULL; - /* now, go through each pixel, compare "normal" pixel to blurred - pixel. if the difference is more than threshold values, apply + /* Now, go through each pixel, compare "normal" pixel to blurred + pixel. If the difference is more than threshold values, apply the OPPOSITE correction to the amount of blur, multiplied by percent. */ ImagingSectionEnter(&cookie); - for (y = 0; y < im->ysize; y++) { - if (channels == 1) { - lineIn8 = im->image8[y]; - lineOut8 = imOut->image8[y]; - } else { - lineIn = im->image32[y]; - lineOut = imOut->image32[y]; - } - for (x = 0; x < im->xsize; x++) { - newPixel = 0; - /* compare in/out pixels, apply sharpening */ - if (channels == 1) { - diff = - ((UINT8 *) & lineIn8[x])[0] - - ((UINT8 *) & lineOut8[x])[0]; - if (abs(diff) > threshold) { - /* add the diff*percent to the original pixel */ - imOut->image8[y][x] = - clip((((UINT8 *) & lineIn8[x])[0]) + - (diff * ((float) percent) / 100.0)); - } else { - /* newPixel is the same as imIn */ - imOut->image8[y][x] = ((UINT8 *) & lineIn8[x])[0]; - } - } + for (y = 0; y < imIn->ysize; y++) { + if (imIn->image8) + { + lineIn8 = imIn->image8[y]; + lineOut8 = imOut->image8[y]; + for (x = 0; x < imIn->xsize; x++) { + /* compare in/out pixels, apply sharpening */ + diff = lineIn8[x] - lineOut8[x]; + if (abs(diff) > threshold) { + /* add the diff*percent to the original pixel */ + lineOut8[x] = clip8(lineIn8[x] + diff * percent / 100); + } else { + /* new pixel is the same as imIn */ + lineOut8[x] = lineIn8[x]; + } + } + } else { + lineIn = (pixel *)imIn->image32[y]; + lineOut = (pixel *)imOut->image32[y]; + for (x = 0; x < imIn->xsize; x++) { + /* compare in/out pixels, apply sharpening */ + diff = lineIn[x][0] - lineOut[x][0]; + lineOut[x][0] = abs(diff) > threshold ? + clip8(lineIn[x][0] + diff * percent / 100) : lineIn[x][0]; - else { - for (channel = 0; channel < channels; channel++) { - diff = (int) ((((UINT8 *) & lineIn[x])[channel]) - - (((UINT8 *) & lineOut[x])[channel])); - if (abs(diff) > threshold) { - /* add the diff*percent to the original pixel - this may not work for little-endian systems, fix it! */ - newPixel = - newPixel | - clip((float) (((UINT8 *) & lineIn[x])[channel]) - + - (diff * - (((float) percent / - 100.0)))) << (channel * 8); - } else { - /* newPixel is the same as imIn - this may not work for little-endian systems, fix it! */ - newPixel = - newPixel | ((UINT8 *) & lineIn[x])[channel] << - (channel * 8); - } - } - if (strcmp(im->mode, "RGBX") == 0 - || strcmp(im->mode, "RGBA") == 0) { - /* preserve the alpha channel - this may not work for little-endian systems, fix it! */ - newPixel = - newPixel | ((UINT8 *) & lineIn[x])[channel] << 24; - } - imOut->image32[y][x] = newPixel; - } - } + diff = lineIn[x][1] - lineOut[x][1]; + lineOut[x][1] = abs(diff) > threshold ? + clip8(lineIn[x][1] + diff * percent / 100) : lineIn[x][1]; + + diff = lineIn[x][2] - lineOut[x][2]; + lineOut[x][2] = abs(diff) > threshold ? + clip8(lineIn[x][2] + diff * percent / 100) : lineIn[x][2]; + + diff = lineIn[x][3] - lineOut[x][3]; + lineOut[x][3] = abs(diff) > threshold ? + clip8(lineIn[x][3] + diff * percent / 100) : lineIn[x][3]; + } + } } ImagingSectionLeave(&cookie); diff --git a/mp_compile.py b/mp_compile.py index c8a437400..a930f4245 100644 --- a/mp_compile.py +++ b/mp_compile.py @@ -1,13 +1,15 @@ # A monkey patch of the base distutils.ccompiler to use parallel builds # Tested on 2.7, looks to be identical to 3.3. +from __future__ import print_function from multiprocessing import Pool, cpu_count from distutils.ccompiler import CCompiler -import os, sys +import os +import sys try: - MAX_PROCS = int(os.environ.get('MAX_CONCURRENCY', cpu_count())) -except: + MAX_PROCS = int(os.environ.get('MAX_CONCURRENCY', min(4, cpu_count()))) +except NotImplementedError: MAX_PROCS = None @@ -38,7 +40,7 @@ def _mp_compile(self, sources, output_dir=None, macros=None, pool = Pool(MAX_PROCS) try: - print ("Building using %d processes" % pool._processes) + print("Building using %d processes" % pool._processes) except: pass arr = [(self, obj, build, cc_args, extra_postargs, pp_opts) diff --git a/requirements.txt b/requirements.txt index 86ddbd771..bd37d7ba9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,8 @@ -# Testing reqs +# Testing and documentation requirements -e . +-r docs/requirements.txt +coveralls nose +nose-cov +pep8 +pyflakes diff --git a/selftest.py b/selftest.py index b13dfec28..4ffae3a16 100644 --- a/selftest.py +++ b/selftest.py @@ -9,6 +9,7 @@ if "--installed" in sys.argv: del sys.path[0] from PIL import Image, ImageDraw, ImageFilter, ImageMath +from PIL import features if "--installed" in sys.argv: sys.path.insert(0, sys_path_0) @@ -162,22 +163,6 @@ def testimage(): """ -def check_module(feature, module): - try: - __import__(module) - except ImportError: - print("***", feature, "support not installed") - else: - print("---", feature, "support ok") - - -def check_codec(feature, codec): - if codec + "_encoder" not in dir(Image.core): - print("***", feature, "support not installed") - else: - print("---", feature, "support ok") - - if __name__ == "__main__": # check build sanity @@ -189,23 +174,34 @@ if __name__ == "__main__": print("Python modules loaded from", os.path.dirname(Image.__file__)) print("Binary modules loaded from", os.path.dirname(Image.core.__file__)) print("-"*68) - check_module("PIL CORE", "PIL._imaging") - check_module("TKINTER", "PIL._imagingtk") - check_codec("JPEG", "jpeg") - check_codec("JPEG 2000", "jpeg2k") - check_codec("ZLIB (PNG/ZIP)", "zip") - check_codec("LIBTIFF", "libtiff") - check_module("FREETYPE2", "PIL._imagingft") - check_module("LITTLECMS2", "PIL._imagingcms") - check_module("WEBP", "PIL._webp") - try: - from PIL import _webp - if _webp.WebPDecoderBuggyAlpha(): - print("***", "Transparent WEBP", "support not installed") + for name, feature in [ + ("pil", "PIL CORE"), + ("tkinter", "TKINTER"), + ("freetype2", "FREETYPE2"), + ("littlecms2", "LITTLECMS2"), + ("webp", "WEBP"), + ("transp_webp", "Transparent WEBP") + ]: + supported = features.check_module(name) + + if supported is None: + # A method was being tested, but the module required + # for the method could not be correctly imported + pass + elif supported: + print("---", feature, "support ok") else: - print("---", "Transparent WEBP", "support ok") - except Exception: - pass + print("***", feature, "support not installed") + for name, feature in [ + ("jpg", "JPEG"), + ("jpg_2000", "OPENJPEG (JPEG2000)"), + ("zlib", "ZLIB (PNG/ZIP)"), + ("libtiff", "LIBTIFF") + ]: + if features.check_codec(name): + print("---", feature, "support ok") + else: + print("***", feature, "support not installed") print("-"*68) # use doctest to make sure the test program behaves as documented! diff --git a/setup.py b/setup.py index 2d8cafa34..216322ec2 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ _IMAGING = ( "decode", "encode", "map", "display", "outline", "path") _LIB_IMAGING = ( - "Access", "AlphaComposite", "Antialias", "Bands", "BitDecode", "Blend", + "Access", "AlphaComposite", "Resample", "Bands", "BitDecode", "Blend", "Chops", "Convert", "ConvertYCbCr", "Copy", "Crc32", "Crop", "Dib", "Draw", "Effects", "EpsEncode", "File", "Fill", "Filter", "FliDecode", "Geometry", "GetBBox", "GifDecode", "GifEncode", "HexDecode", @@ -37,7 +37,7 @@ _LIB_IMAGING = ( "RankFilter", "RawDecode", "RawEncode", "Storage", "SunRleDecode", "TgaRleDecode", "Unpack", "UnpackYCC", "UnsharpMask", "XbmDecode", "XbmEncode", "ZipDecode", "ZipEncode", "TiffDecode", "Incremental", - "Jpeg2KDecode", "Jpeg2KEncode") + "Jpeg2KDecode", "Jpeg2KEncode", "BoxBlur") def _add_directory(path, dir, where=None): @@ -90,7 +90,7 @@ except (ImportError, OSError): NAME = 'Pillow' -PILLOW_VERSION = '2.6.0' +PILLOW_VERSION = '2.9.0.dev0' TCL_ROOT = None JPEG_ROOT = None JPEG2K_ROOT = None @@ -570,7 +570,7 @@ class pil_build_ext(build_ext): if feature.webpmux: defs.append(("HAVE_WEBPMUX", None)) libs.append(feature.webpmux) - libs.append(feature.webpmux.replace('pmux','pdemux')) + libs.append(feature.webpmux.replace('pmux', 'pdemux')) exts.append(Extension( "PIL._webp", ["_webp.c"], libraries=libs, define_macros=defs)) @@ -722,12 +722,15 @@ class pil_build_ext(build_ext): os.unlink(tmpfile) +def debug_build(): + return hasattr(sys, 'gettotalrefcount') + setup( name=NAME, version=PILLOW_VERSION, description='Python Imaging Library (Fork)', long_description=_read('README.rst').decode('utf-8'), - author='Alex Clark (fork author)', + author='Alex Clark (Fork Author)', author_email='aclark@aclark.net', url='http://python-pillow.github.io/', classifiers=[ @@ -751,9 +754,9 @@ setup( include_package_data=True, packages=find_packages(), scripts=glob.glob("Scripts/pil*.py"), - test_suite='PIL.tests', + test_suite='nose.collector', keywords=["Imaging", ], license='Standard PIL License', - zip_safe=True, + zip_safe=not debug_build(), ) # End of file diff --git a/tox.ini b/tox.ini index 8d6ea7067..80f7edef4 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py26, py27, py32, py33 +envlist = py26, py27, py32, py33, py34 [testenv] commands =