Skip to content

Image.putalpha should handle 16/32-bit to RGBA conversion #4926

@bertsky

Description

@bertsky

This is similar to #2705 (which covered P→PA conversion):

IMHO images in 16-bit (I;16) or 32-bit (I / F) modes should be automatically converted to RGBA when putalpha is attempted on them.

Current (7.2.0) behaviour:

>>> img = PIL.Image.new('F', (200,100), 1.0)
>>> img.putalpha(PIL.Image.new('L', (200,100), 255))
Traceback (most recent call last):
  File "/lib/python3.6/site-packages/PIL/Image.py", line 1627, in putalpha
    self.im.setmode(mode)
ValueError: image has wrong mode

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/lib/python3.6/site-packages/PIL/Image.py", line 1630, in putalpha
    im = self.im.convert(mode)
ValueError: conversion from F to LA not supported

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/python3.6/site-packages/PIL/Image.py", line 1637, in putalpha
    raise ValueError("illegal image mode") from e
ValueError: illegal image mode

If this is too difficult or unwise for other reasons (like quantization), then at least the documentation should state which base modes are supported.

In the very least, the above ugly multi-catch error message should be avoided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions