Skip to content

Can't convert PNG to JPEG due to presence of alpha channel. #10

@chrishettinger

Description

@chrishettinger

Currently, attempting to convert a PNG to a JPEG results in an IOERROR. Specifically, the error says "cannot write mode RGBA as JPEG", which suggests an easy fix. There is an existing if block, currently at line 459 in simple-image-reducer.py, that handles the quality setting before saving a JPEG:

if fmt == 'JPEG':
    options['quality'] = 90

One can simply add a conversion to the RGB space in this block:

if fmt == 'JPEG':
    options['quality'] = 90
    img = img.convert('RGB')

This solves the problem for me, although it may not be a perfect solution. A more robust approach would probably involve looking at the color space currently in use, then deciding on the most appropriate conversion. And if this problem occurs for output formats other than JPEG, it might be better to do the conversion elsewhere.

Also, there is a mistake in the error handling block, which currently starts at line 463. It reads:

except IOError:
    errors.append(_("Unable to open file for writing: %s") % input)

It should read:

except IOError:
    errors.append(_("Unable to open file for writing: %s") % output)

The input file isn't being opened for writing, the output file is.

Even with this correction, the error message might be considered unhelpful or even misleading. It might be better to include the actual error message. A crude way of doing this is:

except IOError as ioe:
    errors.append(_("An error occurred when attempting to save: %s") % output)
    errors.append("Error text: " + str(ioe))

This change is what allowed me to find the RGBA problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions