# Extract text from scanned images with Tesseract

> **About this page**
>
> - Kind: Example
> - Summary: Reads the text of a list of scanned page images with Tesseract, one task per image, and collects the text and word confidences into one Parquet dataset.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/scans-to-text
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/scans-to-text`](https://github.com/swaleio/examples/tree/main/workflows/scans-to-text) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/scans-to-text/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/scans-to-text/workflow.yaml).

Reads the text of scanned pages with Tesseract OCR. Each scan is fetched and
read in its own tasks, all at the same time: once as plain text, and once as a
table of words with Tesseract's confidence in each. A last step gathers the
text and each scan's mean word confidence into one Parquet file, so the scans
Tesseract struggled with stand out.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Make the output folders | [`swaleio/bash`](https://hub.swale.io/swaleio/bash) | `cpu-4` |
| For each scan: fetch it | [`swaleio/http-request`](https://hub.swale.io/swaleio/http-request) | `cpu-4` |
| For each scan: read its text, and its words with confidences, in parallel | [`swaleio/tesseract`](https://hub.swale.io/swaleio/tesseract) | `cpu-4` each |
| Gather text and confidences into `scans.parquet` | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Upload to the Hugging Face Hub, when `hub_repo` is set | [`swaleio/hf-upload`](https://hub.swale.io/swaleio/hf-upload) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), the compute its tasks are allocated.

Every step is its own task.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `image_urls` | two sample scans from Tesseract's test data | The scans, as a JSON array of URLs. |
| `language` | `eng+deu+fra` | Tesseract language codes, joined with `+`. |
| `hub_repo` | empty | Hugging Face dataset repo to upload the dataset to, as `data/train.parquet`. |

The upload reads the secret `hf_token`.

## Output

`scans.parquet` on the workspace, with `url`, `text` and
`mean_word_confidence` (0 to 100) for each scan, and Tesseract's `.txt` and
`.tsv` files in `text/`.

## Run it

You need the CLI, logged in to your account, and a project to add the workflow to; [Getting started](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   swale workflow create --project-name my-project --file workflow.yaml
   ```

   The command prints `Workflow created:` followed by the workflow's ID.

2. The workflow reads the secret `hf_token`. Create it as a project secret, or as an account secret that allows the project, as [Use secrets in workflows](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

3. Start a run with the workflow's ID:

   ```bash
   swale workflow run start --project-name my-project --workflow-id <workflow-id>
   ```

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="workflow.yaml"
name: Extract text from scanned images with Tesseract
description: Reads the text of a list of scanned page images with Tesseract, one task per image, and collects the text and word confidences into one Parquet dataset.
compute_type: cpu-4

inputs:
  image_urls:
    description: The scans to read, as a JSON array of URLs.
    default: '["https://raw.githubusercontent.com/tesseract-ocr/test/232ff181c66516116ec0e84c4963f70de15050fd/testing/phototest.tif", "https://raw.githubusercontent.com/tesseract-ocr/test/232ff181c66516116ec0e84c4963f70de15050fd/testing/eurotext.tif"]'
  language:
    description: Tesseract language code, or several joined with +.
    default: eng+deu+fra
  hub_repo:
    description: Hugging Face dataset repo to upload the dataset to, as namespace/name. Leave empty to keep it on the workspace only.
    default: ""

entry_point: main

blocks:
  main:
    tasks:
      make_dirs:
        name: Make output folders
        uses: swaleio/bash
        args:
          script: mkdir -p "$WORKFLOW_STORAGE/scans" "$WORKFLOW_STORAGE/text"

      each_scan:
        name: Read each scan
        start_on:
          - make_dirs
        for_each:
          items: ${{inputs.image_urls}}
          block: read_one

      collect:
        name: Collect into a dataset
        uses: swaleio/duckdb
        start_on:
          - each_scan
        args:
          sql: |
            CREATE TABLE urls AS
              SELECT unnest(from_json($$${{inputs.image_urls}}$$, '["VARCHAR"]')) AS url,
                     generate_subscripts(from_json($$${{inputs.image_urls}}$$, '["VARCHAR"]'), 1) - 1 AS item;
            -- Tesseract's TSV has one row per word, with its confidence from 0 to 100.
            CREATE TABLE words AS
              SELECT regexp_extract(filename, '(\d+)\.tsv$', 1)::INT AS item, conf, text
              FROM read_csv('${{env.WORKFLOW_STORAGE}}/text/*.tsv', delim = '\t', quote = '', header = true,
                            filename = true, ignore_errors = true)
              WHERE conf >= 0;
            CREATE TABLE scans AS
              SELECT u.url, t.content AS text,
                     (SELECT round(avg(conf), 1) FROM words w WHERE w.item = u.item) AS mean_word_confidence
              FROM urls u
              JOIN read_text('${{env.WORKFLOW_STORAGE}}/text/*.txt') t
                ON regexp_extract(t.filename, '(\d+)\.txt$', 1)::INT = u.item
              ORDER BY u.item;
            SELECT url, length(text) AS characters, mean_word_confidence FROM scans;
            COPY scans TO '${{env.WORKFLOW_STORAGE}}/scans.parquet' (FORMAT parquet);

      publish:
        name: Upload when asked
        start_on:
          - collect
        if:
          condition: ${{inputs.hub_repo}}
          true: upload

  read_one:
    tasks:
      fetch:
        name: Fetch scan
        uses: swaleio/http-request
        args:
          url: ${{tasks.each_scan.for_each.item}}
          expected_status: "200"
          response_path: ${{env.WORKFLOW_STORAGE}}/scans/${{tasks.each_scan.for_each.index}}
      read_text:
        name: Read text
        uses: swaleio/tesseract
        start_on:
          - fetch
        args:
          image: ${{env.WORKFLOW_STORAGE}}/scans/${{tasks.each_scan.for_each.index}}
          output_base: ${{env.WORKFLOW_STORAGE}}/text/${{tasks.each_scan.for_each.index}}
          language: ${{inputs.language}}
      read_words:
        name: Read words with confidences
        uses: swaleio/tesseract
        start_on:
          - fetch
        args:
          image: ${{env.WORKFLOW_STORAGE}}/scans/${{tasks.each_scan.for_each.index}}
          output_base: ${{env.WORKFLOW_STORAGE}}/text/${{tasks.each_scan.for_each.index}}
          language: ${{inputs.language}}
          format: tsv

  upload:
    tasks:
      upload_dataset:
        name: Upload to the Hugging Face Hub
        uses: swaleio/hf-upload
        args:
          repo: ${{inputs.hub_repo}}
          repo_type: dataset
          source: ${{env.WORKFLOW_STORAGE}}/scans.parquet
          path_in_repo: data/train.parquet
          token: ${{secrets.hf_token}}
```

## Licenses of the defaults

- Scans: from [tesseract-ocr/test](https://github.com/tesseract-ocr/test), Apache-2.0.
