Skip to content

Do not initialize image with zero dimension or when color is zero - #9957

Closed
radarhere wants to merge 2 commits into
python-pillow:mainfrom
radarhere:new_size
Closed

Do not initialize image with zero dimension or when color is zero#9957
radarhere wants to merge 2 commits into
python-pillow:mainfrom
radarhere:new_size

Conversation

@radarhere

Copy link
Copy Markdown
Member

#9933 mentioned that

Pillow/Tests/test_image.py

Lines 574 to 577 in 7827507

@timeout_unless_slower_valgrind(0.75)
@pytest.mark.parametrize("size", ((0, 100000000), (100000000, 0)))
def test_empty_image(self, size: tuple[int, int]) -> None:
Image.new("RGB", size)

is intermittently failing on our CI.

I think

Pillow/src/PIL/Image.py

Lines 3234 to 3236 in 7827507

if color is None:
# don't initialize
return Image()._new(core.new(mode, size))

can be adjusted to help.

@codspeed-hq

codspeed-hq Bot commented Sep 4, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 56.95%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 602 untouched benchmarks
⏩ 335 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_allocate[1237x811-L] 1.1 ms 2.5 ms -56.95%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing radarhere:new_size (0f4e203) with main (7827507)

Open in CodSpeed

Footnotes

  1. 335 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this helps with the timeout on tall zero-width images.

core.fill(mode, size, color), paraphrased:

  • im = ImagingNewDirty(mode, xsize, ysize)
  • ImagingFill(im, ink_parsed_from_color)
  • return PyImagingNew(im)

core.new(mode, size), paraphrased:

  • im = ImagingNew(mode, xsize, ysize)
  • return PyImagingNew(im)

ImagingNew and ImagingNewDirty are trampolines to ImagingNewInternal, which, paraphrased;

  • im = ImagingNewPrologue(mode, xsize, ysize); which calls ImagingNewPrologueSubtype, which allocates the pointer array: im->image = (char **)calloc((ysize > 0) ? ysize : 1, sizeof(void *));

In fact, I think # don't initialize isn't a valid comment at all.

  • EDIT: it hasn't been true for 7 to 13 years, depending, I investigated. #9960

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants