Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If Image is created directly, add missing core image when loading #7457

Closed
wants to merge 1 commit into from

Conversation

radarhere
Copy link
Member

Resolves #7455

Normally, image instances are created with Image.new() or Image.open(). The issue would like to be able to use Image() directly.

The only thing that _new() provides that __init__ doesn't is a core image instance. Pillow allows for im.im to be None, but expects load() to populate it.

Using the fact that __init__ sets _mode to ""

Pillow/src/PIL/Image.py

Lines 483 to 487 in 4ecf1df

def __init__(self):
# FIXME: take "new" parameters / other image?
# FIXME: turn mode and size into delegating properties?
self.im = None
self._mode = ""

this PR detects in load() if an image has been directly instantiated, and if so, adds a core image instance with mode 1, as that is the simplest mode.

@homm
Copy link
Member

homm commented Oct 12, 2023

Isn't better restrict creating image instances directly? Is it used anywhere?

@radarhere
Copy link
Member Author

I don't understand why the user in that issue tried to run Image(), but this seemed like a simple solution.

In contrast, preventing users from running Image() but still allowing the following internal uses seemed like it would involve changing some fundamental methods.

Pillow/src/PIL/Image.py

Lines 511 to 512 in 4ecf1df

def _new(self, im):
new = Image()

Pillow/src/PIL/Image.py

Lines 2919 to 2937 in 4ecf1df

if color is None:
# don't initialize
return Image()._new(core.new(mode, size))
if isinstance(color, str):
# css3-style specifier
from . import ImageColor
color = ImageColor.getcolor(color, mode)
im = Image()
if mode == "P" and isinstance(color, (list, tuple)) and len(color) in [3, 4]:
# RGB or RGBA value for a P image
from . import ImagePalette
im.palette = ImagePalette.ImagePalette()
color = im.palette.getcolor(color)
return im._new(core.fill(mode, size, color))

If the issue is closed, I'm happy to close this as well.

@radarhere radarhere closed this Oct 12, 2023
@radarhere radarhere deleted the image branch October 12, 2023 21:40
@radarhere
Copy link
Member Author

I've created #7461 as a suggestion for how to restrict creating image instances directly. I think the changes involved are relatively minimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image.Image() == Image.Image() fails with AttributeError
2 participants