Skip to content

Conversation

@radarhere
Copy link
Member

Looking at SgiImagePlugin, I see that an error for the incorrect number of bands will never be raised.

dim = 3
# X Dimension = width / Y Dimension = height
x, y = im.size
if im.mode == "L" and y == 1:
dim = 1
elif im.mode == "L":
dim = 2
# Z Dimension: Number of channels
z = len(im.mode)
if dim in {1, 2}:
z = 1
# assert we've got the right number of bands.
if len(im.getbands()) != z:
msg = f"incorrect number of bands in SGI write: {z} vs {len(im.getbands())}"
raise ValueError(msg)

Only L, RGB and RGBA modes are supported when saving.

def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
if im.mode not in {"RGB", "RGBA", "L"}:
msg = "Unsupported SGI image mode"

In the first code snippet, you can see that z = len(im.mode) would be set z to the correct values of 1 for L, 3 for RGB and 4 for RGBA. It is later set to 1 if dim is 1 or 2, but dim only has those values for L mode images, which should be 1 anyway.

So the error can be removed.

After some code simplification, this adds coverage for the lines missing at https://app.codecov.io/gh/python-pillow/Pillow/blob/main/src%2FPIL%2FSgiImagePlugin.py

@radarhere radarhere changed the title Improved SgiImagePlugin test coverage Improve SgiImagePlugin test coverage Apr 19, 2025
Comment on lines 158 to 160
dim = 1 if y == 1 else 2
else:
dim = 3
Copy link
Member

@hugovk hugovk Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we use more a descriptive name? (and below)

Suggested change
dim = 1 if y == 1 else 2
else:
dim = 3
dimensions = 1 if y == 1 else 2
else:
dimensions = 3

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit with dimension. It matches

# dimension : 1, 2 or 3 (depending on xsize, ysize and zsize)
dimension = i16(s, 4)

and the NAME in 'The Header' at https://paulbourke.net/dataformats/sgirgb/sgiversion.html

@hugovk hugovk merged commit e783aff into python-pillow:main Jun 27, 2025
57 checks passed
@radarhere radarhere deleted the sgi branch June 27, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants