1

I have a problem with quality when I try only to RESIZE an image to a specific width and height using imgsclr 4.2(http://www.thebuzzmedia.com/imgscalr-3-2-released/comment-page-1/#comment-650387).

I have an image which 240X320 and I have to resize it to 50X75 and 120X180 . I tried both separately using the following code(I am using imgscalr 4.2).

image=resize(image,Method.ULTRA_QUALITY, 50,75, OP_ANTIALIAS, OP_BRIGHTER);
saveImage(image, ImageFormatTypes.JPEG, DESTINATION + RESIZED_IMAGE + “.”+ImageFormatTypes.JPEG);

I have two problems. #1 is very low quality compared to other internal tools I used to resize the image manually. #2. The width increases on the output from 50 to 56 and from 120 to 135 which is weird.

Do you have any idea why these are happening? I appreciate your help. Original Image Resized Image

WowBow
  • 7,137
  • 17
  • 65
  • 103
  • First of all, the ratio of 240x320 (0.75) is different from your target sizes (0.67), so imgsclr may be trying to maintain the original images ratio. Can you also provide the actual calls to imgsclr, the original and scaled images – MadProgrammer Feb 23 '15 at 20:49
  • @MadProgrammer Thank you for your time. resize is the only call to imgsclr. I have "BufferedImage image = getImageFromLink(link);" before calling imgsclr that gets the image from a URL. The URL doesn't have any extension like .jpg but I know for sure its an image. Is that helpful? – WowBow Feb 23 '15 at 21:05
  • @MadProgrammer Scalr.resize(image,Method.ULTRA_QUALITY, 50,75, OP_ANTIALIAS, OP_BRIGHTER); is what called but since its static and my method was static too I didn't have to use the class name Scalar its imported statically like import static org.imgscalr.Scalr.*; – WowBow Feb 23 '15 at 21:08
  • So, do you have a comparison of the original image to the scaled image? – MadProgrammer Feb 23 '15 at 23:41
  • @MadProgrammer I am not sure what you mean by that. However, you can see the above two images. Original and resized(smaller). Thanks – WowBow Apr 16 '15 at 18:06

1 Answers1

3

I don't think the core issue is with imgsclr, but with the choice of jpg. Remember, jpg uses a loss-based algorithm, dumping parts of the image to reduce it's size.

For comparison, the two images below were produced using the same methods, but the left was output using jpg and the right using png (using the ImageIO API)

Compare

So the top image is the same master image.

The second row is imgsclr using ULTRA_QUALITY, AUTOMATIC, BALANCED and QUALITY methods. The last image on the second row is using SPEED

The last row is a serious of "other" scaling methods. The first two use a divide and conquer approach, dividing the image by 2 until it reaches it's desired size, demonstrated here. The first is a "to fill" and the other is "to fit" (one will overfill the available space, one will fit within). The third image is using Image#getScaledInstance and I seem to have introduced a alpha channel somewhere, hence the reason the jpg version is red, but I was comparing quality ;)

The reason for the size increase may come down to imgsclr attempting to maintain the aspect ratio of the image

And finally...

enter image description here

This compares BufferedImageOp, using OP_BRIGHTER, OP_ANTIALIAS and OP_BRIGHTER, OP_ANTIALIAS, none from right to left...

These are output as PNGs

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for your input. I'll give them all a try. Btw, I have tried to save the images individually but it seems like they are all together as an individual image. – WowBow Feb 24 '15 at 23:27
  • Yes, the images I've provided are all merged into a single image, makes posting easier – MadProgrammer Feb 24 '15 at 23:29
  • Btw, what functions should I use in order to output the desired image size without caring about the aspect ration? The requirement I have needs to have the exact image to be output. The aspect ratio is the less important thing here. Thanks – WowBow Apr 20 '15 at 17:58
  • @WowBow If you have a look at the section [Image Proportions](http://www.thebuzzmedia.com/downloads/software/imgscalr/javadoc/org/imgscalr/Scalr.html) from the JavaDocs, it should be able to give you some ideas – MadProgrammer Apr 20 '15 at 21:45
  • Thank you. I found it in imgsclr and it is Mode.FIT_EXACT – WowBow Apr 20 '15 at 22:11
  • One more question. We decided to not FIT_EXACT because the image would become totally different. I am thinking of cropping images to make the final size to be equal to the output needed. Is that a better workaround? – WowBow Apr 20 '15 at 23:28
  • @WowBow Personally, I use a "scale to fit" approach (don't know if imgsclar does this out of the box), but the idea is to scale the image down so that the largest edge fits within the bounds of the area you want to fit it in, this does mean that there might be empty space around one edge, but the results are generally more pleasing, but this will depend on you own needs – MadProgrammer Apr 20 '15 at 23:35
  • I used Mode.FIT_EXACT but the result was pretty bad. I am not sure if they have a better alternative. – WowBow Apr 20 '15 at 23:46
  • `FIT_TO_HEIGHT` or `FIT_TO_WIDTH`? – MadProgrammer Apr 20 '15 at 23:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75763/discussion-between-wowbow-and-madprogrammer). – WowBow Apr 20 '15 at 23:47