Python Image Steganography: Hide Data in Plain Sight
ملخص
Pixel shares a developer guide to steganography, explaining how it hides a message’s existence rather than encrypting its contents. The article explores LSB substitution, compares steganography with cryptography, and walks through building an image-hiding tool in Python—useful context for developers exploring data privacy and security.
DEV Community
Steganography for Developers: Hiding Data in Plain Sight
Most developers know cryptography. You encrypt a message, and anyone who intercepts it sees gibberish. But gibberish itself is a signal: it tells an observer that something secret is going on. Steganography takes the opposite approach. Instead of making a message unreadable, it makes the message invisible. The word comes from the Greek steganos ("covered") and graphein ("writing"). The goal is that an observer never suspects a hidden message exists at all. In this article we'll look at how it works, build a working image steganography tool in Python, and discuss where it is (and isn't) useful. Steganography vs. Cryptography Cryptography Steganography Hides The content of a message The existence of a message Visible to an observer? Yes, as ciphertext No, looks like a normal file Failure mode Message stays unreadable Message is discovered They are not competitors. The strongest setups combine them: encrypt first, then hide the ciphertext. If the stego layer is ever detected, the attacker still faces encryption. A Short History Steganography is much older than computers. Herodotus describes messages tattooed on a shaved head and covered by regrown hair. Invisible inks made from lemon juice or milk were used for centuries. In WWII, spies used microdots, entire pages photographed down to the size of a period. Digital steganography follows the same principle: find a place where a small change goes unnoticed. The Core Idea: Redundancy Digital media is full of redundancy. A photo contains far more detail than the human eye can perceive, and an audio file contains frequencies we can't hear. Steganography replaces some of this "unimportant" data with secret data. The classic technique is LSB (Least Significant Bit) substitution. Each pixel in an RGB image has three channels, each stored as a byte (0–255). Consider a red value of 200: 200 = 11001000
If we flip the last bit, we get 11001001, which is 201. Nobody can see the difference between red 200 and red 201. So every channel can carry 1 hidden bit, and every pixel carries 3 bits. A 1920×1080 image has about 2 million pixels, which gives roughly 6.2 million bits, or about 777 KB of hidden capacity. That's plenty for text. Building It in Python Let's implement an LSB encoder and decoder with Pillow. pip install pillow
Step 1: Convert text to bits We need to know where the message ends. A simple approach is to prefix the payload with its length (32 bits). def to_bits(data: bytes) -> list[int]:
length = len(data)
header = [(length >> i) & 1 for i in range(31, -1, -1)]
body = [(byte >> i) & 1 for byte in data for i in range(7, -1, -1)]
return header + body
def from_bits(bits: list[int]) -> bytes:
length = 0
for bit in bits[:32]:
length = (length << 1) | bit
payload = bits[32:32 + length * 8]
out = bytearray()
for i in range(0, len(payload), 8):
byte = 0
for bit in payload[i:i + 8]:
byte = (byte << 1) | bit
out.append(byte)
return bytes(out)
Step 2: Embed the bits from PIL import Image
def embed(cover_path: str, out_path: str, message: str) -> None:
img = Image.open(cover_path).convert("RGB")
pixels = list(img.getdata())
bits = to_bits(message.encode("utf-8"))
capacity = len(pixels) * 3
if len(bits) > capacity:
raise ValueError(
f"Message too large: need {len(bits)} bits, have {capacity}"
)
new_pixels = []
bit_index = 0
for r, g, b in pixels:
channels = [r, g, b]
for c in range(3):
if bit_index < len(bits):
channels[c] = (channels[c] & ~1) | bits[bit_index]
bit_index += 1
new_pixels.append(tuple(channels))
stego = Image.new("RGB", img.size)
stego.putdata(new_pixels)
stego.save(out_path, format="PNG") # must be lossless!
Note the output format: PNG. This is critical, and we'll come back to it below. Step 3: Extract the bits def extract(stego_path: str) -...

Powered by Readybot.io