From d2fbc52d6c88fd01fd8283158ddcb0b45a36c4a4 Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Wed, 30 Apr 2014 11:30:44 +0200 Subject: [PATCH 01/14] Change unsigned INT16 to UINT16 Addresses issue #642 --- libImaging/Gif.h | 2 +- libImaging/Lzw.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libImaging/Gif.h b/libImaging/Gif.h index 85d428b0d..2cb95efd2 100644 --- a/libImaging/Gif.h +++ b/libImaging/Gif.h @@ -59,7 +59,7 @@ typedef struct { unsigned char buffer[GIFTABLE]; /* Symbol table */ - unsigned INT16 link[GIFTABLE]; + UINT16 link[GIFTABLE]; unsigned char data[GIFTABLE]; int next; diff --git a/libImaging/Lzw.h b/libImaging/Lzw.h index 7d087ac47..8fc30bd7f 100644 --- a/libImaging/Lzw.h +++ b/libImaging/Lzw.h @@ -45,7 +45,7 @@ typedef struct { unsigned char buffer[LZWTABLE]; /* Symbol table */ - unsigned INT16 link[LZWTABLE]; + UINT16 link[LZWTABLE]; unsigned char data[LZWTABLE]; int next; From 37bb8eec240ebdcdace5b5ddb174c4e69a375aea Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Fri, 9 May 2014 20:14:46 +0200 Subject: [PATCH 02/14] Don't ignore USE_INLINE on Windows --- libImaging/ImPlatform.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libImaging/ImPlatform.h b/libImaging/ImPlatform.h index 7fc5cbdca..81a39b2f7 100644 --- a/libImaging/ImPlatform.h +++ b/libImaging/ImPlatform.h @@ -33,7 +33,8 @@ #if defined(_MSC_VER) #define inline __inline -#elif !defined(USE_INLINE) +#endif +#if !defined(USE_INLINE) #define inline #endif From 052ea606bf9d46fa6dca5e348c766e1c48afa71a Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Fri, 9 May 2014 21:05:30 +0200 Subject: [PATCH 03/14] Clean up defines and includes for Windows 1) Renamed USE_INLINE to PIL_USE_INLINE to avoid conflicts with other headers/libraries. 2) Replace __WIN32__ and WIN32 with _WIN32 3) Don't define WIN32 when the compiler is MSVC but not on Windows Why would you even... 4) Don't define strcasecmp if you're not even going to use it. 5) Don't include Windows.h with undefs for compilers newer than 1998 everywhere. 6) Don't surpress warnings for MSVC++ 4.0. People still using MSVC++ 4.0 deserve it. 7) Don't include things that are already included in Windows.h --- _imaging.c | 6 +++--- _imagingcms.c | 10 ++-------- decode.c | 3 --- display.c | 4 ++-- encode.c | 3 --- libImaging/Dib.c | 4 ++-- libImaging/ImDib.h | 15 ++------------- libImaging/ImPlatform.h | 41 ++++++++++++++++++++-------------------- libImaging/Incremental.c | 1 - map.c | 18 +++--------------- 10 files changed, 34 insertions(+), 71 deletions(-) diff --git a/_imaging.c b/_imaging.c index c47868b81..4f14b7cbd 100644 --- a/_imaging.c +++ b/_imaging.c @@ -3350,7 +3350,7 @@ extern PyObject* PyImaging_ZipEncoderNew(PyObject* self, PyObject* args); extern PyObject* PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args); /* Display support etc (in display.c) */ -#ifdef WIN32 +#ifdef _WIN32 extern PyObject* PyImaging_CreateWindowWin32(PyObject* self, PyObject* args); extern PyObject* PyImaging_DisplayWin32(PyObject* self, PyObject* args); extern PyObject* PyImaging_DisplayModeWin32(PyObject* self, PyObject* args); @@ -3423,14 +3423,14 @@ static PyMethodDef functions[] = { /* Memory mapping */ #ifdef WITH_MAPPING -#ifdef WIN32 +#ifdef _WIN32 {"map", (PyCFunction)PyImaging_Mapper, 1}, #endif {"map_buffer", (PyCFunction)PyImaging_MapBuffer, 1}, #endif /* Display support */ -#ifdef WIN32 +#ifdef _WIN32 {"display", (PyCFunction)PyImaging_DisplayWin32, 1}, {"display_mode", (PyCFunction)PyImaging_DisplayModeWin32, 1}, {"grabscreen", (PyCFunction)PyImaging_GrabScreenWin32, 1}, diff --git a/_imagingcms.c b/_imagingcms.c index 99da647ae..df26e1a2d 100644 --- a/_imagingcms.c +++ b/_imagingcms.c @@ -28,12 +28,6 @@ http://www.cazabon.com\n\ #include "Imaging.h" #include "py3.h" -#ifdef WIN32 -#include -#include -#include -#endif - #define PYCMSVERSION "1.0.0 pil" /* version history */ @@ -450,7 +444,7 @@ cms_profile_is_intent_supported(CmsProfileObject *self, PyObject *args) return PyInt_FromLong(result != 0); } -#ifdef WIN32 +#ifdef _WIN32 static PyObject * cms_get_display_profile_win32(PyObject* self, PyObject* args) { @@ -496,7 +490,7 @@ static PyMethodDef pyCMSdll_methods[] = { {"createProfile", createProfile, 1}, /* platform specific tools */ -#ifdef WIN32 +#ifdef _WIN32 {"get_display_profile_win32", cms_get_display_profile_win32, 1}, #endif diff --git a/decode.c b/decode.c index 77038cc2c..33367dfe3 100644 --- a/decode.c +++ b/decode.c @@ -433,9 +433,6 @@ PyImaging_TiffLzwDecoderNew(PyObject* self, PyObject* args) #include "TiffDecode.h" #include -#ifdef __WIN32__ -#define strcasecmp(s1, s2) stricmp(s1, s2) -#endif PyObject* PyImaging_LibTiffDecoderNew(PyObject* self, PyObject* args) diff --git a/display.c b/display.c index 9cc1ae3ad..e10d72c0d 100644 --- a/display.c +++ b/display.c @@ -31,7 +31,7 @@ /* -------------------------------------------------------------------- */ /* Windows DIB support */ -#ifdef WIN32 +#ifdef _WIN32 #include "ImDib.h" @@ -864,4 +864,4 @@ error: return buffer; } -#endif /* WIN32 */ +#endif /* _WIN32 */ diff --git a/encode.c b/encode.c index d403ab072..b14942376 100644 --- a/encode.c +++ b/encode.c @@ -670,9 +670,6 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) #include "TiffDecode.h" #include -#ifdef __WIN32__ -#define strcasecmp(s1, s2) stricmp(s1, s2) -#endif PyObject* PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args) diff --git a/libImaging/Dib.c b/libImaging/Dib.c index 6db8c076e..8e138bd6b 100644 --- a/libImaging/Dib.c +++ b/libImaging/Dib.c @@ -22,7 +22,7 @@ #include "Imaging.h" -#ifdef WIN32 +#ifdef _WIN32 #include "ImDib.h" @@ -308,4 +308,4 @@ ImagingDeleteDIB(ImagingDIB dib) free(dib->info); } -#endif /* WIN32 */ +#endif /* _WIN32 */ diff --git a/libImaging/ImDib.h b/libImaging/ImDib.h index 23c660488..55ecb35aa 100644 --- a/libImaging/ImDib.h +++ b/libImaging/ImDib.h @@ -10,20 +10,9 @@ * See the README file for information on usage and redistribution. */ -#ifdef WIN32 +#ifdef _WIN32 -#if (defined(_MSC_VER) && _MSC_VER >= 1200) || (defined __GNUC__) -/* already defined in basetsd.h */ -#undef INT8 -#undef UINT8 -#undef INT16 -#undef UINT16 -#undef INT32 -#undef INT64 -#undef UINT32 -#endif - -#include +#include "ImPlatform.h" #if defined(__cplusplus) extern "C" { diff --git a/libImaging/ImPlatform.h b/libImaging/ImPlatform.h index 81a39b2f7..92c126a10 100644 --- a/libImaging/ImPlatform.h +++ b/libImaging/ImPlatform.h @@ -17,27 +17,22 @@ #error Sorry, this library requires ANSI header files. #endif -#if defined(_MSC_VER) -#ifndef WIN32 -#define WIN32 -#endif -/* VC++ 4.0 is a bit annoying when it comes to precision issues (like - claiming that "float a = 0.0;" would lead to loss of precision). I - don't like to see warnings from my code, but since I still want to - keep it readable, I simply switch off a few warnings instead of adding - the tons of casts that VC++ seem to require. This code is compiled - with numerous other compilers as well, so any real errors are likely - to be catched anyway. */ -#pragma warning(disable: 4244) /* conversion from 'float' to 'int' */ -#endif - -#if defined(_MSC_VER) +#if defined(_MSC_VER) && !defined(__GNUC__) #define inline __inline #endif -#if !defined(USE_INLINE) -#define inline + +#if !defined(PIL_USE_INLINE) +#define inline #endif +#ifdef _WIN32 + +#define WIN32_LEAN_AND_MEAN +#include + +#else +/* For System that are not Windows, we'll need to define these. */ + #if SIZEOF_SHORT == 2 #define INT16 short #elif SIZEOF_INT == 2 @@ -62,12 +57,16 @@ #define INT64 long #endif -/* assume IEEE; tweak if necessary (patches are welcome) */ -#define FLOAT32 float -#define FLOAT64 double - #define INT8 signed char #define UINT8 unsigned char #define UINT16 unsigned INT16 #define UINT32 unsigned INT32 + +#endif + +/* assume IEEE; tweak if necessary (patches are welcome) */ +#define FLOAT32 float +#define FLOAT64 double + + diff --git a/libImaging/Incremental.c b/libImaging/Incremental.c index 9e7fb38ec..206c8130b 100644 --- a/libImaging/Incremental.c +++ b/libImaging/Incremental.c @@ -41,7 +41,6 @@ two cases. */ #ifdef _WIN32 -#include #include #else #include diff --git a/map.c b/map.c index 95d5d1d35..dc9ead0aa 100644 --- a/map.c +++ b/map.c @@ -22,18 +22,6 @@ #include "Imaging.h" -#ifdef WIN32 -#define WIN32_LEAN_AND_MEAN -#undef INT8 -#undef UINT8 -#undef INT16 -#undef UINT16 -#undef INT32 -#undef INT64 -#undef UINT32 -#include "windows.h" -#endif - #include "py3.h" /* compatibility wrappers (defined in _imaging.c) */ @@ -48,7 +36,7 @@ typedef struct { char* base; int size; int offset; -#ifdef WIN32 +#ifdef _WIN32 HANDLE hFile; HANDLE hMap; #endif @@ -71,7 +59,7 @@ PyImaging_MapperNew(const char* filename, int readonly) mapper->base = NULL; mapper->size = mapper->offset = 0; -#ifdef WIN32 +#ifdef _WIN32 mapper->hFile = (HANDLE)-1; mapper->hMap = (HANDLE)-1; @@ -114,7 +102,7 @@ PyImaging_MapperNew(const char* filename, int readonly) static void mapping_dealloc(ImagingMapperObject* mapper) { -#ifdef WIN32 +#ifdef _WIN32 if (mapper->base != 0) UnmapViewOfFile(mapper->base); if (mapper->hMap != (HANDLE)-1) From 29ddeaa81a0de6a488355bca55dd42a6b28259ff Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Sat, 10 May 2014 10:11:04 -0500 Subject: [PATCH 04/14] Adjust Homebrew freetype detection logic XQuartz ships an older freetype that still has a top-level "ft2build.h" header file. Homebrew's freetype is newer and does not have this file, it only has "freetype2/ft2build.h". setup.py finds the header in XQuartz first, but Homebrew's compiler wrappers intentionally strip out the XQuartz include paths during the build unless the package depends on it explicitly. We want to prefer Homebrew's freetype anyway, so if it's installed, let's not even bother to search the XQuartz paths. --- setup.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 93918debd..50ca985e3 100644 --- a/setup.py +++ b/setup.py @@ -205,25 +205,31 @@ class pil_build_ext(build_ext): # darwin ports installation directories _add_directory(library_dirs, "/opt/local/lib") _add_directory(include_dirs, "/opt/local/include") - - # if homebrew is installed, use its lib and include directories + + # if Homebrew is installed, use its lib and include directories import subprocess try: - prefix = subprocess.check_output(['brew', '--prefix']) - if prefix: - prefix = prefix.strip() - _add_directory(library_dirs, os.path.join(prefix, 'lib')) - _add_directory(include_dirs, os.path.join(prefix, 'include')) - - # freetype2 is a key-only brew under opt/ - _add_directory(library_dirs, os.path.join(prefix, 'opt', 'freetype', 'lib')) - _add_directory(include_dirs, os.path.join(prefix, 'opt', 'freetype', 'include')) + prefix = subprocess.check_output(['brew', '--prefix']).strip() except: - pass # homebrew not installed - - # freetype2 ships with X11 (after homebrew, so that homebrew freetype is preferred) - _add_directory(library_dirs, "/usr/X11/lib") - _add_directory(include_dirs, "/usr/X11/include") + # Homebrew not installed + prefix = None + + ft_prefix = None + + if prefix: + # add Homebrew's include and lib directories + _add_directory(library_dirs, os.path.join(prefix, 'lib')) + _add_directory(include_dirs, os.path.join(prefix, 'include')) + ft_prefix = os.path.join(prefix, 'opt', 'freetype') + + if ft_prefix and os.path.isdir(ft_prefix): + # freetype might not be linked into Homebrew's prefix + _add_directory(library_dirs, os.path.join(ft_prefix, 'lib')) + _add_directory(include_dirs, os.path.join(ft_prefix, 'include')) + else: + # fall back to freetype from XQuartz if Homebrew's freetype is missing + _add_directory(library_dirs, "/usr/X11/lib") + _add_directory(include_dirs, "/usr/X11/include") elif sys.platform.startswith("linux"): arch_tp = (plat.processor(), plat.architecture()[0]) From 84a701a82b33896a4d6997743c2131ab0a40c588 Mon Sep 17 00:00:00 2001 From: Alex Clark Date: Sat, 10 May 2014 13:29:36 -0400 Subject: [PATCH 05/14] Update --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index c774a6ddf..81e388cda 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ Changelog (Pillow) 2.5.0 (unreleased) ------------------ +- Adjust Homebrew freetype detection logic + [jacknagel] + - Added Image.close, context manager support. [wiredfool] From 41d3c386a983f3ea716c4a06f83a919485d22615 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 14 May 2014 08:17:49 +0300 Subject: [PATCH 06/14] Don't cover PyPy: it fails intermittently and is x5.8 slower (#640) --- .travis.yml | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1ae6e415c..96c10d0ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,12 +4,12 @@ notifications: irc: "chat.freenode.net#pil" python: - - "pypy" - 2.6 - 2.7 - 3.2 - 3.3 - 3.4 + - "pypy" install: - "sudo apt-get -qq install libfreetype6-dev liblcms2-dev python-qt4 ghostscript libffi-dev cmake" @@ -26,8 +26,14 @@ script: - coverage erase - python setup.py clean - python setup.py build_ext --inplace - - coverage run --append --include=PIL/* selftest.py - - python Tests/run.py --coverage + + # Don't cover PyPy: it fails intermittently and is x5.8 slower (#640) + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python selftest.py; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "pypy" ]; then python Tests/run.py; fi + + # Cover the others + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then coverage run --append --include=PIL/* selftest.py; fi + - if [ "$TRAVIS_PYTHON_VERSION" != "pypy" ]; then python Tests/run.py --coverage; fi after_success: - coverage report @@ -37,7 +43,3 @@ after_success: - pyflakes PIL/*.py - pep8 Tests/*.py - pyflakes Tests/*.py - -matrix: - allow_failures: - - python: "pypy" From 6ca9e9d70383a5be60a69387f99bb08b6847e280 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 14 May 2014 08:47:13 +0300 Subject: [PATCH 07/14] Keep PyPy first, it's still a little slower --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 96c10d0ae..36dae5b7a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,12 +4,12 @@ notifications: irc: "chat.freenode.net#pil" python: + - "pypy" - 2.6 - 2.7 - 3.2 - 3.3 - 3.4 - - "pypy" install: - "sudo apt-get -qq install libfreetype6-dev liblcms2-dev python-qt4 ghostscript libffi-dev cmake" From fc55580a69a56974ee0dc7929834b4cbc3a8f83a Mon Sep 17 00:00:00 2001 From: Alex Clark Date: Wed, 14 May 2014 05:59:48 -0400 Subject: [PATCH 08/14] Update --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 81e388cda..b6eb19b3a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ Changelog (Pillow) 2.5.0 (unreleased) ------------------ +- Clean up preprocessor cruft for Windows + [CounterPillow] + - Adjust Homebrew freetype detection logic [jacknagel] From 55b1accb42a5b2d4e1372546629ff06024863915 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 17 May 2014 19:08:08 +0300 Subject: [PATCH 09/14] Add (failing) test for #664 --- Tests/test_image_convert.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 4d40e43b2..6f170afe2 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -45,7 +45,7 @@ def test_16bit(): def test_16bit_workaround(): im = Image.open('Tests/images/16bit.cropped.tif') _test_float_conversion(im.convert('I')) - + def test_rgba_p(): im = lena('RGBA') im.putalpha(lena('L')) @@ -54,8 +54,8 @@ def test_rgba_p(): comparable = converted.convert('RGBA') assert_image_similar(im, comparable, 20) - -def test_trns_p(): + +def test_trns_p(): im = lena('P') im.info['transparency']=0 @@ -69,13 +69,26 @@ def test_trns_p(): rgb = im.convert('RGB') assert_equal(rgb.info['transparency'], (0,0,0)) # undone assert_no_exception(lambda: rgb.save(f)) - + + +def test_trns_p_rgba(): + # Arrange + im = lena('P') + im.info['transparency'] = 128 + + # Act + rgba = im.convert('RGBA') + + # Assert + assert_false('transparency' in rgba.info) + + def test_trns_l(): im = lena('L') im.info['transparency'] = 128 f = tempfile('temp.png') - + rgb = im.convert('RGB') assert_equal(rgb.info['transparency'], (128,128,128)) # undone assert_no_exception(lambda: rgb.save(f)) @@ -89,13 +102,13 @@ def test_trns_l(): assert_false('transparency' in p.info) assert_no_exception(lambda: p.save(f)) - + def test_trns_RGB(): im = lena('RGB') im.info['transparency'] = im.getpixel((0,0)) f = tempfile('temp.png') - + l = im.convert('L') assert_equal(l.info['transparency'], l.getpixel((0,0))) # undone assert_no_exception(lambda: l.save(f)) @@ -103,7 +116,7 @@ def test_trns_RGB(): p = im.convert('P') assert_true('transparency' in p.info) assert_no_exception(lambda: p.save(f)) - + p = assert_warning(UserWarning, lambda: im.convert('P', palette = Image.ADAPTIVE)) assert_false('transparency' in p.info) From ca7608f1d5a0d4322d185da6607fa1db2b418c0f Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 17 May 2014 20:36:17 +0300 Subject: [PATCH 10/14] Possible fix for #664 --- PIL/Image.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 333397701..ec25f1027 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -101,7 +101,7 @@ import collections import numbers # works everywhere, win for pypy, not cpython -USE_CFFI_ACCESS = hasattr(sys, 'pypy_version_info') +USE_CFFI_ACCESS = hasattr(sys, 'pypy_version_info') try: import cffi HAS_CFFI=True @@ -233,7 +233,7 @@ _MODE_CONV = { "CMYK": ('|u1', 4), "YCbCr": ('|u1', 3), "LAB": ('|u1', 3), # UNDONE - unsigned |u1i1i1 - # I;16 == I;16L, and I;32 == I;32L + # I;16 == I;16L, and I;32 == I;32L "I;16": ('u2', None), "I;16L": (' 8bit images. + # a gamma function point transform on > 8bit images. scale, offset = _getscaleoffset(lut) return self._new(self.im.point_transform(scale, offset)) # for other modes, convert the function to a table @@ -1420,8 +1423,8 @@ class Image: self._copy() self.pyaccess = None self.load() - - if self.pyaccess: + + if self.pyaccess: return self.pyaccess.putpixel(xy,value) return self.im.putpixel(xy, value) From a59bc40c38661c92748d8be93ca2e03bdb01f613 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 17 May 2014 23:33:50 +0300 Subject: [PATCH 11/14] flake8 --- Tests/test_image_convert.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 6f170afe2..6a39b0e3b 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -2,6 +2,7 @@ from tester import * from PIL import Image + def test_sanity(): def convert(im, mode): @@ -16,6 +17,7 @@ def test_sanity(): for mode in modes: yield_test(convert, im, mode) + def test_default(): im = lena("P") @@ -26,26 +28,29 @@ def test_default(): assert_image(im, "RGB", im.size) - # ref https://github.com/python-imaging/Pillow/issues/274 def _test_float_conversion(im): - orig = im.getpixel((5,5)) - converted = im.convert('F').getpixel((5,5)) + orig = im.getpixel((5, 5)) + converted = im.convert('F').getpixel((5, 5)) assert_equal(orig, converted) + def test_8bit(): im = Image.open('Images/lena.jpg') _test_float_conversion(im.convert('L')) + def test_16bit(): im = Image.open('Tests/images/16bit.cropped.tif') _test_float_conversion(im) + def test_16bit_workaround(): im = Image.open('Tests/images/16bit.cropped.tif') _test_float_conversion(im.convert('I')) + def test_rgba_p(): im = lena('RGBA') im.putalpha(lena('L')) @@ -55,22 +60,24 @@ def test_rgba_p(): assert_image_similar(im, comparable, 20) + def test_trns_p(): im = lena('P') - im.info['transparency']=0 + im.info['transparency'] = 0 f = tempfile('temp.png') l = im.convert('L') - assert_equal(l.info['transparency'], 0) # undone + assert_equal(l.info['transparency'], 0) # undone assert_no_exception(lambda: l.save(f)) - rgb = im.convert('RGB') - assert_equal(rgb.info['transparency'], (0,0,0)) # undone + assert_equal(rgb.info['transparency'], (0, 0, 0)) # undone assert_no_exception(lambda: rgb.save(f)) +# ref https://github.com/python-imaging/Pillow/issues/664 + def test_trns_p_rgba(): # Arrange im = lena('P') @@ -90,7 +97,7 @@ def test_trns_l(): f = tempfile('temp.png') rgb = im.convert('RGB') - assert_equal(rgb.info['transparency'], (128,128,128)) # undone + assert_equal(rgb.info['transparency'], (128, 128, 128)) # undone assert_no_exception(lambda: rgb.save(f)) p = im.convert('P') @@ -98,19 +105,19 @@ def test_trns_l(): assert_no_exception(lambda: p.save(f)) p = assert_warning(UserWarning, - lambda: im.convert('P', palette = Image.ADAPTIVE)) + lambda: im.convert('P', palette=Image.ADAPTIVE)) assert_false('transparency' in p.info) assert_no_exception(lambda: p.save(f)) def test_trns_RGB(): im = lena('RGB') - im.info['transparency'] = im.getpixel((0,0)) + im.info['transparency'] = im.getpixel((0, 0)) f = tempfile('temp.png') l = im.convert('L') - assert_equal(l.info['transparency'], l.getpixel((0,0))) # undone + assert_equal(l.info['transparency'], l.getpixel((0, 0))) # undone assert_no_exception(lambda: l.save(f)) p = im.convert('P') @@ -118,8 +125,6 @@ def test_trns_RGB(): assert_no_exception(lambda: p.save(f)) p = assert_warning(UserWarning, - lambda: im.convert('P', palette = Image.ADAPTIVE)) + lambda: im.convert('P', palette=Image.ADAPTIVE)) assert_false('transparency' in p.info) assert_no_exception(lambda: p.save(f)) - - From 0938145e57e6d4e838f02495da80e03fd2a4cbeb Mon Sep 17 00:00:00 2001 From: hugovk Date: Mon, 19 May 2014 10:04:56 +0300 Subject: [PATCH 12/14] Move to an elif clause of the if in line 778, as it's just another combination of mode,self.mode under the master transparency condition --- PIL/Image.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index ec25f1027..5d23a278a 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -802,7 +802,8 @@ class Image: # after quantization. trns_im = trns_im.convert('RGB') trns = trns_im.getpixel((0,0)) - + elif self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info: + delete_trns = True if mode == "P" and palette == ADAPTIVE: im = self.im.quantize(colors) @@ -837,9 +838,6 @@ class Image: except KeyError: raise ValueError("illegal conversion") - if self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info: - delete_trns = True - new_im = self._new(im) if delete_trns: #crash fail if we leave a bytes transparency in an rgb/l mode. From 88d74883bb507ca42a5f203e46b0ada8a47fc1cc Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 19 May 2014 14:12:43 -0700 Subject: [PATCH 13/14] remove redundant transparency check --- PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index 5d23a278a..08b0dbe7d 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -802,7 +802,7 @@ class Image: # after quantization. trns_im = trns_im.convert('RGB') trns = trns_im.getpixel((0,0)) - elif self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info: + elif self.mode == 'P' and mode == 'RGBA': delete_trns = True if mode == "P" and palette == ADAPTIVE: From a7cb81ec56107234b012e2f2859a53122045f77b Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 20 May 2014 16:42:18 +0100 Subject: [PATCH 14/14] Update CHANGES.rst --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index b6eb19b3a..8121a9519 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ Changelog (Pillow) 2.5.0 (unreleased) ------------------ +- Remove transparency resource after P->RGBA conversion + [hugovk] + - Clean up preprocessor cruft for Windows [CounterPillow]