Since JPEG images are compressed in chunks of 8x8 pixels, if a JPEG has a width or height that is not divisible by 8, is it technically possible to remove the 1-7 pixels of the outer edge losslessly?

(Or to crop outer edges of any size, for that matter, as long as the inner 8x8 blocks are intact.)

2 Answers 2


Based on the jpegtran man page (and Wikipedia), the top left of a JPEG image must be at the corner of an 8x8 block, so it's possible to crop single pixel lines/columns from the bottom and the right edges, but not from the left or the top. Full 8x8 blocks can of course be cropped from the left and top too.

The man page mentions this under the -crop command:

Like the rotate and flip transforms, lossless crop is restricted by the current JPEG format; the upper left corner of the selected region must fall on an iMCU boundary. If it doesn't, then it is silently moved up and/or left to the nearest iMCU boundary (the lower right corner is unchanged.)

  • That's brilliant! Are there free CLI tools and image editors that automatically round the width and height down to a product of 8 -- and possibly also help you achieve a certain aspect ratio? Eg. take a JPEG that was previously unevenly cropped, and get it back to full 8x8 blocks and 4:3 ratio?
    – forthrin
    Apr 6, 2018 at 4:29
  • @forthrin, jpegtran has the -trim switch to "Drop non-transformable edge blocks", which I think would mean rounding down to 8. I can't see anything about automatically fixing the aspect ratio, but I don't know about other tools. With -crop you'd need to calculate the size manually.
    – ilkkachu
    Apr 6, 2018 at 4:47
  • I tried -trim on an image, but it had the same size afterwards. I think it's related to the adjacent option -transform, and I don't know exactly what it does.
    – forthrin
    Apr 6, 2018 at 6:20

The top and left edges of a jpeg image are always on a block boundary. The bottom and right edges may or may not be.

So you have free choice of the bottom and right edges of your losslessly cropped image but the top and left edges of the cropped image must be an integer multiple of the effective block size from the original top and left edges.

The effective block size of JPEG images varies. The DCT works on 8x8 blocks but some JPEGs are chroma-subsampled making the effective block size 8x16 or 16x16.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.