<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jan Ludwina</title>
    <description>The latest articles on DEV Community by Jan Ludwina (@miosp).</description>
    <link>https://dev.to/miosp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4235259%2Fcfaca791-a780-4e1e-b6b6-4a0da7126da7.png</url>
      <title>DEV Community: Jan Ludwina</title>
      <link>https://dev.to/miosp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/miosp"/>
    <language>en</language>
    <item>
      <title>Un-squashing my family photos with a 2.6M-parameter network</title>
      <dc:creator>Jan Ludwina</dc:creator>
      <pubDate>Mon, 09 Nov 2026 15:14:14 +0000</pubDate>
      <link>https://dev.to/miosp/un-squashing-my-family-photos-with-a-26m-parameter-network-4mp7</link>
      <guid>https://dev.to/miosp/un-squashing-my-family-photos-with-a-26m-parameter-network-4mp7</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://ludwina.top/blog/building-dejpeg" rel="noopener noreferrer"&gt;ludwina.top&lt;/a&gt;, where this post also has interactive charts and a tiling demo.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The story starts when I was roughly in middle school. Our family photo library was getting a bit obese - dozens of gigabytes felt like a lot back then. I started searching for a solution and found out about this little GUI around mozJPEG (probably this one &lt;a href="https://github.com/nibasya/MozjpegGUI" rel="noopener noreferrer"&gt;mozJpegGUI&lt;/a&gt;), that could magically turn megabyte photos into kilobyte photos. So I ran the whole collection through it at something like &lt;code&gt;quality=60&lt;/code&gt;. It took me years to understand just what I did at that time, and I've been trying to undo my mistakes since.&lt;/p&gt;

&lt;p&gt;My first proper attempt was &lt;a href="https://github.com/Miosp/ComfyUI-FBCNN" rel="noopener noreferrer"&gt;porting FBCNN to ComfyUI&lt;/a&gt;, which fixed the photos nicely, but &lt;a href="https://comfy.org/" rel="noopener noreferrer"&gt;ComfyUI&lt;/a&gt; was never meant to be a good batch processor for images. FBCNN also wasn't light. Even on my fairly beefy GPU it took seconds per picture, which meant days for the whole collection.&lt;/p&gt;

&lt;p&gt;Recently, I had to pick my master's thesis topic and I wanted to pick this exact problem - JPEG artifact removal. My promoter unfortunately turned down the idea and wanted me to go into NR-IQA (no-reference image quality assessment) instead. This gave me a final push to just it by myself. &lt;a href="https://github.com/Miosp/dejpeg" rel="noopener noreferrer"&gt;dejpeg&lt;/a&gt; is the result of this effort. My own restoration network and a public website that runs the model locally, live at &lt;a href="https://dejpeg.ludwina.top" rel="noopener noreferrer"&gt;dejpeg.ludwina.top&lt;/a&gt; for everyone to use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhfutv8l1s3jpqvzqq6kw.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhfutv8l1s3jpqvzqq6kw.webp" alt="A 512 by 512 photo crop shown twice: heavily JPEG-compressed on the left, restored by DeJPEGNet on the right" width="800" height="431"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;One of my photos, 512x512 crop. As PNG it is 394 KiB; as JPEG quality 10 with 4:2:0 subsampling, 10.2 KiB. Right side: DeJPEGNet's restoration, running in the browser.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What JPEG actually destroys
&lt;/h2&gt;

&lt;p&gt;JPEG cuts an image into 8x8 blocks and keeps a handful of cosine frequencies per block. Quantization throws away the high frequencies, more aggressively at low quality, and once they're gone a sharp edge no longer fits inside a block. What's left is blocky steps and mosquito ringing around edges.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdus4ucvtbaw7wf9glwkb.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdus4ucvtbaw7wf9glwkb.webp" alt="Two 4x zoomed crops of the same photo detail side by side: a clean one and one visibly broken into 8 by 8 blocks with color smears, DCT block grid overlaid in blue" width="800" height="425"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same crop before and after JPEG quality 10 with 4:2:0 subsampling, at 4x zoom. Blue lines mark the 8x8 DCT grid the codec works on.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The useful part is that JPEG is a fixed algorithm, so the damage is &lt;em&gt;structured&lt;/em&gt;. It always lands on the same 8x8 grid, always the same families of blur and ringing. None of that is perfectly invertible, the information really is gone, but a network can learn what the missing structure plausibly looked like without inventing new content. The plan naturally emerges from that: restore the codec's damage and don't repaint the photo. Having a small network gets you that almost for free: it can't hallucinate new details, it's simply too dumb to.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why not just run FBCNN in the browser
&lt;/h2&gt;

&lt;p&gt;The app actually started that way, running my ONNX port of &lt;a href="https://arxiv.org/abs/2102.02871" rel="noopener noreferrer"&gt;FBCNN&lt;/a&gt;. Nothing wrong with FBCNN, apart from its size (a bit unfit for a light browser app). I just knew that with a bit of research and good old trial-and-error I can achieve  better results than a 5-year-old network. I had a couple of assumptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I need something small - if it's going to run directly on people's computers, it needs to be efficient&lt;/li&gt;
&lt;li&gt;The size also should bring the benefit of simplified hosting (e.g. Cloudflare pages size limits)&lt;/li&gt;
&lt;li&gt;I can make the model more universal - remove flexible sizing in favor of tiling, remove flexible quality in favor of auto-guessing (I used auto in FBCNN anyways), and remove separate models for color and grayscale - it should perform well in both scenarios&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So: a single small model for everything, that can be compiled to native ONNX.&lt;/p&gt;
&lt;h2&gt;
  
  
  DeJPEGNet
&lt;/h2&gt;

&lt;p&gt;DeJPEGNet is a small three-level U-Net (2.6M parameters) built from &lt;a href="https://arxiv.org/abs/2204.04676" rel="noopener noreferrer"&gt;NAFNet&lt;/a&gt;-style blocks. Most of its design follows from one constraint: memory on the user's machine is limited, and you can't know how limited until you're running. Most commonly, this is solved by processing an image in tiles. This works best when the processing does not require global context of the image (e.g. composition, lighting, content) - a small crop of an image, which has some white texture in it can be both a piece of paper and a white wall. You do not know which one it is without looking at the rest of the image. But JPEG artifact removal is a local problem: the codec's damage is local, and the network can learn to fix it locally.&lt;/p&gt;

&lt;p&gt;My original plan was to just accept the tiling: lock the input to a constant 512x512 and build the tiling around that. Turns out I didn't need to. The architecture I ended up with has no global operation anywhere, so it works on any input size by itself. I kept tiling anyway, because smaller inputs save memory.&lt;/p&gt;

&lt;p&gt;What's inside the blocks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local channel attention (LSCA) pools over a fixed 32x32-pixel window. It's NAFNet's simple channel attention with a window, and the window is what makes tiles not matter: a pixel a thousand pixels away has no way to affect the output. Two overlapping crops of the same image produce identical pixels on the overlap, as long as the overlap is wider than the network's bounded reach (at the deepest U-Net level a 32px window spans ~128 input pixels, so a few hundred pixels is a probable real value).&lt;/li&gt;
&lt;li&gt;There is no ReLU or GELU anywhere. Nonlinearity comes from SimpleGate (split the channels in half, multiply the halves) and from multiplicative attention. Fewer element-wise ops and kernel launches, and the ONNX export gets easier.&lt;/li&gt;
&lt;li&gt;Each block's residual branches are scaled by zero-initialized &lt;code&gt;beta&lt;/code&gt;/&lt;code&gt;gamma&lt;/code&gt;, and the output head is a zero-init conv. At step zero the whole network computes the identity function, so it starts out doing nothing and only learns corrections. If it doesn't understand something, it should leave it alone.&lt;/li&gt;
&lt;li&gt;Training uses parallel 3x3, 1x1, and identity branches; at export they are algebraically turned into a single depthwise 3x3, the same structural re-parameterization &lt;a href="https://arxiv.org/abs/2101.03697" rel="noopener noreferrer"&gt;RepVGG&lt;/a&gt; uses for its deploy path.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwwukhof1dunwi58gx341.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwwukhof1dunwi58gx341.webp" alt="Diagram of the DeJPEGNet U-Net with encoder levels, bottleneck, decoder, skip connections and residual output on the left; anatomy of one NAFBlock with its two gated branches and local attention on the right" width="800" height="365"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Left: the full network - a 3-level U-Net, widths 40/80/160 channels, 2.6M parameters, output is input plus a zero-initialized residual. Right: one NAFBlock. The 32x32 attention window is what makes tiled inference seam-free.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The exported model is a 5.4 MB FP16 ONNX file with dynamic axes, so any image size that's a multiple of 16 px works with no re-export.&lt;/p&gt;
&lt;h2&gt;
  
  
  Training: 64 GB of photos and a degradation sampler
&lt;/h2&gt;

&lt;p&gt;There is no public paired dataset of squashed and clean images, and you wouldn't want one anyway, since it would bake in a single encoder's idea of degradation. So training pairs are synthesized on the fly from clean sources: 604 WebDataset shards (~64 GB) built from DIV2K, Flickr2K, and LIU4K-class corpora, plus ~18 GB of my own raw photos straight from my camera.&lt;/p&gt;

&lt;p&gt;Each sample takes a clean crop, applies a random degradation, and emits the &lt;code&gt;(degraded, clean)&lt;/code&gt; pair. The sampler mimics what actually happens to photos in the wild:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;quality factors sampled across 1–100, weighted toward the common 30–75 range&lt;/li&gt;
&lt;li&gt;chroma subsampling mixed 4:2:0 / 4:2:2 / 4:4:4, progressive JPEG 10% of the time&lt;/li&gt;
&lt;li&gt;clean PNGs re-saved as JPEG at random qualities between 1 and 100 (generation loss, since real images get re-saved all the time)&lt;/li&gt;
&lt;li&gt;random resizes before and after compression, mild noise, and a small crop jitter so the 8x8 grid lands at random phases like real crops do&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, 10% of pairs skip degradation entirely, because without that the model got too eager on near-clean inputs and 25% get converted to grayscale-replicated RGB, because otherwise the model learns that "JPEG artifacts are colorful" and starts hallucinating color into grayscale photos.&lt;/p&gt;

&lt;p&gt;The loss is L1 plus &lt;a href="https://arxiv.org/abs/1801.03924" rel="noopener noreferrer"&gt;LPIPS&lt;/a&gt;, a learned perceptual distance: roughly, how &lt;em&gt;different&lt;/em&gt; two images look to a human, rather than how many pixels differ. LPIPS runs on a random 128x128 crop rather than the full 256px patch, because full-patch LPIPS under bf16 autocast often returned NaNs and the crop stays numerically stable. Training uses the VGG flavor of LPIPS, while evaluation uses LPIPS-Alex and DISTS. Grading a network with the same feature extractor it trained against isn't a great way to get honest numbers.&lt;/p&gt;

&lt;p&gt;The rest is standard: AdamW at lr 1e-3, warmup then cosine decay, gradient accumulation to an effective batch of 16, an EMA of the weights (a slowly-updated smoothed copy, and the smoothed copy is what ships), bf16 plus &lt;code&gt;torch.compile&lt;/code&gt;, 150k iterations over 256px patches. I trained it locally on my Windows machine through WSL (much better ML library support than Windows), with the dataset sitting on an old 2 TB spinning disk.&lt;/p&gt;
&lt;h2&gt;
  
  
  The numbers: perceptually good, not really pixel-perfect
&lt;/h2&gt;

&lt;p&gt;Restorations are scored against pristine originals on the standard Classic5 and LIVE1 sets, re-encoded at quality factors (QF) 10–40 with 4:2:0, with FBCNN re-run under the identical protocol. &lt;a href="https://arxiv.org/abs/2004.07728" rel="noopener noreferrer"&gt;DISTS&lt;/a&gt; is a learned perceptual distance: lower is closer to how the difference looks.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;QF&lt;/th&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;FBCNN (35.7M)&lt;/th&gt;
&lt;th&gt;DeJPEGNet (2.6M)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;0.2412&lt;/td&gt;
&lt;td&gt;0.1938&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.1191&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0.1536&lt;/td&gt;
&lt;td&gt;0.1597&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.0911&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;0.1215&lt;/td&gt;
&lt;td&gt;0.1405&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.0743&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;0.1054&lt;/td&gt;
&lt;td&gt;0.1268&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.0642&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Classic5 benchmark, DISTS. Lower is closer to the original.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Look at the FBCNN column: from QF 20 up it scores worse than the compressed input it was given. It's trained to get pixels close, and chasing that smooths texture in a way the perceptual metric doesn't like. LPIPS agrees with DISTS here (Classic5 QF10: 0.1804 for DeJPEGNet vs 0.2594 for FBCNN).&lt;/p&gt;

&lt;p&gt;FBCNN does win where you'd expect, on PSNR, by about 1 dB (29.80 vs 28.81 dB at QF10). Decibels are logarithmic, so a 1 dB lead is easier to read as a ratio of error power relative to the compressed input. That's the price of picking a perceptual metric as the main one. A mild unsharp filter applied after restoration (default 0.10, tunable, off-able) buys back a good chunk of the sharpness at a negligible perceptual cost.&lt;/p&gt;
&lt;h2&gt;
  
  
  The app: tiles that shrink themselves
&lt;/h2&gt;

&lt;p&gt;The inference stack lives in &lt;code&gt;inference-core&lt;/code&gt;, a framework-free TypeScript package, so the worker, codecs, and tiling also work outside the Astro/Svelte app. ONNX Runtime Web runs the model in a worker, on WebGPU with WASM as the fallback.&lt;/p&gt;

&lt;p&gt;Large images are sliced into configurable tiles with tile/8 overlap and cosine-feathered back together. If a device doesn't have the memory for 512px tiles, the app doesn't crash, it tries again with a smaller tile size.&lt;/p&gt;

&lt;p&gt;One thing that surprised me is that batching multiple tiles per one iteration looked faster but actually was considerably slower on weak GPUs, so the shipped default is one tile for one iteration.&lt;/p&gt;

&lt;p&gt;There is an interactive demo of the tiling in the original post: you can move a tile across a damaged photo and shrink the tile size to watch the pass re-plan around more, smaller tiles. &lt;a href="https://ludwina.top/blog/building-dejpeg" rel="noopener noreferrer"&gt;Try it at ludwina.top&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I expected deployment to be the annoying part. Serving models (or fairly large files in general) is a pain without a CDN (classically paid), and I wanted to avoid that. I looked into a few ways to serve the model: Cloudflare's R2, GitHub Releases, HuggingFace, jsDelivr and some more. In the end the 5.4 MB model just fits in the static bundle, well under Cloudflare's 25 MB per-asset limit, so there's nothing to host separately and no CORS to fight. Additionally, everything is cached, so one download of assets makes the website work offline.&lt;/p&gt;
&lt;h2&gt;
  
  
  Where it falls short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you want pixel-exact restoration measured in dB, FBCNN wins by about 1 dB. This model is tuned for how the results look, not for averaging pixels.&lt;/li&gt;
&lt;li&gt;FBCNN has a "quality" knob. Is this useful? In my experience not really, since you normally just want the "auto" mode.&lt;/li&gt;
&lt;li&gt;The degradation sampler covers the common real-world mess (multi-pass saves, resizes, subsampling mixes), but it's still synthetic. Some encoder somewhere will surprise it.&lt;/li&gt;
&lt;li&gt;Compression can remove detail and this model does not hallucinate it back. That's mostly by design and cuts both ways: restored photos look softer than the original, but the model can stay small, because it never has to learn how to invent detail.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Steal it
&lt;/h2&gt;

&lt;p&gt;Everything is open (Apache-2.0): the &lt;a href="https://github.com/Miosp/dejpeg" rel="noopener noreferrer"&gt;app and training code&lt;/a&gt;, the &lt;a href="https://dejpeg.ludwina.top" rel="noopener noreferrer"&gt;live tool&lt;/a&gt;, and the model weights via GitHub Releases. There's also a plain Python package if browser inference isn't your thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dejpeg&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;restore_image&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# weights auto-download
&lt;/span&gt;&lt;span class="nf"&gt;restore_image&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;out.png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Training your own variant from any folder of clean images is one command, and since degradations are synthesized on the fly, your photo library is a dataset.&lt;/p&gt;

&lt;p&gt;If you manage to train a better model, please let me know. I would love to be able to use it.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>imageprocessing</category>
      <category>webgpu</category>
      <category>onnx</category>
    </item>
    <item>
      <title>One AVIF file, five opinions on color</title>
      <dc:creator>Jan Ludwina</dc:creator>
      <pubDate>Mon, 09 Nov 2026 14:07:53 +0000</pubDate>
      <link>https://dev.to/miosp/one-avif-file-five-opinions-on-color-d0h</link>
      <guid>https://dev.to/miosp/one-avif-file-five-opinions-on-color-d0h</guid>
      <description>&lt;p&gt;I'm a photographer. Well, more of a hobbyist, but still. However, my main interest in broad computer science invades my photographic choices heavily.&lt;br&gt;
From very early on I used webp, as an alternative to JPEG, then I jumped to AVIF when it became widely supported.&lt;br&gt;
It beats JPEG on quality per byte by a wide margin.&lt;/p&gt;

&lt;p&gt;The problem is: AVIF encoders suck. Well, they were never designed for still images, after all AVIF is a byproduct of the video world.&lt;br&gt;
Most AVIF encoders are slow, weirdly tuned out of the box for images, and not ergonomic at all.&lt;/p&gt;

&lt;p&gt;This is the story of &lt;a href="https://github.com/Miosp/avif-converter" rel="noopener noreferrer"&gt;avif-converter&lt;/a&gt;, the tool that came out of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  First generation
&lt;/h2&gt;

&lt;p&gt;My first attempt was very simple.&lt;br&gt;
I gambled that the encoder is not parallel enough to saturate the CPU, so I wrote a simple PowerShell script that ran a pool of avifEnc encoders in parallel and had better defaults set-up. It worked quite well, if you ignored some less important issues.&lt;/p&gt;

&lt;p&gt;I used it for a long time, but what in the end nudged me to write a second generation - I really like wide-gamut photos.&lt;br&gt;
When viewed on a right display, they can be much richer in color than standard ones.&lt;br&gt;
The problem is that AVIF is a byproduct of the video world. Depending on where I opened a file, colors came out slightly or very wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two ways to describe color
&lt;/h2&gt;

&lt;p&gt;A pixel is just numbers. Numbers mean nothing until you know the coordinate system they live in: what is "red", how a stored value maps to actual color.&lt;br&gt;
Unfortunately, two systems emerged:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ICC profile&lt;/strong&gt; — the print/imaging world's answer. A full binary blob describing the color space in detail. Flexible and widely supported in images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CICP&lt;/strong&gt; — the video world's answer. Described by three integers: RGB primaries ID (how colorful is color), which brightness curve (how to map values to brightness), and how color was converted to the YUV values the codec actually stores. Small, but has fixed vocabulary from ISO/IEC 23091-2 / ITU-T H.273.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ICC profiles are like verbose config files, while CICP is like a tuple of predefined values.&lt;br&gt;
Both can describe the same thing (*sometimes). However, we do not care what they can, we care about what they do.&lt;/p&gt;

&lt;p&gt;The issues arise here for AVIF, since because it came from a video codec, CICP is the de-facto standard for it.&lt;br&gt;
While you can embed an ICC profile, you never were meant to do that. This would not be a problem if not for the lack of support for CICP in image viewers.&lt;br&gt;
Some ignore it, some prefer it over ICC, and some do something totally different.&lt;/p&gt;

&lt;p&gt;From testing my files across the software I use:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Viewer&lt;/th&gt;
&lt;th&gt;Behavior with AVIF color&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Google Drive thumbnails&lt;/td&gt;
&lt;td&gt;ignore both ICC and CICP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows Photo Viewer (pre-2026)&lt;/td&gt;
&lt;td&gt;prefers CICP over ICC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows Photo Viewer (2026+)&lt;/td&gt;
&lt;td&gt;applies both CICP and ICC → double correction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;prefers CICP over ICC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Immich (sharp / Node.js)&lt;/td&gt;
&lt;td&gt;ignores CICP, uses only ICC&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;No single file can satisfy every viewer.&lt;/p&gt;

&lt;p&gt;In the end, back when I wrote the tool (in 2025), I chose to support the majority of viewers I tested for.&lt;br&gt;
It targets pre-2026 Windows, Chrome, and Immich.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the tool does
&lt;/h2&gt;

&lt;p&gt;From my experimentation, AVIF (and AV1 for that matter) is tuned for 10-bit, so no matter the input, the output is always 10-bit.&lt;br&gt;
Additionally, as I do not do HDR for now, the sRGB transfer curve is always used (this also has to do with the fact that some viewers, even when properly supporting wide gamut primaries, always expected the standard sRGB curve).&lt;/p&gt;

&lt;p&gt;However, for color itself, I decided to have two separate paths:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;sRGB input&lt;/strong&gt; → signal CICP (1, 13, 1). No ICC profile. Best support.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Everything else&lt;/strong&gt; → libvips transforms the pixels to BT.2020 primaries, keeping the sRGB transfer curve; signal CICP (9, 13, 1) and inject a Rec2020 ICC profile into the container. No encoder supports that, so the tool does the injection itself: hand-written ISO-BMFF surgery that appends a &lt;code&gt;colr&lt;/code&gt; box and re-points the item's associations and offsets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which path a file takes is decided by ExifTool: the ICC profile description first, then CICP primaries, then the EXIF color-space tag.&lt;br&gt;
If no metadata exists, sRGB is assumed, the same assumption every viewer makes.&lt;/p&gt;

&lt;p&gt;I decided this way since sRGB is the old standard. Old, but still the standard.&lt;br&gt;
Any other profile is a safe bet that the photo is wide-gamut (better to widen than to cut-off).&lt;br&gt;
Rec.2020 was the natural container: big enough to hold the majority of colors modern displays support.&lt;/p&gt;

&lt;p&gt;Rec.2020 also represents cleanly on both sides of the fence: CICP (9, 13, 1) and a matching ICC profile.&lt;br&gt;
It's the variant pre-2026 Windows renders correctly, and the injected profile (&lt;code&gt;Rec2020-elle-V4-srgbtrc.icc&lt;/code&gt;) comes from &lt;a href="https://github.com/ellelstone/elles_icc_profiles" rel="noopener noreferrer"&gt;elles_icc_profiles&lt;/a&gt;.&lt;br&gt;
The injection trick is borrowed. Adobe Photoshop also does some surgical modifications on AVIF and studying its output is where the whole idea came from. If Adobe ships files this way, the structure is clearly somewhat needed, even though no encoder CLI will write it for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoder settings
&lt;/h2&gt;

&lt;p&gt;Encoding runs in Python. This was not my first choice, but the availability of libraries and relative simplicity were very useful.&lt;/p&gt;

&lt;p&gt;Because we use a real programming language we can optimize the script, so that the pixels rarely touch the disk: the decoded pixels are piped straight from memory into &lt;code&gt;SvtAv1EncApp&lt;/code&gt; over stdin, unlike in the PowerShell version where every step required writing to a temp file.&lt;/p&gt;

&lt;p&gt;Why &lt;code&gt;SvtAv1EncApp&lt;/code&gt; now? I switched from &lt;code&gt;avifEnc&lt;/code&gt; because it is much more configurable and has lot of recent improvements.&lt;br&gt;
Since SVT-AV1 has many forks with features added, I landed on &lt;a href="https://github.com/Uranite/svt-av1-tritium" rel="noopener noreferrer"&gt;SVT-AV1-Tritium&lt;/a&gt; eventually for its psycho-visual features; the tool also works on upstream builds (more on that below). The settings and why:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--tune 3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The fork's still image tune (tuned on SSIMULACRA2 against the CID22 set). The fork's own recipe for still coding is exactly this plus &lt;code&gt;--avif 1&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--crf 23&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The quality dial. Fork docs suggest starting at 30 for Tune IQ; I spend the extra bits, stills deserve them.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--preset 5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Inside the fork's recommended 2–6 band for stills; quality has converged, one frame encodes fast regardless.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--input-depth 10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;See above.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;--color-primaries&lt;/code&gt;, &lt;code&gt;--transfer-characteristics&lt;/code&gt;, &lt;code&gt;--matrix-coefficients&lt;/code&gt;, &lt;code&gt;--color-range 1&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The CICP signaling itself. Fulle range because the default double-scales levels.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-n 1 --avif 1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A single-frame AVIF encode&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Plus the fork-only psycho-visual flags, used when the local encoder supports them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--tx-bias 1&lt;/code&gt; — biases transform decisions toward sharpness and detail retention (fork docs warn of possible blocking/banding in exchange; at CRF 23 there's headroom).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--complex-hvs 1&lt;/code&gt; — swaps in a more elaborate visual model during mode decisions, so the encoder optimizes for what eyes notice.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--noise-adaptive-filtering 1&lt;/code&gt; — detects noise and backs off the smoothing filters (CDEF/restoration), keeping photographic grain instead of smearing it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--hbd-mds 1&lt;/code&gt; — forces the internal mode decisions themselves to 10-bit, on the same logic as the input depth.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those flags don't exist upstream: a stock &lt;code&gt;SvtAv1EncApp&lt;/code&gt; rejects them outright. So the tool parses the encoder's &lt;code&gt;--help&lt;/code&gt; once, uses every supported flag, and warns about anything dropped. Same command line, upstream or fork: never fails, just different quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  An end to the madness
&lt;/h2&gt;

&lt;p&gt;The clean fix is switching to a modern format where color signaling is more standardized or just uses ICC. &lt;a href="https://developer.chrome.com/blog/chrome-155-beta" rel="noopener noreferrer"&gt;Chrome 155&lt;/a&gt; (stable October 6, 2026) finally &lt;a href="https://chromestatus.com/feature/5114042131808256" rel="noopener noreferrer"&gt;shipped JPEG XL decoding enabled by default&lt;/a&gt;; a format designed with wide gamut, HDR, and high bit depth as first-class citizens. This is the biggest step forward for JPEG XL adoption and may finally make it a viable alternative to AVIF for still images.&lt;/p&gt;

&lt;p&gt;Once it's widespread enough, I'm migrating my tools to it and deleting most of these hacky workarounds.&lt;/p&gt;

&lt;p&gt;Full source and usage: &lt;a href="https://github.com/Miosp/avif-converter" rel="noopener noreferrer"&gt;github.com/Miosp/avif-converter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>avif</category>
      <category>imageprocessing</category>
      <category>color</category>
      <category>python</category>
    </item>
  </channel>
</rss>
