Skip to content

Commit

Permalink
Merge pull request #858 from girder/local-colors
Browse files Browse the repository at this point in the history
Add local color definitions.
  • Loading branch information
manthey authored Jul 26, 2022
2 parents b803b9c + 56a9414 commit 89b9368
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 89b9368

Please sign in to comment.