› Forums › Python › What Does Image.new(“L”, (width, height)) Do in Python? (Explained with LSB Recovery)
- This topic is empty.
-
AuthorPosts
-
January 16, 2026 at 10:30 am #5940
ߓ Context (Code First)
When recovering a hidden black & white image from the LSB (least significant bit) of another image, we often use this code:
from PIL import Image def reveal_bw_image(filename): img = Image.open(filename) width, height = img.size pixels = img.load() secret_img = Image.new("L", (width, height)) secret_pixels = secret_img.load() for x in range(width): for y in range(height): pixel = pixels[x, y] hidden_bit = extract_end_bits(1, pixel) secret_pixels[x, y] = hidden_bit * 255 return secret_imgThis raises a common question:
✅ Q&A: What Does
Image.new("L", (width, height))Create?❓ Q1: On what basis is this new image created?
secret_img = Image.new("L", (width, height))✅ Answer:
The new image is created based on:- Mode
"L"→ meaning the image is grayscale - Size
(width, height)→ meaning the image has the same dimensions as the carrier image
So it becomes a blank grayscale canvas where we will build the recovered hidden image.
❓ Q2: What will be its pixel value at each point initially?
✅ Answer:
When you create an image like this (without specifying a color), Pillow fills it with:0 everywhere
That means every pixel starts as:
0→ black
So initially, the secret image is a completely black image.
❓ Q3: Why does the code create a blank black image first?
✅ Answer:
Because we need an empty image to write the recovered hidden pixels into.The loop extracts hidden bits from the original image and replaces pixels in the secret image:
secret_pixels[x, y] = hidden_bit * 255So during the loop, pixels change from the default
0to:0(black) if hidden bit = 0255(white) if hidden bit = 1
That’s how the hidden image gets drawn.
❓ Q4: What does this line do?
secret_pixels = secret_img.load()✅ Answer:
This gives you access to the pixels of the new image, so you can change pixel values directly like:secret_pixels[x, y] = 255Without
.load(), you wouldn’t be able to set pixels easily.
✅ Final Takeaway
Image.new("L", (width, height))creates a blank grayscale image of the same size as the original, and its pixels start as 0 (black) until your code fills them with the recovered hidden image data. - Mode
-
AuthorPosts
- You must be logged in to reply to this topic.

