Commit Graph

174 Commits

Author SHA1 Message Date
Andrew Murray
d9845c14c8 Lint fixes 2019-10-29 22:42:34 +11:00
Jon Dufresne
4cd4adddc3 Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.

To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:

- __del__ isn't called until the object's reference count reaches 0.
  Therefore, resource handlers remain open or in use longer than
  necessary.

- The __del__ method isn't guaranteed to execute on system exit. See the
  Python documentation:

  https://docs.python.org/3/reference/datamodel.html#object.__del__

  > It is not guaranteed that __del__() methods are called for objects
  > that still exist when the interpreter exits.

- Exceptions that occur inside __del__ are ignored instead of raised.
  This has the potential of hiding bugs. This is also in the Python
  documentation:

  > Warning: Due to the precarious circumstances under which __del__()
  > methods are invoked, exceptions that occur during their execution
  > are ignored, and a warning is printed to sys.stderr instead.

Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.

Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.

All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-10-12 08:27:17 -07:00
Hugo
3e24c5fea4 Replace isStringType(t) with isinstance(t, str)
Co-Authored-By: Jon Dufresne <jon.dufresne@gmail.com>
2019-10-08 17:26:52 +03:00
Hugo
e118de943d Remove redundant __ne__ method
Co-Authored-By: Jon Dufresne <jon.dufresne@gmail.com>
2019-10-08 17:26:36 +03:00
Andrew Murray
6cd99fc3cf
Merge branch 'master' into rm-2.7 2019-10-08 18:57:27 +11:00
Jon Dufresne
64032061c0 Move several imports to the top-level of the file
This better follows PEP 8 style guide:

https://www.python.org/dev/peps/pep-0008/#imports

> Imports are always put at the top of the file, just after any module
> comments and docstrings, and before module globals and constants.

This also avoids duplicate import code within the same file.
2019-10-07 06:28:36 -07:00
Hugo
538d9e2e5d Upgrade Python syntax with pyupgrade --py3-plus 2019-10-07 14:30:59 +03:00
Hugo
af770a6c55 Drop support for EOL Python 2.7 2019-10-07 14:30:59 +03:00
Andrew Murray
f61af2fed0
Merge pull request #4107 from hugovk/rm-deprecated-PILLOW_VERSION
Remove deprecated PILLOW_VERSION
2019-10-03 20:52:48 +10:00
Hugo
f898ccbaf8 Remove deprecated PILLOW_VERSION 2019-10-01 14:43:15 +03:00
Andrew Murray
2f5e24da48 Removed warning 2019-09-25 18:52:53 +10:00
Andrew Murray
8f91eff078 Changed default frombuffer raw decoder args 2019-09-25 18:52:07 +10:00
Andrew Murray
595a8fd33c Updated warning to specify time of change 2019-09-24 20:33:02 +10:00
Andrew Murray
82f4a0d8f2 Changed WindowsViewer format to PNG 2019-09-21 09:11:32 +10:00
Andrew Murray
0009646a41 Corrected comment [ci skip] 2019-09-16 21:06:13 +10:00
Andrew Murray
ef16cb8efe ImageFileDirectory_v1 does not raise KeyError 2019-09-07 18:31:23 +10:00
Andrew Murray
f3ed44a566 Changed the Image getexif method to return a shared Exif instance 2019-08-23 06:13:20 +10:00
Andrew Murray
0b405c86be Lazily use ImageFileDirectory_v1 values from Exif 2019-08-18 23:03:43 +10:00
Andrew Murray
75fe92fa88 Updated documentation [ci skip] 2019-08-16 19:16:06 +10:00
Jon Dufresne
d50445ff30 Introduce isort to automate import ordering and formatting
Similar to the recent adoption of Black. isort is a Python utility to
sort imports alphabetically and automatically separate into sections. By
using isort, contributors can quickly and automatically conform to the
projects style without thinking. Just let the tool do it.

Uses the configuration recommended by the Black to avoid conflicts of
style.

Rewrite TestImageQt.test_deprecated to no rely on import order.
2019-07-06 16:11:35 -07:00
Alexander Böhn
7b815a5f1d Added an image.entropy() method
This calculates the entropy for the image, based on the histogram.

Because this uses image histogram data directly, the existing C
function underpinning the `image.histogram()` method was abstracted
into a static function to parse extrema tuple arguments, and a new
C function was added to calculate image entropy, making use of the
new static extrema function.

The extrema-parsing function was written by @homm, based on the
macro abstraction I wrote, during the discussion of my first
entropy-method pull request: https://git.io/fhodS

The new `image.entropy()` method is based on `image.histogram()`,
and will accept the same arguments to calculate the histogram data
it will use to assess the entropy of the image.

The algorithm and methodology is based on existing Python code:

* https://git.io/fhmIU

... A test case in the `Tests/` directory, and doctest lines in
`selftest.py`, have both been added and checked.

Changes proposed in this pull request:

* Added “math.h” include to _imaging.c
* The addition of an `image.entropy()` method to the `Image`
  Python class,
* The abstraction of the extrema-parsing logic of of the C
  function `_histogram` into a static function, and
* The use of that static function in both the `_histogram` and
  `_entropy` C functions.
* Minor documentation addenda in the docstrings for both the
  `image.entropy()` and `image.histogram()` methods were also
  added.
* Removed outdated boilerplate from testing code
* Removed unused “unittest” import
2019-06-26 19:15:47 +10:00
Hugo
cab7231e2a Format with Black 2019-06-11 11:42:05 +03:00
Hugo
169455f924 Refactor and format with Black 2019-06-10 18:22:46 +03:00
Hugo
53a7e35004 Format with Black 2019-06-10 18:22:46 +03:00
Andrew Murray
a94bd13791 Changed string formatting 2019-06-06 21:36:38 +10:00
Andrew Murray
4fa3016d67 Changed string formatting
Co-Authored-By: Hugo <hugovk@users.noreply.github.com>
2019-06-06 21:36:38 +10:00
Andrew Murray
77630f000c Updated resampling filter error messages 2019-06-06 21:36:38 +10:00
Hugo
169961649d
Merge pull request #3848 from radarhere/pa
Improved palette handling for LA and PA modes
2019-06-05 22:32:44 +03:00
Jon Dufresne
3f2cfb2027 Remove unnecessary numeric placeholders from format strings
Unnecessary since Python 2.7.
2019-05-26 07:07:32 -07:00
Andrew Murray
e9c0cf61f3 Fixed palette for LA and PA mode when pickling 2019-05-11 14:43:00 +10:00
Andrew Murray
691df96734 Fixed opening mmap image through Path on Windows 2019-05-04 15:00:49 +10:00
Andrew Murray
40bc46ff49 Moved ImageFile.Exif to Image.Exif 2019-04-01 20:03:02 +11:00
Hugo
4f8cfd536b
Merge pull request #3721 from radarhere/quantize
Update palette in quantize
2019-03-30 12:07:38 +02:00
Andrew Murray
dba38912fb Allow putpalette for LA and PA 2019-03-29 23:13:50 +11:00
Hugo
a93bcdfe0c
Merge pull request #3724 from radarhere/readonly_save
Ensure image is mutable before saving
2019-03-28 11:53:33 +02:00
Hugo
e7c2828b4e
Merge pull request #3740 from radarhere/remap_palette
Corrected remap_palette documentation
2019-03-28 11:47:36 +02:00
Hugo
0572983104
Merge pull request #3726 from radarhere/pa
Promote P images to PA in putalpha
2019-03-28 11:44:52 +02:00
Hugo
adbe97594e
Merge pull request #3719 from radarhere/p_rgb_rgba
Allow RGB and RGBA values for new P images
2019-03-27 18:03:23 +02:00
Andrew Murray
4a5666f1f4 Added transparency for all PNG greyscale modes 2019-03-27 07:41:33 +11:00
Andrew Murray
81cfede546 Corrected remap_palette documentation [ci skip] 2019-03-23 09:36:59 +11:00
Andrew Murray
f61828acdc Promote P images to PA in putalpha 2019-03-19 11:13:58 +11:00
Hugo
b8ea8814f0
Merge pull request #3683 from radarhere/exclusive
Only close original fp in __del__ and __exit__ if original fp is exclusive
2019-03-18 20:37:21 +02:00
Andrew Murray
9bdab56689 Replaced hasattr conditions with getattr and default 2019-03-18 09:15:37 +11:00
Andrew Murray
0b62337b6f Ensure image is mutable before saving 2019-03-17 23:37:40 +11:00
Andrew Murray
fa8ecffff2 Corrected seek documentation 2019-03-17 00:00:10 +11:00
Andrew Murray
5b9c11b426 Update palette in quantize 2019-03-16 13:36:58 +11:00
Andrew Murray
6790c2e375 Allow RGB and RGBA values for new P images 2019-03-15 17:35:05 +11:00
Andrew Murray
22b0110f89 Only close original fp in __del__ and __exit__ if original fp is exclusive 2019-03-12 08:54:43 +11:00
Hugo
125a001b66
Merge pull request #3698 from radarhere/context
Only close exclusive fp on Image __exit__
2019-03-11 21:41:44 +02:00
Hugo
63f31ee380
Merge pull request #3699 from glasnt/topic/quantize-dither
Add option to set dither param on quantize
2019-03-11 12:18:32 +02:00
Andrew Murray
878244bf38 Fixed typo 2019-03-11 11:13:39 +11:00
Katie McLaughlin
0b63579f39 Add option to set dither param on quantize
Default the option to `1`, as per original setting
2019-03-09 10:36:13 +11:00
Andrew Murray
b29365e8a0 Updated list of Unix utilities used to show an image [ci skip] 2019-03-07 15:24:27 +11:00
Hugo
2edab165a1 Since #2527, macOS saves to a temp PNG before showing 2019-03-06 15:57:58 +02:00
Andrew Murray
b1cc7df7ba Only close exclusive fp on Image __exit__ 2019-03-06 21:55:32 +11:00
Frédéric Chapoton
e514fbbbb1
use the already import io
instead of importing again from io (lgtm suggestion)
2019-03-02 21:19:57 +01:00
Andrew Murray
57807e9ce2
Merge pull request #3624 from hugovk/rm-VERSION
Remove deprecated VERSION
2019-03-02 11:47:59 +11:00
Hugo
d9f5f44799
Merge pull request #3632 from radarhere/thumbnail
Do not resize in Image.thumbnail if already the destination size
2019-02-27 23:37:20 +02:00
Andrew Murray
1162b4cf83 Do not resize if already the destination size 2019-02-13 19:26:37 +11:00
Jon Dufresne
dd1e7ccc4a Slightly simplify Image.__eq__
Two identical types can be compared using the `is` operator. Object
identity is slightly faster than a string comparison as well.
2019-02-03 07:13:28 -08:00
Hugo
4d1a459fa7
Merge pull request #3574 from jdufresne/base-exc
Catch BaseException for resource cleanup handlers
2019-02-03 11:35:44 +02:00
Hugo
158d99b8b0 Remove deprecated VERSION 2019-01-30 15:46:14 +02:00
Jon Dufresne
51e95f6252 Catch BaseException for resource cleanup handlers
In the event of a SystemExit or KeyboardInterrupt, file resources should
still be cleaned up before re-raising the exception.

https://docs.python.org/3/library/exceptions.html#exception-hierarchy
2019-01-12 18:32:14 -08:00
Jon Dufresne
3e28ba3b7d Python del is a statement not a function
https://docs.python.org/3/reference/simple_stmts.html#the-del-statement
2019-01-12 18:28:35 -08:00
Andrew Murray
71b64dcf73 Only close __fp if not fp 2019-01-04 12:29:23 +11:00
Andrew Murray
671f7a392d Allow RGBA value for P image putpixel 2018-12-31 13:37:04 +11:00
Andrew Murray
3f6282e259 Allow RGB value for P image putpixel 2018-12-31 11:35:15 +11:00
Andrew Murray
5edeca28b6 Corrected docstring 2018-12-28 07:14:44 +11:00
Hugo
69837baba2
Merge pull request #3490 from radarhere/open_files
Updated open files documentation
2018-12-13 21:14:22 +02:00
Hugo
3ed5dcc928
Merge pull request #3467 from hugovk/lint-can-fail
CI: Allow lint job to fail
2018-12-13 21:09:58 +02:00
Hugo
961fecb35c
Merge pull request #3261 from radarhere/fp
Resolve __fp when closing and deleting
2018-12-13 21:09:23 +02:00
Andrew Murray
91f727051b Updated open files documentation 2018-12-11 14:39:10 +11:00
Andrew Murray
ad26f53bf6 Updated comments 2018-12-05 18:19:00 +11:00
Hugo
c353225851 Prefer 'except Exception:' to bare 'except:' 2018-12-04 23:25:28 +02:00
Andrew Murray
47775f3d50 Resolve __fp when closing and deleting 2018-11-17 21:56:06 +11:00
Hugo
0b7910c7cd flake8: E402 module level import not at top of file 2018-11-11 19:01:10 +02:00
Hugo
91da8ff31d flake8: F401 imported but unused 2018-11-11 19:01:10 +02:00
Andrew Murray
e3220c0f4d Changed capitalisation 2018-11-11 16:00:48 +11:00
Andrew Murray
747eccbfc2 Close exclusive fp on open exception 2018-11-09 11:35:08 +11:00
Andrew Murray
929f7397db Line too long 2018-10-21 18:26:08 +11:00
Andrew Murray
6099ddeef9 Expected 1 blank line before a nested definition 2018-10-21 17:59:18 +11:00
Erich Seifert
9807f1ad56
Fixed documentation on bilevel conversion
The cutoff when converting an image to bilevel (mode "1") is 128. This should resolve issue #3394.
2018-10-05 01:11:24 +02:00
Hugo
2fa54408d4
Merge pull request #3203 from radarhere/size
Changed Image size property to be read-only by default
2018-09-30 14:35:35 +03:00
Hugo
a0fda60662 Use snake_case 2018-09-30 15:34:33 +10:00
Andrew Murray
82d942c3d8 Added warnings if image file identification fails due to lack of support 2018-09-30 15:34:27 +10:00
Andrew Murray
0adeb82e98 Changed Image size property to be read-only by default 2018-09-30 13:45:18 +10:00
Andrew Murray
29bc4b2df4 Improved convert documentation [ci skip] 2018-09-29 20:14:56 +10:00
Hugo
a3f7ce5b73
Merge pull request #3313 from dinkolubina/fix-img-crop
Fix _crop and tests
2018-09-26 16:50:54 +03:00
Hugo
5a8c48c4f3
Merge pull request #3205 from radarhere/transparency
Added transparency to matrix conversion
2018-09-26 15:29:21 +03:00
Hugo
ed4de6cb62
Fix docstring typo
If we `import numpy as np`, use `np` not `numpy`
2018-09-20 15:27:30 +03:00
Hugo
73c305d4d5
Merge pull request #3333 from jdufresne/debug-print
Clean up commented out debug print statements
2018-09-17 11:56:19 +03:00
Giovanni Cavallin
e5160bd373 From KeyError to ValueError when saving image
When saving an image, if the extension is not determined it raises a ValueError (while internally it manages a KeyError) so I propose this change
2018-09-11 19:32:47 +10:00
dinko
66207b47bc fix _crop and tests 2018-09-05 21:00:35 +02:00
Jon Dufresne
37f10651bd Clean up commented out debug print statements 2018-09-04 20:02:42 -07:00
Andrew Murray
e98469ecf6 Added transparency to matrix conversion 2018-08-25 00:54:51 +10:00
Andrew Murray
e3aaa80c06 Added NumPy to documentation 2018-08-12 13:58:26 +10:00
Andrew Murray
4d59941608 Simplified dictionary pop 2018-07-01 14:47:48 +10:00
Alex Clark
9e5494e9a6
Merge pull request #3123 from hugovk/collections.abc-deprecationwarning
Fix collections ABCs DeprecationWarning in Python 3.7
2018-06-30 18:00:10 -04:00
Andrew Murray
d9653a48c7 Added file handling links in documentation 2018-06-30 16:44:59 +10:00
Andrew Murray
7274636a7e Added coordinate system links in documentation 2018-06-24 14:34:01 +10:00