Skip to content

Commit

Permalink
Possible fix for python-pillow#664
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed May 17, 2014
1 parent 55b1acc commit ca7608f
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __getattr__(self, id):
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
Expand Down Expand Up @@ -233,7 +233,7 @@ def isImageType(t):
"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;16B": ('>u2', None),
"I;16L": ('<u2', None),
Expand Down Expand Up @@ -502,7 +502,7 @@ def __enter__(self):
return self
def __exit__(self, *args):
self.close()

def close(self):
"""
Closes the file pointer, if possible.
Expand All @@ -524,7 +524,7 @@ def close(self):
# deferred error that will better explain that the core image
# object is gone.
self.im = deferred_error(ValueError("Operation on closed image"))


def _copy(self):
self.load()
Expand All @@ -540,7 +540,7 @@ def _dump(self, file=None, format=None):
if not file:
f, file = tempfile.mkstemp(suffix)
os.close(f)

self.load()
if not format or format == "PPM":
self.im.save_ppm(file)
Expand Down Expand Up @@ -672,7 +672,7 @@ def load(self):
normal cases, you don't need to call this method, since the
Image class automatically loads an opened image when it is
accessed for the first time. This method will close the file
associated with the image.
associated with the image.
:returns: An image access object.
"""
Expand Down Expand Up @@ -777,7 +777,7 @@ def convert(self, mode=None, matrix=None, dither=None,
if "transparency" in self.info and self.info['transparency'] is not None:
if self.mode in ('L', 'RGB') and mode == 'RGBA':
# Use transparent conversion to promote from transparent
# color to an alpha channel.
# color to an alpha channel.
return self._new(self.im.convert_transparent(
mode, self.info['transparency']))
elif self.mode in ('L', 'RGB', 'P') and mode in ('L', 'RGB', 'P'):
Expand All @@ -799,19 +799,19 @@ def convert(self, mode=None, matrix=None, dither=None,
trns_im = trns_im.convert(mode)
else:
# can't just retrieve the palette number, got to do it
# after quantization.
# after quantization.
trns_im = trns_im.convert('RGB')
trns = trns_im.getpixel((0,0))


if mode == "P" and palette == ADAPTIVE:
im = self.im.quantize(colors)
new = self._new(im)
from PIL import ImagePalette
new.palette = ImagePalette.raw("RGB", new.im.getpalette("RGB"))
if delete_trns:
# This could possibly happen if we requantize to fewer colors.
# The transparency would be totally off in that case.
# The transparency would be totally off in that case.
del(new.info['transparency'])
if trns is not None:
try:
Expand All @@ -826,7 +826,7 @@ def convert(self, mode=None, matrix=None, dither=None,
# colorspace conversion
if dither is None:
dither = FLOYDSTEINBERG

try:
im = self.im.convert(mode, dither)
except ValueError:
Expand All @@ -837,6 +837,9 @@ def convert(self, mode=None, matrix=None, dither=None,
except KeyError:
raise ValueError("illegal conversion")

if self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info:

This comment has been minimized.

Copy link
@wiredfool

wiredfool May 18, 2014

I'd put this as an elif clause of the if in line 778, as it's just another combination of mode,self.mode under the master transparency condition.

delete_trns = True

new_im = self._new(im)
if delete_trns:
#crash fail if we leave a bytes transparency in an rgb/l mode.
Expand All @@ -863,18 +866,18 @@ def quantize(self, colors=256, method=None, kmeans=0, palette=None):
# quantizer interface in a later version of PIL.

self.load()

if method is None:
# defaults:
method = 0
if self.mode == 'RGBA':
method = 2

if self.mode == 'RGBA' and method != 2:
# Caller specified an invalid mode.
# Caller specified an invalid mode.
raise ValueError('Fast Octree (method == 2) is the ' +
' only valid method for quantizing RGBA images')

if palette:
# use palette from reference image
palette.load()
Expand Down Expand Up @@ -928,7 +931,7 @@ def crop(self, box=None):
def draft(self, mode, size):
"""
NYI
Configures the image file loader so it returns a version of the
image that as closely as possible matches the given mode and
size. For example, you can use this method to convert a color
Expand Down Expand Up @@ -1277,7 +1280,7 @@ def point(self, lut, mode=None):
if self.mode in ("I", "I;16", "F"):
# check if the function can be used with point_transform
# UNDONE wiredfool -- I think this prevents us from ever doing
# a gamma function point transform on > 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
Expand Down Expand Up @@ -1420,8 +1423,8 @@ def putpixel(self, xy, value):
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)

Expand Down

0 comments on commit ca7608f

Please sign in to comment.