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

Use new() instead of Image()._new() #7460

Merged
merged 1 commit into from Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/PIL/Image.py
Expand Up @@ -933,9 +933,9 @@ def convert(
msg = "illegal conversion"
raise ValueError(msg)
im = self.im.convert_matrix(mode, matrix)
new = self._new(im)
new_im = self._new(im)
if has_transparency and self.im.bands == 3:
transparency = new.info["transparency"]
transparency = new_im.info["transparency"]

def convert_transparency(m, v):
v = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3] * 0.5
Expand All @@ -948,8 +948,8 @@ def convert_transparency(m, v):
convert_transparency(matrix[i * 4 : i * 4 + 4], transparency)
for i in range(0, len(transparency))
)
new.info["transparency"] = transparency
return new
new_im.info["transparency"] = transparency
return new_im

if mode == "P" and self.mode == "RGBA":
return self.quantize(colors)
Expand Down Expand Up @@ -980,7 +980,7 @@ def convert_transparency(m, v):
else:
# get the new transparency color.
# use existing conversions
trns_im = Image()._new(core.new(self.mode, (1, 1)))
trns_im = new(self.mode, (1, 1))
if self.mode == "P":
trns_im.putpalette(self.palette)
if isinstance(t, tuple):
Expand Down Expand Up @@ -1021,23 +1021,25 @@ def convert_transparency(m, v):

if mode == "P" and palette == Palette.ADAPTIVE:
im = self.im.quantize(colors)
new = self._new(im)
new_im = self._new(im)
from . import ImagePalette

new.palette = ImagePalette.ImagePalette("RGB", new.im.getpalette("RGB"))
new_im.palette = ImagePalette.ImagePalette(
"RGB", new_im.im.getpalette("RGB")
)
if delete_trns:
# This could possibly happen if we requantize to fewer colors.
# The transparency would be totally off in that case.
del new.info["transparency"]
del new_im.info["transparency"]
if trns is not None:
try:
new.info["transparency"] = new.palette.getcolor(trns, new)
new_im.info["transparency"] = new_im.palette.getcolor(trns, new_im)
except Exception:
# if we can't make a transparent color, don't leave the old
# transparency hanging around to mess us up.
del new.info["transparency"]
del new_im.info["transparency"]
warnings.warn("Couldn't allocate palette entry for transparency")
return new
return new_im

if "LAB" in (self.mode, mode):
other_mode = mode if self.mode == "LAB" else self.mode
Expand Down