Skip to content

Commit

Permalink
restyle
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmwu committed Mar 10, 2015
1 parent dfe80f1 commit def5968
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/psd_tools/user_api/pil_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
ImageCms = None


def tobytes(image):
# Some versions of PIL are missing the tobytes alias for tostring
if hasattr(image, 'tobytes'):
return image.tobytes()
else:
return image.tostring()


def extract_layer_image(decoded_data, layer_index):
"""
Converts a layer from the ``decoded_data`` to a PIL image.
Expand Down Expand Up @@ -116,13 +124,9 @@ def _merge_bands(bands, color_mode, size, icc_profile):
merged_image = Image.merge('RGB', [bands[key] for key in 'RGB'])
elif color_mode == ColorMode.CMYK:
merged_image = Image.merge('CMYK', [bands[key] for key in 'CMYK'])

# Monkey patch some versions of PIL that lack the alias tobytes
if not hasattr(merged_image, 'tobytes'):
merged_image.tobytes = merged_image.tostring

merged_bytes = tobytes(merged_image)
# colors are inverted in Photoshop CMYK images; invert them back
merged_image = frombytes('CMYK', size, merged_image.tobytes(), 'raw', 'CMYK;I')
merged_image = frombytes('CMYK', size, merged_bytes, 'raw', 'CMYK;I')
elif color_mode == ColorMode.GRAYSCALE:
merged_image = bands['L']
else:
Expand Down

0 comments on commit def5968

Please sign in to comment.