0

I am generating a jpg image using phantomjs. I want image with 600x400px dimension and 144 dpi/ppi.

But I am only able to generate a image with 72dpi. So, I am trying to generate the image with 1200x800px dimensions in 72 dpi and then resizing it to 600x400px. so my idea is,

1200x800 px has dpi 72

so,

600x400px will have dpi 72*2 = 144   (because 1200/2=600px , 800/2=400px )

I am using java. But whenever I resize my image the quality becomes less.

Approaches I tried :

  1. I tried to manipulate the metadata information of image as explained in the post below, but still it gives image with poor quality.

    Increasing Resolution and Reducing Size of an Image in Java

    Write dpi metadata to a jpeg image in Java

  2. I tried using AffineTransform as given in following post, still not satisfied with results:

    Java image scaling improve quality?

Am I using a wrong approach? If yes, then please suggest some alternatives. If the approach is right then please guide me on how to implement it correctly?

UPDATE

I don't want to reduce the no. of pixels. I want to keep the no. of pixels same but reduce the height/width of image while increasing the dpi. example, In an image of 1200x800px having 72 dpi, I will reduce it to 600x400 px while increasing dpi to 144 . So , if you see the no of pixels are same and the image has higher dpi value.


Finally solved it using itext renderer. Refer to the following post on using itext for scaling the large image to increase resolution.


Image Quality using Itext

Community
  • 1
  • 1
ankit agarwal
  • 61
  • 1
  • 9
  • 4
    If you can control the pixel dimensions of an image, then the DPI are completely irrelevant. Whatever libraries you use should give you the ability to slap any DPI number into your image without altering it in any other way whatsoever. DPI is essentially nothing but a comment, and it is only used in the very rare case of wanting to print the image on paper without specifying the print size. In all other cases, DPI is just a nonsensical and useless piece of information. – Mike Nakis Feb 11 '15 at 14:00
  • So is there any other way by which I follow the same approach instead of using the dpi? Basically I am trying to convert a Image of 1200x800 px dimesion to 600x400 px image while maintaining the quality. It is a same situation, like when you take a big image and more you zoom out, more the picture gets clear. – ankit agarwal Feb 11 '15 at 14:08
  • 1
    You cannot convert an image of 1200x800px to 600x400 px while maintaining the quality, because ***by definition*** this type of conversion throws away three quarters of the information contained in the image. – Mike Nakis Feb 11 '15 at 14:12
  • FYI: Reducing the number of pixels in an image is called "decimation" http://en.wikipedia.org/wiki/Decimation_%28signal_processing%29 , and there are various ways to do it, each having its own peculiar tradeoffs. But, what Mike Nakis said about DPI is spot-on: If the library will generate 600x400@72 DPI, then all you have to do is take that and change the DPI comment. – Solomon Slow Feb 11 '15 at 14:52
  • I don't want to reduce the no. of pixels. I want to keep the no. of pixels same but reduce the height/width while increasing the dpi. So, In a image of 1200x800 px having 72 dpi, I will reduce it to 600x400 px while increasing dpi to 144 . So , if you see the no of pixels are same and the image has higher dpi value. – ankit agarwal Feb 12 '15 at 07:26
  • If your problem is solved, please post the solution as an answer and mark it as accepted. Thanks. – Tanmay Patil Feb 12 '15 at 13:08

1 Answers1

1

I solved it using Itext.

File file = new File("demo.png");
Image img = Image.getInstance(file.getName());
img.scaleAbsolute(width, height); //where width,height is the reduced width,height; in my case 600,400.

Read it through Image class and use scaleAbsolute(). For more details please read this post: Image Quality using Itext

PS: This is only helpful if you are trying to put image in pdf. If anyone knows how to convert img to a png file then please comment.

Community
  • 1
  • 1
ankit agarwal
  • 61
  • 1
  • 9