Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local color definitions. #858

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions large_image/tilesource/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
# Turn off decompression warning check
PIL.Image.MAX_IMAGE_PIXELS = None

# Extend colors so G and GREEN map to expected values. CSS green is #0080ff,
# which is unfortunate.
colormap = {
'R': '#ff0000',
'G': '#00ff00',
'B': '#0000ff',
'RED': '#ff0000',
'GREEN': '#00ff00',
'BLUE': '#0000ff',
}


def _encodeImageBinary(image, encoding, jpegQuality, jpegSubsampling, tiffCompression):
"""
Expand Down Expand Up @@ -185,7 +196,7 @@ def _letterboxImage(image, width, height, fill):
corner = False
if fill.lower().startswith('corner:'):
corner, fill = True, fill.split(':', 1)[1]
color = PIL.ImageColor.getcolor(fill, image.mode)
color = PIL.ImageColor.getcolor(colormap.get(fill, fill), image.mode)
width = max(width, image.width)
height = max(height, image.height)
result = PIL.Image.new(image.mode, (width, height), color)
Expand Down Expand Up @@ -451,7 +462,7 @@ def _arrayToPalette(palette):
arr.append(numpy.array((list(clr) + [1, 1, 1])[:4]) * 255)
else:
try:
arr.append(PIL.ImageColor.getcolor(str(clr), 'RGBA'))
arr.append(PIL.ImageColor.getcolor(str(colormap.get(clr, clr)), 'RGBA'))
except ValueError:
try:
import matplotlib
Expand Down Expand Up @@ -482,7 +493,7 @@ def getPaletteColors(value):
palette = value
if palette is None:
try:
PIL.ImageColor.getcolor(str(value), 'RGBA')
PIL.ImageColor.getcolor(str(colormap.get(value, value)), 'RGBA')
palette = ['#000', str(value)]
except ValueError:
pass
Expand Down Expand Up @@ -559,6 +570,7 @@ def getAvailableNamedPalettes(includeColors=True, reduced=False):
palettes = set()
if includeColors:
palettes |= set(PIL.ImageColor.colormap.keys())
palettes |= set(colormap.keys())
_recursePalettablePalettes(palettable, palettes)
try:
import matplotlib
Expand Down
1 change: 1 addition & 0 deletions test/test_source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def testIsGeospatial(filename, isgeo):
'matplotlib.Plasma_6',
[(0.5, 0.5, 0.5), (0.1, 0.1, 0.1, 0.1), 'xkcd:blue'],
'coolwarm',
'GREEN',
])
def testGoodGetPaletteColors(palette):
large_image.tilesource.utilities.getPaletteColors(palette)
Expand Down