Skip to main content

PhotoCraft now shows how big a JPEG will be before you save it

Updated 6 min readMarkdown
PhotoCraft now shows how big a JPEG will be before you save it

You pick a quality for a JPEG, press OK, then look at the file. Too big for the email you meant to send. So you save again and check again. Until recently, that was the only way to learn how big a JPEG from PhotoCraft would be.

PhotoCraft is an open source photo editor written in Rust. Issue #7843, from Stargazer7780, asked for two things: a way to set JPEG quality by hand, and an idea of the file size. Quality had already arrived with the JPEG Options dialog in #7883; the project's docs still listed the size preview as missing. I built that part, and it was merged in storytold/photocraft#9952.

A quick word on JPEG

JPEG is the usual file format for photos. It keeps files small by throwing away fine detail your eye is unlikely to notice, and the quality setting decides how much goes. In PhotoCraft's dialog, Quality runs from 0 to 12, and three format choices (Baseline, Optimized, Progressive) change how the data is packed. Higher quality takes more space, and how much more depends on the photo: a clear blue sky packs down far smaller than a busy forest.

The real size, not a guess

A formula could guess the size from pixel count and quality. But the dialog already knows everything the save will use, so the readout simply does the save in memory. It encodes the picture (turns its pixels into JPEG bytes) through the same export call the real save makes:

pub(super) fn encoded_size(doc: &Document, settings: &ExportSettings) -> Option<u64> {
    let bytes = photocraft_io::export(doc, "size.jpg", &settings.io_options()).ok()?.bytes;
    u64::try_from(bytes.len()).ok()
}

The name "size.jpg" only tells export which format to produce. The bytes stay in memory; the function returns their count, or None if encoding failed. Because these are the save's own settings, the colour profile (a note telling other apps how to show the colours) and the metadata (extra details stored with the photo) count too.

The encoder uses a finer quality scale, 1 to 100, and the dialog's 13 steps map onto it. When you reopen a JPEG and leave the slider on the step its own quality falls on, OK saves with the file's exact original quality. The readout does the same, so its number still matches the file.

Showing the number

The size appears in dim text to the left of OK and Cancel: "152.3K" under a megabyte, "1.5M" above, counting in units of 1024. Hover it and you see "Estimated file size". The formatting had one small trap (trimmed):

let kilo = (bytes as f64 / K * 10.0).round() / 10.0;
if kilo < K { format!("{kilo:.1}K") } else { format!("{:.1}M", bytes as f64 / (K * K)) }

K is 1024. The first line turns bytes into kilobytes rounded to one decimal, and only then does the second pick the unit. Picking first would let a file a hair under a megabyte pass the "under 1024" test and then display as "1024.0K". Rounding first makes it "1.0M".

Keeping the dialog smooth

Encoding a big photo takes time, and the dialog mustn't freeze while you drag the slider. egui, the library PhotoCraft draws its windows with, redraws the window many times a second; each redraw is a frame. In the desktop app the encode runs on a worker thread, a separate line of work beside the one drawing the window, as Export As already does for its exact size. When the worker finishes, it asks egui to redraw.

PhotoCraft also runs in a web browser, and that build has no threads, so there the encode happens inside a frame. Hence a lower limit: 4 megapixels (4 million pixels) on the web, 40 on desktop. Bigger documents show —.

Answers are cached (remembered) under a key:

pub(super) struct Key {
    doc: DocId,
    revision: u64,
    quality: u8,
    format: JpegFormat,
}

The revision is a counter that goes up with every edit. If nothing changed since the last frame, the key is the same and nothing gets encoded. Only one encode runs at a time, so dragging across all 13 steps never piles up workers. All of this lives in a new file, size_readout.rs.

What review changed

CodeRabbit, a review bot, left three notes marked minor on my first push, all fair. The docs said the encode runs off the window's thread, which isn't true on the web. Only one finished size was kept, so stepping back to a quality you'd just seen encoded the photo again. And while a new size was being worked out, the old number stayed up, looking like the answer for the new settings.

My follow up commit, "keep recent sizes, mark a pending one", fixed all three. The last 8 sizes are kept, so going back is instant. An old size standing in for one still being computed gets a trailing ellipsis, "152.3K…", and the docs now describe the web build correctly. CodeRabbit marked its threads resolved after that push.

The tests check, among other things, that each size equals the bytes the save writes, and they open the real dialog through kittest, a tool that drives egui windows in tests. The six in my first push failed against a placeholder version and passed with the change; the follow up added two.

Two weeks of merging main

That was the last feature commit, yet the PR stayed open from 28 October to 12 November. It also touched shared spots: doc status lines, a row in each of 21 translation catalogs (files holding the on screen text for one language each), and the contributors list behind the About window. Other PRs kept editing those same lines in main, the shared branch, and when two branches change one line, a person has to settle that merge conflict by hand. The branch ended up with 20 merges of main: 11 from me and 9 from davidmann4.

On 7 November davidmann4 called the size readout "a really nice addition to JPEG Options", explained the conflict, and offered to do the merge. After that they merged main in themselves several times, never rewriting my commits. On 10 November they asked me to resolve the next one, and I did. On 11 November main moved the catalogs to a separate repo, storytold/craft-i18n, deleting the files my 21 rows lived in, so the tooltip shows in English until that repo has the row. davidmann4 merged the PR on 12 November 2026. Thank you, davidmann4, for carrying it through.

What I'm keeping from this one

When the real answer is cheap enough to compute, compute it instead of estimating. The save's own encoder can't disagree with the file it writes.

And putting the feature in its own file kept its code out of the conflicts, apart from one doc comment. The rest came from shared lists, and those deserve a plan of their own.

Do you show file sizes before saving in your own apps? I'd like to hear how you keep that number honest.

About the author

  • I contribute to open source developer tools: Tauri, Biome, oxc, Julia, Quarkus, Grafana, Burn and a few more. I start from an issue I can reproduce and a test that fails, then fix it. Here I write up the ones worth telling: what broke, why, and how the fix got through review.

No comments yet. Be the first to comment.