Skip to content
all posts

Upscaling old photos in Immich

6 min readSubscribe via RSS

I've got a batch of old family photos that only exist as tiny JPEGs. The originals are long gone, they were taken on whatever camera or phone was around at the time, and what's left is 215 by 161 pixels and under 10 KB each. Immich stores them fine. It can't make them any bigger, and nothing I could find for Immich does.

I didn't want to use AI like LLMs which alter the images. I wanted something which could just scale up the images I had in Immich and try to fill in the gaps. So I built a job that does that and nothing else, and this is how to run it on your own Immich.

What it does

You tag a photo enhance in Immich. Once an hour a CronJob picks up everything with that tag, downloads the original, and makes two copies of it:

  • an upscale from Real-ESRGAN, four times as wide and as tall;
  • a pixel-doubled copy at the same size, where every original pixel becomes a 4 by 4 block.

Both go back into Immich with the original's dates, favourite flag and albums, and get stacked on the original with the upscale on top. The original is never edited, re-encoded or deleted. Then you open the stack, look at the three, and delete whichever you don't want. The job never revisits a stack it has made.

Side by side at 860 by 644 pixels: on the left the 215 by 161 sample cat photo pixel-doubled four times, every original pixel a visible square block; on the right the same photo upscaled four times by Real-ESRGAN, with smooth fur and sharp eyes

The pixel copy is there because the upscale isn't always better. On some photos the blocky version is the honest one, and I'd rather decide per photo than have the job decide for me.

Photos up to 1 megapixel get the 4 times treatment, up to 4 megapixels get 2 times, and anything bigger is skipped. The whole point is the tiny ones.

The part I deleted

The first version also restored faces. GFPGAN finds each face, aligns it, and runs it through a network that has been trained on what faces look like. On paper that's exactly what a 15-pixel face needs.

In practice it gave me a convincing photo of a stranger. A face that small has no information left in it, so the model fills it in from its training, and what comes out looks like a person, just not the person in the photo. There are no reference materials for how that face should look. On larger faces, around 40 pixels across, it was closer, but at full strength it added what looked like a pair of glasses.

The upscaler on its own doesn't do that. It sharpens what's there. Faces stay soft, but they stay the same people. So the face pass came out entirely: the code, the three model files and the libraries only they needed. The image went from 1.1 GB to 350 MB, and every non-commercial licence went with it. The work isn't lost. It's in the git history and a shelved plan, for the day someone finds a model that can keep a person's identity from 15 pixels.

Deploying it

The image is ghcr.io/ralton-dev/immich-enhancer, built for amd64 and arm64, CPU only. The model weights are baked in, so the container needs no internet. It needs two things from you: an Immich API key, and the environment variables below.

The API key

Make the key in Immich under Account Settings, API Keys, logged in as the account that owns the photos. Keys and tags are per user, so the job only ever sees that account's library. Give it exactly these permissions and nothing else:

asset.read  asset.download  asset.upload  stack.create
album.read  albumAsset.create  tag.create  tag.asset

The job checks the key's permissions before it writes anything, and stops with a list of what's missing if the key is short.

Settings

Variable Default What it does
IMMICH_URL http://immich-server.immich.svc.cluster.local:2283 Your Immich server. Requests go to /api under it
IMMICH_API_KEY required The key above
APP_VERSION required Any string. It's logged and goes in the summary line, so I set it to the image tag
ENHANCER_SR_MODEL RealESRGAN_x4plus Or realesr-general-x4v3, about ten times faster and softer. Both are in the image
ENHANCER_MAX_PER_RUN 10 Photos attempted per run
ENHANCER_TIME_BUDGET_S 2700 No new photo is started after this many seconds
ENHANCER_THREADS 4 CPU threads. Set OMP_NUM_THREADS to the same number; the image leaves it unset on purpose
ENHANCER_DRY_RUN false true downloads and upscales but writes nothing back

The full table is in the repo's README.

Without Kubernetes

Any scheduler will do, because a run is one container that exits. This is enough from a box that can reach Immich, in cron if you like:

docker run --rm \
  -e IMMICH_URL=http://immich-server:2283 \
  -e IMMICH_API_KEY=... \
  -e APP_VERSION=latest \
  -e OMP_NUM_THREADS=4 \
  ghcr.io/ralton-dev/immich-enhancer:latest

On Kubernetes

Mine runs as a CronJob on the NAS node, which has the spare CPU, under the restricted Pod Security Standard. This is the shape of it, with the ConfigMap holding the settings table above and the key in a Secret:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: immich-enhancer
spec:
  schedule: "10 7-23 * * *"
  timeZone: Europe/London
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      activeDeadlineSeconds: 3300
      backoffLimit: 0
      ttlSecondsAfterFinished: 86400
      template:
        spec:
          restartPolicy: Never
          automountServiceAccountToken: false
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1000
            seccompProfile: { type: RuntimeDefault }
          containers:
            - name: enhancer
              image: ghcr.io/ralton-dev/immich-enhancer:sha-<full sha>
              envFrom:
                - configMapRef: { name: immich-enhancer-config }
              env:
                - name: APP_VERSION
                  value: sha-<full sha>
                - name: IMMICH_API_KEY
                  valueFrom:
                    secretKeyRef: { name: immich-enhancer-api-key, key: IMMICH_API_KEY }
              securityContext:
                allowPrivilegeEscalation: false
                readOnlyRootFilesystem: true
                capabilities: { drop: [ALL] }
              volumeMounts:
                - { name: tmp, mountPath: /tmp }
              resources:
                requests: { cpu: "2", memory: 4Gi }
                limits: { cpu: "4", memory: 6Gi }
          volumes:
            - name: tmp
              emptyDir: { sizeLimit: 2Gi }

Two things in there will bite you. backoffLimit: 0 is deliberate, because a retry would re-attempt the same photos and the next hour is the retry anyway. And ttlSecondsAfterFinished matters if you alert on failed Jobs: without it a stale failed Job keeps the alert up for good.

The only network the pod needs is DNS and Immich on port 2283. If you run NetworkPolicies, that's the whole allow list. The first ping from a fresh pod can get connection refused while the policies are still being programmed; the job retries six times, five seconds apart, so it rides that out.

Running it

Tag some photos and watch the log. A run looks like this, trimmed:

INFO immich-enhancer sha-052df73…
INFO Immich answered ping
INFO Immich server v3.3.5
INFO 2 asset(s) tagged 'enhance'
INFO asset … downloaded 9522 bytes
INFO enhanced scale=4 in=215x161 out=860x644 seconds=14.00 peak_rss_mib=863
INFO asset … uploaded the enhanced copy as …
INFO asset … uploaded the pixel copy as …
INFO asset … done, stacked under … (enhanced) and … (pixel)
{"event": "summary", "processed": 2, "succeeded": 2, "failed": 0, "skipped": 0, "seconds": 34.2, "exit_code": 0}

On my NAS, an Intel N95 with four cores, a 215 by 161 photo takes 13 to 14 seconds at under 1 GB of memory. The first photo pays for loading the model, about a third of a second.

The job exits 1 only when something systemic fails: the key, the ping, the tag upsert or reading the queue. One bad photo doesn't fail the run. It gets tagged and the run carries on. That's so the stock KubeJobFailed alert pages for the runs that need a person and not for one odd file.

The tags are the whole interface:

Tag On Means
enhance a photo you queued waiting for the next run
enhanced-source the original, once done its copies exist and the job won't touch it again
enhanced the Real-ESRGAN copy top of the stack
pixel-x4 the pixel copy every pixel a block
enhance-failed the original the run's log says why; fix it and tag enhance again
enhance-skipped the original not something it handles

Skipped means a video, anything that isn't JPEG or PNG, a photo over 4 megapixels, a photo already in a stack, or one that has been edited inside Immich, because the download is the unedited original and copies of that would undo the edit.

What's left

The job holds one user's key, so it only sees one library. Another user's photos need their own key and a second schedule. HEIC isn't handled yet, though a modern phone photo is over 4 megapixels and would be skipped on size anyway. And the choosing is still by hand. A keep tag the job could act on would be nice, but it needs a key that can delete, and the one thing I like about the current key is that it can't.