System designExplainer

JPEG file size bigger after a re-save: set quality below 100

Why a decoded JPEG re-save has a bigger file size: quality 100 recompression keeps everything. Set quality 80-90 and the file shrinks below the original.

LumaSpec Vision9 min read Share on LinkedIn

On this page 10 sections
  1. 01What decode and re-save do to the bytes
  2. 02Why quality 100 writes a bigger file
  3. 03Worked example: 100 kB in, 300 kB out
  4. 04Quality 100 is still not lossless
  5. 05Set the quality once, where the pixels are final
  6. 06What the growth costs over a run
  7. 07Fix and verify
  8. 08When a bigger file is the right answer
  9. 09The decision, per case
  10. 10Questions

You decode a JPEG, touch the pixels, save it again, and the file triples: 100 kB in, 300 kB out. Nothing in the image looks different, so the extra bytes look like a decoder bug. They are not. The second file is bigger because the save call re-encoded the raw pixels at quality 100, the encoder's maximum, and the original compression was discarded at decode time. What follows: what decode and re-save do to the bytes, the arithmetic behind the threefold growth, and the quality value that brings the size back down. It applies to any pipeline that decodes a JPEG to a bitmap and writes it out again — Android utilities, OpenCV resize tools, snapshot steps in a vision system.

What decode and re-save do to the bytes

A JPEG file does not store pixels. It stores quantized DCT coefficients: the image cut into 8 × 8 blocks, transformed, divided by a quantization table, and packed into a bitstream. That division is where the compression comes from, and it is why a photo that fills megabytes as raw pixels fits in a 100 kB file.

Decoding runs the inverse path: coefficients are multiplied back, the transform is inverted, and plain pixels land in a memory buffer. On Android the default buffer is ARGB_8888, four bytes per pixel. At this point every compression decision the original encoder made is gone; you hold raw pixels and nothing else.

Saving runs a fresh encode from those pixels. The quality argument scales the quantization tables: low values mean coarse tables that zero out most high-frequency coefficients, high values mean fine tables that keep nearly everything. The size of the new file is chosen by this save call alone. The original file's byte count plays no part after decode.

decoded buffer = width × height × bytes per pixel ARGB_8888 = 4 bytes per pixel, RGB_565 = 2 bytes per pixel growth factor = file size at quality 100 / original file size

Why quality 100 writes a bigger file

Quality 100 tells the encoder to quantize as little as possible. In the libjpeg-derived encoders behind Android's bitmap save and OpenCV's image write, quality 100 scales the tables to their finest step, so nearly every coefficient survives into the bitstream. The re-encoded file keeps detail the original encoder had already thrown away on purpose. That is the entire mechanism behind 100 kB in and 300 kB out: 300 kB ÷ 100 kB = 3×.

The original file was small because its encoder ran a coarser table. The re-save discards that compression work and rebuilds the file with maximum fidelity to the decoded pixels. Identical image content, triple the bytes, decided by one argument.

In the failing pipeline the resize routine passed a quality value through CV_IMWRITE_JPEG_QUALITY, and the save routine then hard-coded 100 in bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut). One number did all the damage, and it sits in the most ordinary place: the save helper everyone reuses.

Worked example: 100 kB in, 300 kB out

The case behind the question: a resized photo written to org.jpg at 100 kB, decoded with BitmapFactory.decodeFile, and written back as result.jpg by that compress call at quality 100. Result: about 300 kB, a growth factor of 3.

Memory during the round trip is a separate budget. Take the resized image as 1600 × 1200 pixels:

  • ARGB_8888: 1600 × 1200 × 4 bytes = 7,680,000 bytes, about 7.7 MB held in RAM
  • RGB_565: 1600 × 1200 × 2 bytes = 3,840,000 bytes, about 3.8 MB

The decode buffer is what fills memory; quality sets the file size afterwards. For the same bitmap at lower quality values, typical sizes for photo-like content (run the save on your own file for exact bytes):

Save quality Typical size Against the 100 kB original
100 ~300 kB 3× bigger
90 ~140 kB 40% bigger
85 ~100 kB back to the original
80 ~80 kB 20% smaller
70 ~55 kB about half

Ratios are typical, not exact: scene detail and encoder differences move them, which is why the test below measures your own file. The fix is one argument and one test:

int quality = 85; // 0-100, was hard-coded 100
resultBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fOut);
// re-run on the same bitmap: result.jpg drops from ~300 kB to ~100 kB

Quality 100 is still not lossless

Paying the maximum byte count does not buy a bit-exact copy. The 8 × 8 transform itself rounds, and many encoders keep 4:2:0 chroma subsampling at every quality value, so color resolution is halved before quantization even starts. At quality 100 the error per cycle is small, but it is not zero — and the file is three times the size.

If the copy must be bit-exact, use a format that is: PNG for masters you will edit again. If the copy must be the original file, do not decode it at all.

When the capture device itself writes the JPEG, the same decision moves upstream: set the quality once, where the pixels are born.

Set the quality once, where the pixels are final

Every extra decode-recompress is a second lossy generation and a second place where a hard-coded 100 can hide. The clean structure keeps exactly one encode, at the point where the pixels are final: right after capture, or after the last processing step. Everything downstream then moves and stores bytes instead of re-deciding the image.

Machine vision hardware starts you ahead: area scan cameras deliver raw frames over GigE or USB3 instead of an in-camera JPEG, and the piece on why machine vision cameras stay raw gives the reasons. Where the pipeline needs pixels for processing, capture unencoded frames and encode your own JPEG afterwards at the chosen quality. If the camera module itself writes the JPEG, set its quality at capture: configuring JPEG capture for consistent quality covers that side of the same setting.

What the growth costs over a run

Size multiplies quietly across an archive or a link. One camera writing 1,000 snapshot JPEGs per day:

  • at 300 kB each: 300 kB × 1,000 = 300,000 kB = 300 MB per day, about 9 GB per 30-day month
  • at 100 kB each (quality 85): 100 MB per day, about 3 GB per month

The same ratio hits a network. Ten frames per second at 300 kB is 300 kB × 10 = 3 MB/s, and 3 × 8 = 24 Mbit/s; at 100 kB it is 1 MB/s, or 8 Mbit/s. On a shared link carrying PLC traffic, that difference shows up as jitter long before the disk fills. Moving snapshot JPEGs over HTTP to a PLC works the same way: the bytes you write are the bytes the network carries, so quality is a bandwidth setting too.

Fix and verify

  1. Find every save call on the path and read its quality argument. Change each 100 to 85.
  2. Re-run the save on the same decoded bitmap and compare byte counts. The 300 kB result.jpg should return to roughly the original 100 kB.
  3. Open the new file at 1:1 and at 400% zoom on text, edges and flat areas. At 85 the difference should be invisible; if fine text or barcode edges suffer, use 90.
  4. Where pixel values feed a measurement, compare decoded output against the master pixel buffer before and after the change, with a tolerance in gray levels. Tune by numbers, not by eye.
  5. Search the whole project for compress and cvSaveImage calls, not just the first one you found. A single leftover 100 restores the growth silently.
  6. If the quality number must travel between libraries, retest per library: 85 in one encoder does not produce the same bytes as 85 in another.

When a bigger file is the right answer

A larger file is the right answer only when it buys something you named in advance.

  • A master you will edit repeatedly: save PNG once, render every JPEG from it. PNG is bigger and lossless by design; the size is the point.
  • A pixel-exact handoff: pass the original encoded bytes through untouched instead of decode and re-save.
  • A decode you genuinely need downstream: keep it close to where it is used, as the piece on JPEG decode placement in a GPU pipeline argues. Decoding early and re-encoding everywhere multiplies both generations and bytes.
  • Raw frames feeding measurement: store the raw or lossless frame, encode presentation copies on demand.

Decoding itself has traps worth knowing before you restructure the path: the raw-pixel decode fix covers the case where a decoder is handed pixels it expected already encoded.

The decision, per case

Re-saved JPEG bigger than the original means the save call ran at quality 100. Set 85 for inspection snapshots, 70-80 for previews, and never re-encode a JPEG you could have copied byte for byte. Keep exactly one encode in the pipeline, where the pixels are final, and keep raw frames wherever pixels feed a measurement. For the same 100 kB image: quality 100 writes about 300 kB, quality 85 returns it to about 100 kB, quality 70 lands near half.

If you are choosing the capture side too, the camera finder filters by interface, resolution and shutter, the configurator starts at the camera step, and the team answers setup questions through the contact page. More decode-and-storage fixes sit on the systems topic page.

Questions

Is JPEG quality 100 lossless?

No. At quality 100 most encoders flatten the quantization tables to their finest step, but the transform round-off and any chroma subsampling remain, so decoded pixels still differ slightly from the source. Quality 100 buys the largest file the encoder can produce, not a bit-exact copy. For a bit-exact copy use a lossless format such as PNG, or pass the original encoded bytes through untouched.

What quality should I use when re-saving machine vision snapshots?

Use 85-90 for snapshots a person or a matcher inspects: the size lands near or below the original with no visible loss. Use 70-80 for thumbnails and previews. If pixel values feed a measurement, skip JPEG recompression entirely and keep the raw or lossless frame; a lossy re-encode is the wrong storage for metrology data.

Does decode-recompress loss compound if I re-save many times?

Yes. Each cycle decodes the previous file's pixels and encodes them again, so small errors stack. At quality 85-90 one or two cycles stay invisible; after ten or more, blocking and ringing around edges become measurable. Never build an edit loop that re-saves a JPEG on every iteration: keep a lossless master and render each output from it, so generation loss stays at one generation.

Why did my re-saved file come out the same size or even smaller?

Size follows the encoder, the quality value and the chroma subsampling, not the file's history. A different encoder, a downscale before saving, or a moderate quality value can all produce a smaller file than the original. An identical size usually means both encoders ran similar tables. Quality numbers are not comparable across libraries, so calibrate by testing your own images rather than trusting the number.

How much memory does a decoded JPEG bitmap occupy?

Width times height times bytes per pixel: 4 bytes per pixel for ARGB_8888, 2 for RGB_565. A 1600 × 1200 image occupies 7.7 MB as ARGB_8888 and 3.8 MB as RGB_565. A 4000 × 3000 frame reaches 48 MB as ARGB_8888, which is why decode buffers, not compressed files, are what fill memory in image utilities.