1

I have an image and want to scale it down using imgscalr [1].
The source image's size is 1836 x 3264 (so it's portrait mode) and destination resolution is 1336 x 768.
The failure is that the image is in landscape mode and not portrait mode anymore after scaling. The scaling itself works like a charm.
My code:

BufferedImage src = ImageIO.read(new File("sourceimage.jpg"));
BufferedImage scaled = Scalr.resize(src, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
ImageIO.write(scaled, "jpg", f);

The metadata of the file looks correct (orientation = 1).
My expactation is when downscaling an portrait picture that it is still a portrait picture after the scaling process.

I also tried some other libraries/classes like this: https://frickelblog.wordpress.com/2009/06/08/fast-image-scaling-in-java/ but the effect is the same.

Can you please help me? This one drives me crazy! I'm pretty sure there is just a detail to change.

[1] http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/#maven

edit:
Here's the original image: https://i.stack.imgur.com/NEc8j.jpg
the scaled version: https://i.stack.imgur.com/SYx6p.jpg

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Allipon
  • 57
  • 3
  • 13
  • Do you have a sample of two images (a original and a scaled version)? – MadProgrammer May 30 '15 at 09:18
  • I tested an image from 3300x4200 down to 603x768 which seems to have worked okay – MadProgrammer May 30 '15 at 09:35
  • 1
    I added the pictures in the first post – Allipon May 30 '15 at 09:53
  • I tried you source image and it seems to work okay for me, produces an image of 432x768, which (at least when displayed in MaxOS's preview) comes up as portrait (and photoshop) (using imgscalr 4.2) – MadProgrammer May 30 '15 at 10:22
  • 1
    ok, with the image here it works. The original image is 1.7 Mb big (here just ~320 Kb). You can [find it here](http://cloud.a3k2l.de/public.php?service=files&t=2d47d7a941ed58d8e63db8cf3bddc3d7&download). Can you try again with this one please? – Allipon May 30 '15 at 13:25
  • The image is been read in as `3264x1836` by `ImageIO`... – MadProgrammer May 30 '15 at 23:14
  • ImageIO doesn't seem to read the orientation of the incoming image, see http://stackoverflow.com/questions/15978809/imageio-read-always-rotates-my-uploaded-picture for some more details – MadProgrammer May 30 '15 at 23:28

2 Answers2

4

So the problem isn't with ImgScar but with ImageIO, ImageIO won't read the orientation of the image, so it was been read in as 3264x1836 - Landscape.

So what I ended up doing was rotating the image 90 degrees...

        BufferedImage src = ImageIO.read(new File("/Users/swhitehead/Downloads/original.jpg"));

        System.out.println(src.getWidth() + "x" + src.getHeight());
        BufferedImage rotated = Scalr.rotate(src, Scalr.Rotation.CW_90, Scalr.OP_ANTIALIAS);
        BufferedImage scaled = Scalr.resize(rotated, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
        System.out.println(scaled.getWidth() + "x" + scaled.getHeight());
        ImageIO.write(scaled, "jpg", new File("Scaled.jpg"));

Which seems to have fixed "this" problem.

You can have a look at ImageIO.read( ) always rotates my uploaded picture for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Browsers implement <img> differently.
Firefox will not read the EXIF-information so portrait pictures are displayed in landscape.
Chrome does read out the EXIF-information so portait stays portrait.

I fixed it with this CSS declaration:
image-orientation: from-image;

progonkpa
  • 3,590
  • 9
  • 31
  • 50