mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-28 02:04:36 +03:00
Set color after attaching palette in expand()
This commit is contained in:
parent
8210645e4b
commit
b6b362c807
|
@ -157,9 +157,16 @@ def test_scale():
|
||||||
|
|
||||||
|
|
||||||
def test_expand_palette():
|
def test_expand_palette():
|
||||||
im = Image.open("Tests/images/hopper.gif")
|
im = Image.open("Tests/images/p_16.tga")
|
||||||
im_expanded = ImageOps.expand(im)
|
im_expanded = ImageOps.expand(im, 10, (255, 0, 0))
|
||||||
assert_image_equal(im_expanded.convert("RGB"), im.convert("RGB"))
|
|
||||||
|
px = im_expanded.convert("RGB").load()
|
||||||
|
assert px[0, 0] == (255, 0, 0)
|
||||||
|
|
||||||
|
im_cropped = im_expanded.crop(
|
||||||
|
(10, 10, im_expanded.width - 10, im_expanded.height - 10)
|
||||||
|
)
|
||||||
|
assert_image_equal(im_cropped, im)
|
||||||
|
|
||||||
|
|
||||||
def test_colorize_2color():
|
def test_colorize_2color():
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
import functools
|
import functools
|
||||||
import operator
|
import operator
|
||||||
|
|
||||||
from . import Image
|
from . import Image, ImageDraw
|
||||||
|
|
||||||
#
|
#
|
||||||
# helpers
|
# helpers
|
||||||
|
@ -392,10 +392,17 @@ def expand(image, border=0, fill=0):
|
||||||
left, top, right, bottom = _border(border)
|
left, top, right, bottom = _border(border)
|
||||||
width = left + image.size[0] + right
|
width = left + image.size[0] + right
|
||||||
height = top + image.size[1] + bottom
|
height = top + image.size[1] + bottom
|
||||||
out = Image.new(image.mode, (width, height), _color(fill, image.mode))
|
color = _color(fill, image.mode)
|
||||||
if image.mode == "P" and image.palette:
|
if image.mode == "P" and image.palette:
|
||||||
|
out = Image.new(image.mode, (width, height))
|
||||||
out.putpalette(image.palette)
|
out.putpalette(image.palette)
|
||||||
out.paste(image, (left, top))
|
out.paste(image, (left, top))
|
||||||
|
|
||||||
|
draw = ImageDraw.Draw(out)
|
||||||
|
draw.rectangle((0, 0, width, height), outline=color, width=border)
|
||||||
|
else:
|
||||||
|
out = Image.new(image.mode, (width, height), color)
|
||||||
|
out.paste(image, (left, top))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -115,7 +115,7 @@ class ImagePalette:
|
||||||
return self.colors[color]
|
return self.colors[color]
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
# allocate new color slot
|
# allocate new color slot
|
||||||
if isinstance(self.palette, bytes):
|
if not isinstance(self.palette, bytearray):
|
||||||
self._palette = bytearray(self.palette)
|
self._palette = bytearray(self.palette)
|
||||||
index = len(self.palette) // 3
|
index = len(self.palette) // 3
|
||||||
if index >= 256:
|
if index >= 256:
|
||||||
|
@ -129,9 +129,11 @@ class ImagePalette:
|
||||||
raise ValueError("cannot allocate more than 256 colors") from e
|
raise ValueError("cannot allocate more than 256 colors") from e
|
||||||
self.colors[color] = index
|
self.colors[color] = index
|
||||||
if index * 3 < len(self.palette):
|
if index * 3 < len(self.palette):
|
||||||
self._palette[index * 3] = color[0]
|
self._palette = (
|
||||||
self._palette[index * 3 + 1] = color[1]
|
self.palette[: index * 3]
|
||||||
self._palette[index * 3 + 2] = color[2]
|
+ bytes(color)
|
||||||
|
+ self.palette[index * 3 + 3 :]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._palette += bytes(color)
|
self._palette += bytes(color)
|
||||||
self.dirty = 1
|
self.dirty = 1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user