0

I am working on a web application that:

  1. Uploads a JPEG image
  2. extracts a zone from it (using coordinates)
  3. and resizes the zone extracted, without loosing image quality .

I used this code to do this:

//method to extract an image 
def extractedImage=exractImageRect( imageFile ,  x , y , h , w)

ImageIO.write(extractedImage, "PNG", new File("C:\\Users\\Ma\\Documents\\out.png"));
int type = extractedImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : extractedImage.getType();

//resize the image 
resizedImage =resizeImage(extractedImage,  type ,w*2 , h*2)

But using this code, the resized image has a very low quality. I also used RenderingHints: same problem.

So my question is: What is the best way to keep the image quality, while using the height and width of the original image, which has 300dpi and 2479 width , 3508 height?

marcAntoine
  • 668
  • 5
  • 19
  • 35
  • It's impossible to create real data out of thin air. Whichever method you use, resized image **will** lose quality. – Cengiz Can May 13 '14 at 07:58
  • can't i set the dpi of the resized image equal to the one of the original image manipulating height and width of the resized image ? – marcAntoine May 13 '14 at 08:01

2 Answers2

2

and resizes the zone extracted, without loosing image quality .

I think this part is impossible. Let's assume that you have a picture that's just a 10px X 10px blakc rectangle. Now you double the dimensions there will be a 20px X 20px rectangle. Originally you had 100 black pixels but now you have 400 pixels of which 100 are black and 300 are unknown. You need to figure out by some algorithm that what your pixels colors are.

In this case it's easier because it's black, but if instead of a rectangle you'd had a picture it would be impossible to figure out exactly what color pixels belong to the unknown pixels, so you must approximate but approximating is not perfect it most cases, so there will be quality loss

maczikasz
  • 1,113
  • 5
  • 12
  • i am always working on black and white pictures , also the resizeImage(extractedImage, type ,w*2 , h*2) , i wanted to calculate a coefficient instead of "2" – marcAntoine May 13 '14 at 08:04
  • Still it's not that easy, are your picture truly black and white or are they grayscale? If grayscale that's nearly as bad as color, because your algorithm have to guess the shade of grey to fill the empty pixels with. If it's black and white imagine having a sine curve in white on a black background, if you enlarge your image by a coefficient (whatever it may be) your algorithm have to perfectly recreate the sine curve without actually knowing that it's a sine curve and that's very hard to do – maczikasz May 13 '14 at 08:32
0

I have tested different algorithms to resize an image (see here), the one giving the best image quality is AVERAGE, but it's very slow. PROGRESSIVE_BILINEAR is a good compromise between processing time and image quality.

Community
  • 1
  • 1
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97