Fundamentals 4 min read

How to Hide Secret Messages in Photos with Python and OpenCV

This tutorial shows how to embed hidden text into an image using Python's OpenCV and PIL libraries, detailing the step‑by‑step process, complete code, customization tips, and visual results, enabling messages that appear only when the picture is enlarged.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Hide Secret Messages in Photos with Python and OpenCV

1. Code Idea

First read the target photo with cv2.imread, then create a same‑size white canvas using PIL.Image.new, and finally draw characters onto each block of the image based on the chosen text.

2. Complete Python Code

# -*- coding:utf-8 -*-
from PIL import Image, ImageDraw, ImageFont
import cv2

font_path = './font-family/MiNiJianPangWa-1.ttf'

def draw(image_path, draw_text):
    img = cv2.imread(image_path)  # read image file
    img_temp = Image.new("RGB", [img.shape[1], img.shape[0]], "white")  # create white canvas
    drawObj = ImageDraw.Draw(img_temp)  # drawing object
    n = 8  # block interval
    m = 8  # font size
    font = ImageFont.truetype(font_path, size=m)
    for i in range(0, img.shape[0], n):
        for j in range(0, img.shape[1], n):
            drawObj.text([j, i], draw_text[int(j / n) % len(draw_text)],
                         fill=(img[i][j][2], img[i][j][1], img[i][j][0]), font=font)
    img_temp.save('img_' + image_path)

draw('bingbing.jpg', "都是冰冰的")  # you can modify the text

3. Code Adjustments

The font_path must point to a valid TrueType font file you download yourself; different fonts produce different visual effects. Both m (font size) and n (block interval) are configurable, but values of 8 for each give the best result in most tests.

Result

Original photo (use a high‑resolution image for better quality):

Resulting image – the hidden text is invisible at normal view but becomes clear when zoomed in:

Because the enlarged image may appear blurry, you can use an online lossless upscaling tool (choose the highest denoise level) to improve clarity.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonImage ProcessingOpenCVPILSteganographyHidden Message
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.