DataMasque Portal

Image Masking

The unstructured_text mask can mask the text in PNG and JPEG images (see File Type Detection for where it applies). Text in the image is read with optical character recognition (OCR) and masked, then the image is redrawn with only its text.

How It Works

  1. Reading - OCR reads the words in the image.
  2. Masking - Entities in the text are detected and masked with the configured matchers and masks.
  3. Rebuild - The background is replaced with a plain colour, and each line of text is redrawn on a box, with the masked values in place of the originals. Pictures and anything else not recognised as text are removed. The result is always an 8-bit RGB image in the input's format, without transparency. A masked or redacted JPEG is re-encoded at quality 90.

image_handling

Controls how standalone PNG and JPEG images are handled, whether as files or in binary columns. Note that this parameter also applies to the other formats the unstructured_text mask handles (see the ruleset specification), and has no effect on text columns.

Value Description
redact (default) Replaces the image with a black rectangle of the same dimensions.
retain Preserves the original image, with its metadata removed.
mask Reads the text in the image with OCR and masks it as described above.

mask applies to PNG and JPEG files only. An image inside a Word, PowerPoint or PDF document falls back to redact, with a warning.

Note: Metadata, such as text comments, EXIF data and colour profiles, is removed from standalone PNG and JPEG images under every image_handling value. Note that retain also keeps the orientation from EXIF.

Example

This ruleset reads the text in every PNG and JPEG file in the connection, then masks the names it finds:

version: "1.0"
tasks:
  - type: mask_file
    name: mask_scanned_forms
    include:
      - glob: "*.png"
        match_against: filename
      - glob: "*.jpg"
        match_against: filename
      - glob: "*.jpeg"
        match_against: filename
      - glob: "*.jpe"
        match_against: filename
      - glob: "*.jfif"
        match_against: filename
    rules:
      - masks:
          - type: unstructured_text
            image_handling: mask
            matchers:
              ai_detect:
                - label: FIRST_NAME
                  use_preset: FIRST_NAME
                # ...
            masks:
              - label: FIRST_NAME
                masks:
                  - type: from_file
                    seed_file: DataMasque_firstNames_mixed.csv
                    seed_column: firstname-mixed
              # ...

File Type Detection

Image masking is supported in two contexts:

  • In mask_file tasks, the file extension must be one that unstructured_text supports. The file content then decides the format: a file that begins with the PNG or JPEG signature is masked as an image.
  • In mask_table tasks, binary column values are inspected: content beginning with the PNG or JPEG signature is treated as an image. This allows masking of images stored as blobs in database columns.

Image Size

With image_handling: mask or redact, images over 10,000 pixels on a side fail with an error. With retain, this limit does not apply. With mask, images that need more than 536 MB once decoded also fail.

In images over 2,560 pixels on a side, small text may be missed and is then removed from the output.

Memory

The server that runs image masking should have at least 16 GiB of RAM.

Limitations

  • Text is redrawn in DejaVu Sans, so the original fonts are not preserved.
  • Stamps, faint text and heavily degraded scans may not be read, in which case they are removed from the output.
  • regex, checksum and seed_files matchers need the exact text, so a value OCR misreads may not match and is then redrawn as it was read, unmasked. ai_detect matchers use context, so they are more likely to still detect a misread entity.
  • Entities that break mid-word across lines, or that sit on pages with several columns or in wrapped table cells, may not be fully masked.
  • Tilted or rotated text may not be redrawn exactly as it appeared.
  • The position and size of redrawn words may differ slightly from the original.
  • A masked value longer than the original is drawn narrower to fit its space.
  • Text not in the Latin script (the alphabet of English and most European languages, accented letters such as é included) is not preserved: Chinese and Japanese text is redrawn as empty boxes, and other scripts, such as Korean, Cyrillic and Greek, may be redrawn as the wrong characters or removed.
  • Text read with low confidence is removed.
  • Only PNG and JPEG files are masked as images. Other formats, such as TIFF, GIF and BMP, are not supported.
  • Animated PNGs keep only their default image.
  • redact and mask do not support JPEG files with lossless, arithmetic or 12-bit encoding.
  • Hierarchical JPEG files or JPEG extensions, such as JPEG-LS, are not supported.