Lossless JPEG Crop

How to perform a crop to a JPEG image and save it back as a JPEG without any loss (or compression) using fast.ai library?

Because img.px[:, 0:256, 0:256].save("cropped.jpg") saves at quality=75 as per PIL’s documentation at https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html#jpeg. I neither want to increase nor decrease the quality of the original image after a crop.

Looks like only imagemagick and jpegtran provide lossless JPEG crop functionality. Analyzed fastai, skimage and PIL libraries as well for copping operation.

Example: jpegtran -crop 256x256+0+0 -outfile cropped.jpg american_pit_bull_terrier_72.jpg

Proved this hypothesis 2 ways at https://colab.research.google.com/drive/1_RjdPwEjsgi9MmfHb5CxvvRBEnogcnOG

  1. Estimate the size of compressed file using the dimensions and see which tool comes close for a fixed image
  2. If crop is lossless, ratio of original & cropped image should be the same with and without compression (or rather any other deterministic transformation)

What do you folks think?