- This topic is empty.
-
AuthorPosts
-
January 25, 2026 at 8:32 am #5964
π Context (Initial Code)
In this discussion, we explore how to recover a hidden black-and-white image stored inside another image using LSB steganography (Least Significant Bit).
Here is the core code:
from PIL import Image def extract_end_bits(num_end_bits, pixel): return pixel % (2 ** num_end_bits) 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): hidden_bit = extract_end_bits(1, pixels[x, y]) secret_pixels[x, y] = hidden_bit * 255 return secret_imgThis program opens a βcarrier imageβ, extracts the LSB bit from every pixel, and builds a new secret image from those bits.
β Q&A: Recovering a Hidden Black & White Image Using LSB
β Q1: What is
Imagein Python?β Answer:
Imagecomes from the Pillow library:from PIL import ImageIt is used to open, create, manipulate, and save images.
β Q2: What does
img = Image.open(filename)do?β Answer:
It opens the image file given byfilenameand returns a PIL image object.Example:
img = Image.open("hidden1.bmp")
β Q3: Does
width, height = img.sizerefer to the image passed as the filename?β Answer:
Yes β
It reads the width and height of the exact image file opened usingfilename.
β Q4: What does
pixels = img.load()do?β Answer:
It loads pixel data and gives a pixel-access object, allowing pixel reading like:pixels[x, y]For grayscale images, this returns an integer from
0to255.
β Q5: Where does the
pixelvalue come from inextract_end_bits(1, pixel)?β Answer:
The pixel value comes from the image at a specific coordinate:pixel_value = pixels[x, y]So this line:
extract_end_bits(1, pixels[x, y])means:
βExtract the last bit from the pixel value at
(x, y).β
β Q6: Does
extract_end_bits(1, pixel)return only 0 or 1?β Answer:
Yes β
Because only 1 bit is extracted, the output can only be:0or1
β Q7: Why multiply by 255?
β Answer:
Because if the new image contains only pixel values0and1, it will look almost completely black.In grayscale
"L"mode:0= black1= almost black255= white
So scaling improves visibility:
hidden_bit * 255This converts:
0 β 0(black)1 β 255(white)
β Q8: Can this be written in one line?
β Answer:
Yes β this is equivalent:secret_pixels[x, y] = 255 * extract_end_bits(1, pixels[x, y])
β Q9: What are pixel values immediately after
Image.new()creates the image?β Answer:
When the new image is created:secret_img = Image.new("L", (width, height))All pixels are initialized to:
β
0(black)So it starts as a fully black image until pixels are updated in the loop.
β Q10: Why is the hidden image the same size as the original image?
β Answer:
Because one hidden bit is extracted from every pixel of the carrier image.Each pixel maps directly:
secret_pixels[x, y] β derived from pixels[x, y]So the recovered secret image uses the same width and height.
β Q11: If the secret image contains only 0 and 1, do its dimensions change?
β Answer:
No β
Dimensions depend on the number of pixels (width Γ height), not the pixel values.Whether the pixel values are:
0/1or0/255
the size remains the same.
β Q12: Can a black-and-white secret image still show objects like a dog?
β Answer:
Yes β
Images can be formed using only black and white pixels through pixel patterns (silhouettes, outlines, shapes, text, logos, etc.).
β Q13: Why loop through
xfirst instead ofy?β Answer:
It is mainly a style choice. Both orders work correctly as long as all pixels are visited.These both work:
for x in range(width): for y in range(height):and
for y in range(height): for x in range(width):
β Q14: What is happening in LSB steganography at a high level?
β Answer:
A hidden black-and-white image is stored by replacing the least significant bit of each pixel in the carrier image. The carrier looks almost identical to humans, but the hidden image can be recovered by extracting and rescaling those bits.
β Final Takeaway
LSB steganography hides a black-and-white secret image by storing one bit inside the least significant bit of every pixel in a normal image. The secret image is recovered by extracting those bits and scaling them to full black/white for visibility.
-
AuthorPosts
- You must be logged in to reply to this topic.
