Skip to content

Image.new(): correct docstring and comment about uninitialized memory - #9960

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
akx:not-really-not-initializing
Sep 5, 2026
Merged

Image.new(): correct docstring and comment about uninitialized memory#9960
radarhere merged 2 commits into
python-pillow:mainfrom
akx:not-really-not-initializing

Conversation

@akx

@akx akx commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

The comment "# don't initialize" and the docstring's claim that passing None as the fill color leaves the image uninitialized have both been untrue for a long time.

dcd40ce (#348, Pillow 2.2.0) added a memset to the block-allocation path, to fix #254 (uninitialized memory showing through in image transformations); that memset later became a calloc in 52d60cd. Since the block allocator was used for everything up to THRESHOLD (16 MiB), only very large images were still uninitialized after that. 768936f (#1781, Pillow 3.3.0) turned the array-allocation path's per-line mallocs into callocs, which is the point where no allocation path could return dirty memory any more.

7a1e70d (#2655, Pillow 4.3.0) added an explicit internal ImagingNewDirty for callers that overwrite the whole image anyway, and 0a3c852 (#2738) added the block pool, where a caller asks for either a cleared or a dirty block.

Nothing exposed to Python returns an image whose memory is left dirty: core.new() uses ImagingNew (cleared), and core.fill() uses ImagingNewDirty but immediately fills it.

Follows up on #9957.

The comment "# don't initialize" and the docstring's claim that passing `None`
as the fill color leaves the image uninitialized have both been untrue for a
long time.

dcd40ce (python-pillow#348, Pillow 2.2.0) added a memset to the block-allocation path, to
fix python-pillow#254 (uninitialized memory showing through in image transformations); that
memset later became a calloc in 52d60cd. Since the block allocator was used
for everything up to THRESHOLD (16 MiB), only very large images were still
uninitialized after that. 768936f (python-pillow#1781, Pillow 3.3.0) turned the
array-allocation path's per-line mallocs into callocs, which is the point where
no allocation path could return dirty memory any more.

7a1e70d (python-pillow#2655, Pillow 4.3.0) added an explicit internal ImagingNewDirty for
callers that overwrite the whole image anyway, and 0a3c852 (python-pillow#2738) added the
block pool, where a caller asks for either a cleared or a dirty block.

Nothing exposed to Python returns an image whose memory is left dirty:
core.new() uses ImagingNew (cleared), and core.fill() uses ImagingNewDirty
but immediately fills it.
Comment thread src/PIL/Image.py Outdated
creating RGB or HSV images, you can also use color strings as supported
by the ImageColor module. See :ref:`colors` for more information. If the
color is None, the image is not initialised.
color is None, the image is filled with zeroes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
color is None, the image is filled with zeroes.
color is ``None``, the image is black.

What do you think of this? I expect users reading the documentation are more concerned with the visual result than the underlying data.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fine by me, but does that stand for a CMYK image?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've created akx#24 to say

The default color is zero, which appears as black in single band or RGB-based images. None is also treated as zero.

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
@akx

akx commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

I was slightly surprised that Image.new("LAB", (16, 16), 0).tobytes() isn't all zeroes (but b'\x00\x80\x80' repeated), but it makes sense, since in LAB space, A and B can be negative too.

@radarhere
radarhere merged commit cfc8a1e into python-pillow:main Sep 5, 2026
49 checks passed
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.

Problem with MESH transform /w transparent background

2 participants