thumbnailator icon indicating copy to clipboard operation
thumbnailator copied to clipboard

Need API to rotate image without losing /compressing

Open jforjava1981 opened this issue 9 years ago • 7 comments

Hi

I have been using Thumbnailtor to create thumbnails and it works great. However I have a problem to solve. I store images captured from iphone that come with exif data. If I store them as it is without using Thumbnailtor I retain original size but images are shown rotated 90 degree on windows and chrome. I know the issue is because chrome and other windows photo viewers do not honour EXIF data.

  So What I am trying to do is store the image using thumbnailtor with following code :

          Thumbnails.of(bais).rendering(Rendering.QUALITY).scale(1.0).outputQuality(1.0).outputFormat(PricerConstants.PRICER_FILE_TYPE_JPG).toFile(itemImageFile);

This solves the rotation problem but the image generated has much lesser size. Is it losing the quality even if I am telling it to store it using scale 1,0 which i assume means do not actually reduce it and ouputquality also 1.0. So why it reduces the size. Is it because it strips of the exif while storing?

I would like to know if above is correct way if I just want to correct /honour the rotation without applying any other features. So basically i want original image only and not the thumbnail.

I believe the size reduction might be because of stripping of EXIF but for 2.5 MB image size after above code gets reduced to 367 KB. I believe it is losing actual image data as well either because of rotation or other.

Please help me to sort this out.

jforjava1981 avatar Sep 20 '16 21:09 jforjava1981

Same issue here

vrto avatar Oct 04 '16 16:10 vrto

Any updates on this?. @coobird is it possible to consider this?

jforjava1981 avatar Feb 14 '17 10:02 jforjava1981

Exactly what I'm looking for as well. Rotate it by EXIF data, but don't compress it. How to do this?

ILyaCyclone avatar Feb 23 '17 00:02 ILyaCyclone

It's not a Thumbnailator-based answer, but may be helpful. For this task, I've separately used https://github.com/drewnoakes/metadata-extractor to read the ExifIFD0Directory.TAG_ORIENTATION and rotate it myself.

https://github.com/drewnoakes/metadata-extractor/blob/5b07a49f7b3d90c43a36a79dc4f6474845e1ebc7/Tests/com/drew/metadata/exif/ExifReaderTest.java

codegoddesskh avatar Feb 23 '17 15:02 codegoddesskh

@coobird Any updates on this?

jforjava1981 avatar Apr 13 '17 10:04 jforjava1981

I've actually looked into this before, but is was way too involved so I never got around to responding.

Is it losing the quality even if I am telling it to store it using scale 1,0 which i assume means do not actually reduce it and ouputquality also 1.0. So why it reduces the size. Is it because it strips of the exif while storing?

It's true that Thumbnailator does not preserve the Exif information in the resulting image, so some of the file size reduction comes from that. However, the main reduction in size is because it is indeed losing quality due to the JPEG compression being performed on the image.

Keep in mind that Thumbnailator is a library that's basically designed to create thumbnails, so performing lossless rotations of JPEGs is not its primary goal. It's definitely a nice-to-have feature, but getting it right is going to involve a lot of effort.

For example, there are some caveats that does not allow lossless rotation of all JPEGs. In particular, the image dimensions become an issue, as discussed here and here.

is it possible to consider this?

I would like to consider doing this, but frankly, it will end up being low on the priority scale, as it only applies to a subset of functionality that is being served by Thumbnailator, but will require quite a bit of effort to do it right.

Please keep in mind that Thumbnailator is just a wrapper that uses JPEG reader/writer plugins that is registered with the Java Image I/O SPI. Doing lossless JPEG rotations would be a "paradigm shift" for Thumbnailator, as it will have to actually read JPEG data and manipulate it directly.

coobird avatar May 10 '17 15:05 coobird

@coobird thanks and look forward into future when we have this.

jforjava1981 avatar May 11 '17 09:05 jforjava1981