Skip to content

Commit

Permalink
Merge remote-tracking branch 'pil/master' into tiff-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
olt committed Jun 12, 2019
2 parents 4c0fbba + a986fed commit ea4ea0d
Show file tree
Hide file tree
Showing 104 changed files with 3,358 additions and 2,739 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Expand Up @@ -53,7 +53,7 @@ Changelog (Pillow)
- Travis CI: Upgrade PyPy from 6.0.0 to 7.1.1 #3783
[hugovk, johnthagen]

- Depends: Updated openjpeg to 2.3.1 #3794, raqm to 0.7.0 #3877
- Depends: Updated openjpeg to 2.3.1 #3794, raqm to 0.7.0 #3877, libimagequant to 2.12.3 #3889
[radarhere]

- Fix numpy bool bug #3790
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_box_blur.py
Expand Up @@ -175,7 +175,7 @@ def test_radius_bigger_then_width(self):
delta=0,
)

def test_exteme_large_radius(self):
def test_extreme_large_radius(self):
self.assertBlur(
sample, 600,
[
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_im.py
Expand Up @@ -48,7 +48,7 @@ def test_eoferror(self):
im.seek(n_frames-1)

def test_roundtrip(self):
for mode in ["RGB", "P"]:
for mode in ["RGB", "P", "PA"]:
out = self.tempfile('temp.im')
im = hopper(mode)
im.save(out)
Expand Down
10 changes: 10 additions & 0 deletions Tests/test_file_tiff.py
Expand Up @@ -489,6 +489,16 @@ def test_tiled_planar_raw(self):
self.assert_image_equal_tofile(im,
"Tests/images/tiff_adobe_deflate.png")

def test_palette(self):
for mode in ["P", "PA"]:
outfile = self.tempfile("temp.tif")

im = hopper(mode)
im.save(outfile)

reloaded = Image.open(outfile)
self.assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))

def test_tiff_save_all(self):
import io
import os
Expand Down
9 changes: 9 additions & 0 deletions Tests/test_image_transform.py
Expand Up @@ -160,6 +160,15 @@ def test_missing_method_data(self):
im = hopper()
self.assertRaises(ValueError, im.transform, (100, 100), None)

def test_unknown_resampling_filter(self):
im = hopper()
(w, h) = im.size
for resample in (Image.BOX, "unknown"):
self.assertRaises(ValueError, im.transform, (100, 100), Image.EXTENT,
(0, 0,
w, h),
resample)


class TestImageTransformAffine(PillowTestCase):
transform = Image.AFFINE
Expand Down
27 changes: 27 additions & 0 deletions Tests/test_pickle.py
Expand Up @@ -75,6 +75,15 @@ def test_pickle_p_mode(self):
protocol=protocol,
test_file=test_file)

def test_pickle_pa_mode(self):
# Arrange
import pickle

# Act / Assert
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
self.helper_pickle_string(pickle, protocol, mode="PA")
self.helper_pickle_file(pickle, protocol, mode="PA")

def test_pickle_l_mode(self):
# Arrange
import pickle
Expand All @@ -84,6 +93,24 @@ def test_pickle_l_mode(self):
self.helper_pickle_string(pickle, protocol, mode="L")
self.helper_pickle_file(pickle, protocol, mode="L")

def test_pickle_la_mode_with_palette(self):
# Arrange
import pickle
im = Image.open('Tests/images/hopper.jpg')
filename = self.tempfile('temp.pkl')
im = im.convert("PA")

# Act / Assert
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
im.mode = "LA"
with open(filename, 'wb') as f:
pickle.dump(im, f, protocol)
with open(filename, 'rb') as f:
loaded_im = pickle.load(f)

im.mode = "PA"
self.assertEqual(im, loaded_im)

def test_cpickle_l_mode(self):
# Arrange
try:
Expand Down
2 changes: 1 addition & 1 deletion depends/install_imagequant.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# install libimagequant

archive=libimagequant-2.12.2
archive=libimagequant-2.12.3

./download-and-extract.sh $archive https://raw.githubusercontent.com/python-pillow/pillow-depends/master/$archive.tar.gz

Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Expand Up @@ -169,7 +169,7 @@ Many of Pillow's features require external libraries:

* **libimagequant** provides improved color quantization

* Pillow has been tested with libimagequant **2.6-2.12.2**
* Pillow has been tested with libimagequant **2.6-2.12.3**
* Libimagequant is licensed GPLv3, which is more restrictive than
the Pillow license, therefore we will not be distributing binaries
with libimagequant support enabled.
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/Image.rst
Expand Up @@ -210,9 +210,9 @@ Instances of the :py:class:`Image` class have the following attributes:

.. py:attribute:: palette
Colour palette table, if any. If mode is “P”, this should be an instance of
the :py:class:`~PIL.ImagePalette.ImagePalette` class. Otherwise, it should
be set to ``None``.
Colour palette table, if any. If mode is "P" or "PA", this should be an
instance of the :py:class:`~PIL.ImagePalette.ImagePalette` class.
Otherwise, it should be set to ``None``.

:type: :py:class:`~PIL.ImagePalette.ImagePalette` or ``None``

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -2,6 +2,7 @@
-e .
alabaster
Babel
black; python_version >= '3.6'
check-manifest
cov-core
coverage
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -2,4 +2,5 @@
test=pytest

[flake8]
extend-ignore = E203, W503
max-line-length = 88
20 changes: 8 additions & 12 deletions src/PIL/BdfFontFile.py
Expand Up @@ -32,14 +32,10 @@
"O": "Oblique",
"RI": "Reverse Italic",
"RO": "Reverse Oblique",
"OT": "Other"
"OT": "Other",
}

bdf_spacing = {
"P": "Proportional",
"M": "Monospaced",
"C": "Cell"
}
bdf_spacing = {"P": "Proportional", "M": "Monospaced", "C": "Cell"}


def bdf_char(f):
Expand All @@ -50,7 +46,7 @@ def bdf_char(f):
return None
if s[:9] == b"STARTCHAR":
break
id = s[9:].strip().decode('ascii')
id = s[9:].strip().decode("ascii")

# load symbol properties
props = {}
Expand All @@ -59,7 +55,7 @@ def bdf_char(f):
if not s or s[:6] == b"BITMAP":
break
i = s.find(b" ")
props[s[:i].decode('ascii')] = s[i+1:-1].decode('ascii')
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")

# load bitmap
bitmap = []
Expand All @@ -73,7 +69,7 @@ def bdf_char(f):
[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)
bbox = (dx, dy), (l, -d - y, x + l, -d), (0, 0, x, y)

try:
im = Image.frombytes("1", (x, y), bitmap, "hex", "1")
Expand All @@ -87,8 +83,8 @@ def bdf_char(f):
##
# Font file plugin for the X11 BDF format.

class BdfFontFile(FontFile.FontFile):

class BdfFontFile(FontFile.FontFile):
def __init__(self, fp):

FontFile.FontFile.__init__(self)
Expand All @@ -105,10 +101,10 @@ def __init__(self, fp):
if not s or s[:13] == b"ENDPROPERTIES":
break
i = s.find(b" ")
props[s[:i].decode('ascii')] = s[i+1:-1].decode('ascii')
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
if s.find(b"LogicalFontDescription") < 0:
comments.append(s[i+1:-1].decode('ascii'))
comments.append(s[i + 1 : -1].decode("ascii"))

while True:
c = bdf_char(fp)
Expand Down
40 changes: 13 additions & 27 deletions src/PIL/BlpImagePlugin.py
Expand Up @@ -47,11 +47,7 @@


def unpack_565(i):
return (
((i >> 11) & 0x1f) << 3,
((i >> 5) & 0x3f) << 2,
(i & 0x1f) << 3
)
return (((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3)


def decode_dxt1(data, alpha=False):
Expand Down Expand Up @@ -119,7 +115,7 @@ def decode_dxt3(data):

for block in range(blocks):
idx = block * 16
block = data[idx:idx + 16]
block = data[idx : idx + 16]
# Decode next 16-byte block.
bits = struct.unpack_from("<8B", block)
color0, color1 = struct.unpack_from("<HH", block, 8)
Expand All @@ -139,7 +135,7 @@ def decode_dxt3(data):
a >>= 4
else:
high = True
a &= 0xf
a &= 0xF
a *= 17 # We get a value between 0 and 15

color_code = (code >> 2 * (4 * j + i)) & 0x03
Expand Down Expand Up @@ -172,14 +168,12 @@ def decode_dxt5(data):

for block in range(blocks):
idx = block * 16
block = data[idx:idx + 16]
block = data[idx : idx + 16]
# Decode next 16-byte block.
a0, a1 = struct.unpack_from("<BB", block)

bits = struct.unpack_from("<6B", block, 2)
alphacode1 = (
bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24)
)
alphacode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24)
alphacode2 = bits[0] | (bits[1] << 8)

color0, color1 = struct.unpack_from("<HH", block, 8)
Expand Down Expand Up @@ -242,6 +236,7 @@ class BlpImageFile(ImageFile.ImageFile):
"""
Blizzard Mipmap Format
"""

format = "BLP"
format_description = "Blizzard Mipmap Format"

Expand All @@ -258,9 +253,7 @@ def _open(self):
else:
raise BLPFormatError("Bad BLP magic %r" % (self.magic))

self.tile = [
(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))
]
self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))]

def _read_blp_header(self):
self._blp_compression, = struct.unpack("<i", self.fp.read(4))
Expand Down Expand Up @@ -324,7 +317,6 @@ def _read_blp_header(self):


class BLP1Decoder(_BLPBaseDecoder):

def _load(self):
if self._blp_compression == BLP_FORMAT_JPEG:
self._decode_jpeg_stream()
Expand Down Expand Up @@ -368,7 +360,6 @@ def _decode_jpeg_stream(self):


class BLP2Decoder(_BLPBaseDecoder):

def _load(self):
palette = self._read_palette()

Expand All @@ -393,8 +384,7 @@ def _load(self):
linesize = (self.size[0] + 3) // 4 * 8
for yb in range((self.size[1] + 3) // 4):
for d in decode_dxt1(
self.fd.read(linesize),
alpha=bool(self._blp_alpha_depth)
self.fd.read(linesize), alpha=bool(self._blp_alpha_depth)
):
data += d

Expand All @@ -410,18 +400,14 @@ def _load(self):
for d in decode_dxt5(self.fd.read(linesize)):
data += d
else:
raise BLPFormatError("Unsupported alpha encoding %r" % (
self._blp_alpha_encoding
))
raise BLPFormatError(
"Unsupported alpha encoding %r" % (self._blp_alpha_encoding)
)
else:
raise BLPFormatError(
"Unknown BLP encoding %r" % (self._blp_encoding)
)
raise BLPFormatError("Unknown BLP encoding %r" % (self._blp_encoding))

else:
raise BLPFormatError(
"Unknown BLP compression %r" % (self._blp_compression)
)
raise BLPFormatError("Unknown BLP compression %r" % (self._blp_compression))

self.set_as_raw(bytes(data))

Expand Down

0 comments on commit ea4ea0d

Please sign in to comment.