4

I have a 1400x1400 image in which I want to trim 4 pixels to the left, 1 at the bottom and, to keep square proportions, 3 from the top. The problem is, whenever I perform a crop with jpegtran, pixels get removed from the bottom-right, no matter what I do. For instance, testing just the left part, I tried:

jpegtran -perfect -crop 1396x1400+0+0 -outfile crop.jpg image.jpg

but that just removes 4 pixels from the right;

jpegtran -perfect -crop 1396x1400-4+0 -outfile crop.jpg image.jpg

and that again removes 4 pixels from the right;

jpegtran -perfect -crop 1396x1400+4+0 -outfile crop.jpg image.jpg

and that does not remove any pixels at all; more in general,

jpegtran -perfect -crop 1396x1400+x+0 -outfile crop.jpg image.jpg

jpegtran -perfect -crop 1396x1400-x+0 -outfile crop.jpg image.jpg

with x between 0 and 4, does respectively remove 4-x and x pixels from the right. With x higher than 5, obviously gives an error. I couldn't achieve any left trim. Can anyone help me?

I'm using jpegtran from libjpeg-turbo version 1.4.2 (build 20151205) on an Arch Linux x86_64 system.

4

jpegtran can't losslessly cut at any finer increment than the Minimum Coded Unit size, which varies depending on the channel and the chroma sampling mode. It's going to be 8×8, 16×8, or 16×16.

This means the minimum cut size must be an even multiple of 8 or 16, depending on the way the image was encoded. The exceptions are the right and bottom edge when the image dimensions are not an even multiple of 8 or 16px. In that case, you have partial blocks on the right and/or bottom edges. For example, a 17×17 pixel image could have 1px cut from its right or bottom edges, but not 2px from those edges, nor 1px from the top and left edges.

Anything else requires re-encoding of the cut blocks.

Quoting from the manual:

...lossless crop is restricted by the current JPEG format: the upper left corner of the selected region must fall on an iMCU boundary. If this does not hold for the given crop parameters, we silently move the upper left corner up and/or left to make it so, simultaneously increasing the region dimensions to keep the lower right crop corner unchanged. (Thus, the output image covers at least the requested region, but may cover more.)

  • Do you mean that, in order to do a real lossless crop, I have to cut at least 32 pixels both horizontally and vertically? – Jeffrey Lebowski Mar 30 '16 at 10:32
  • Further sub-question: I tried cutting 16 pixels from the top-left and it worked. I tried cutting 15 and it cut 8. Does it mean ICUs in my image are 8x8? – Jeffrey Lebowski Mar 30 '16 at 10:39
  • I've edited the answer to be clearer about the effect of MCU size on the limitations of jpegcrop. – Warren Young Mar 30 '16 at 10:45
  • Apart from the fact I wrote ICU instead of iMCU, I got it, lossless jpeg manipulation obviously has some limits depending on how is your image structured. Thanks! – Jeffrey Lebowski Mar 30 '16 at 10:48
  • Is there some way to force jpegtran to crop the exact amount specified (being lossy as needed), rather than rounding it to the nearest MCU boundary (which seems to be the default)? – Michael Sep 2 '17 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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