Skip to content Skip to sidebar Skip to footer

Why Does Pil.image.rotate() Results In Loss Of Quality Despite `resample=image.bicubic`

Per https://stackoverflow.com/a/17822099/1639359 using resample=PIL.Image.BICUBIC should retain quality through rotation. This question is follow up from my comment on the above a

Solution 1:

In general, any image rotation is going to result in a loss of image quality simply because the output requires an interpolation of the input for pixels that don't map 1:1 from input to output. For rotations of multiples of 90 degrees, it's possible to get a 1:1 mapping and do a lossless rotation. It's not clear if PIL/Pillow is smart enough to do that, but it might. Your example rotates by -90 degrees so you could luck out.

The metadata is being lost because rotate generates a new image, it doesn't modify the existing one. It's not easy to determine which metadata might be relevant for the new image, so the safest course of action is to simply not copy any of it. As you've discovered, it's possible to manually copy anything you think is important. I don't think there's an option to automatically copy anything.

As noted in the comments, using file size as a judge of image quality is a very poor metric. A smaller size is suggestive of poorer quality, but it doesn't have to be so; the whole premise of JPEG compression is that some image degradations simply won't be visible in normal viewing. A visual comparison is the ultimate test. If you must have an automated comparison there are methods to do so, but I don't think any have emerged as the undisputed best way to compare.

Post a Comment for "Why Does Pil.image.rotate() Results In Loss Of Quality Despite `resample=image.bicubic`"