Tools,

Redact text in an image, tested on 35 images

16 of 164

values OCR still read after redaction

Check a redacted image

One fix cut Tesseract from 16 to 1

Method Not fully hidden Read in full
Tesseract and regex 20 16
Tesseract, psm 11 39 27
Apple Vision 6 3
Tesseract, emails joined 1 1
Tesseract and Vision, emails joined 0 0

164 fake values in 30 screenshots and 5 book scans. Each method: OCR, regex, black boxes.

Not fully hidden: some ink shows. Read in full: Tesseract psm 11, Apple Vision or our Actor got the whole value. People read more, like [email protected].

The same two short paragraphs of a made-up email three times: as made, with black boxes over the card number, two phone numbers and an email, and with those values lightly blurred
A made-up email: as made, after the script below, and lightly blurred.

OCR puts spaces inside emails

True Read as
[email protected] dana.whitfieLd@examp le. com
Tesseract
[email protected] lena. berg@example. net
Apple Vision
4111-1111-1111-1111 4aant-1n11-1111-1111
Tesseract

All 16 emails that Tesseract did not fully hide sat in the terminal shots, in a monospace font. The spaces cut each email in pieces, and the regex needs one piece.

The fix: run the email regex again on the line with every space taken out, then box the words it touches. Every email got a box.

Blur is not redaction

Cover Read in full
Light blur 8
Heavy blur 0
Light pixelate 0
Heavy pixelate 0

Here each value's own box was blurred, so none was missed. Of 164 values, Apple Vision read 8 in full after the light blur. Tesseract and our Actor read none.

The script: Tesseract, regex, black boxes

brew install tesseract
python3 -m venv .venv
.venv/bin/pip install pillow
.venv/bin/python redact.py shot.png redacted.png
redact.py, the full script
import re
import subprocess
import sys

from PIL import Image, ImageDraw

EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
PATTERNS = [
    EMAIL,
    re.compile(r"(?<!\d)(?:\+?1[ .-]?)?\(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4}(?!\d)"),  # US phone
    re.compile(r"(?<!\d)(?:\d[ -]?){12,18}\d(?!\d)"),  # card number
    re.compile(r"\b[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,3})?\b"),  # IBAN
]


def hits(words, sep, patterns):
    starts, line = [], ""
    for w in words:
        starts.append(len(line))
        line += w + sep
    for p in patterns:
        for m in p.finditer(line):
            yield tuple(i for i, s in enumerate(starts) if s < m.end() and s + len(words[i]) > m.start())


def redact(src, dst):
    tsv = subprocess.run(["tesseract", src, "-", "tsv"], capture_output=True, text=True, check=True).stdout
    lines = {}
    for row in tsv.splitlines()[1:]:
        c = row.split("\t")
        if len(c) == 12 and c[0] == "5" and c[11].strip():
            x, y, w, h = map(int, c[6:10])
            lines.setdefault(tuple(c[1:5]), []).append((c[11], (x, y, x + w, y + h)))
    im = Image.open(src).convert("RGB")
    draw = ImageDraw.Draw(im)
    for line in lines.values():
        words = [w for w, _ in line]
        # OCR puts spaces in emails ("examp le. com"), so look again with the spaces taken out
        for idx in set(hits(words, " ", PATTERNS)) | set(hits(words, "", [EMAIL])):
            b = [line[i][1] for i in idx]
            x0, y0 = min(x[0] for x in b), min(x[1] for x in b)
            x1, y1 = max(x[2] for x in b), max(x[3] for x in b)
            pad = max(2, round(0.12 * (y1 - y0)))
            draw.rectangle((x0 - pad, y0 - pad, x1 + pad, y1 + pad), fill="black")
    im.save(dst)  # a new file of new pixels: what was under the boxes is gone


redact(sys.argv[1], sys.argv[2])

Read the result again before you share

Run OCR on the redacted file with a different reader, and look at it yourself. Our Actor and Apple Vision both caught the 1 card the script left. Tesseract psm 11 did not.

Save a new file. In 2023, aCropalypse showed why: the Pixel Markup tool, the Windows Snipping Tool and Snip & Sketch wrote an edit over the old file without cutting it short, so old pixels stayed after the end.

Check a redacted image in one call

Our Apify Actor, PDF to Markdown OCR, reads scanned PDFs and images and returns the text. It draws no boxes. Send it the redacted image: any value in its text is still readable.

What I tested, and what I did not

How I tested
  • Screenshots: 15 made-up scenes (mail, chat, checkout, dark settings, terminal), drawn by headless Chromium at 1x and 2x.
  • Scans: 5 public domain book pages from Wikisource, with a typed note of 4 fake values added at the bottom.
  • Values: 59 emails, 53 phones, 35 cards and 17 IBANs. Emails at example.com, example.org and example.net, phones in 555-0100 to 555-0199, test card numbers from Stripe and Braintree, and example IBANs. Plus 78 decoys: order numbers, a reference number, dates, prices and other numbers.
  • Tesseract 5.5.4, eng from tessdata_fast, psm 3 (the default) and 11. Apple Vision accurate, en-US, word boxes from boundingBox(for:).
  • Light and heavy blur: Gaussian, radius 12% and 25% of the value's box height. Pixelate: blocks of 33% and 66% of it.
  • Each box grows by 12% of its height, 2 px at least. Ink: pixels more than 50 gray levels from the middle gray of the box's edge.
  • Read in full: the value's letters and digits in a row, ignoring case, spaces and punctuation, in a reader's text of the whole image. Our Actor with its second reader on, the default.
  • Apple M5 Max, macOS 26.6, shared with other jobs.
Not tested
  • Names, street addresses, faces, signatures or handwriting. A regex does not find a name.
  • Phones outside the US format, turned text, photos of screens.
  • Presidio Image Redactor, or a model that finds personal data.
  • Undoing blur or pixelation. I only ran OCR on it.
  • Values that wrap onto a second line. The script reads one line at a time, so it misses them.
Sources

Questions people ask

How do I redact text in an image with Python?

Read word boxes with Tesseract, run a regex on each line, and fill the matched words black with Pillow. On my 35 images, OCR still read 16 of 164 fake values after that. Running the email regex again with spaces taken out cut it to 1.

Is blurring text in a screenshot safe?

No. After a light blur, Apple Vision still read 8 of 164 values in full, and in the picture on this page you can read blurred phone numbers yourself. Use a solid black box, save a new file, then check it again.

Can Apple Vision find text to redact?

Yes, on a Mac. Its word boxes alone hid 158 of 164 values. With Tesseract's boxes and the second email pass, they hid all 164.