Skip to content

Commit

Permalink
Handle RGB->LAB conversion as special case.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkunic committed Apr 22, 2019
1 parent d3a9778 commit d8ae477
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions smiler_tools/smiler_tools/image_processing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import numpy as np
from PIL import Image
from PIL import Image, ImageCms
import scipy.ndimage
import scipy.misc
import scipy.stats
Expand Down Expand Up @@ -45,8 +45,19 @@ def pre_process(img, options, check_channels=True):
color_space = 'L'

if color_space != 'default':
img = Image.fromarray(img).convert(color_space)
img = np.array(img)
if color_space == 'LAB':
# Special case, PIL can't convert RGB -> LAB directly.
img = Image.fromarray(img)

srgb_p = ImageCms.createProfile('sRGB')
lab_p = ImageCms.createProfile('LAB')

rgb2lab = ImageCms.buildTransformFromOpenProfiles(srgb_p, lab_p, "RGB", "LAB")
ImageCms.applyTransform(img, rgb2lab, inPlace=True)
img = np.array(img)
else:
img = Image.fromarray(img).convert(color_space)
img = np.array(img)

if color_space == 'L':
img = np.expand_dims(img, 2) # adding third channel
Expand Down

0 comments on commit d8ae477

Please sign in to comment.