1

I need to resize alot of images from the ratio aspect (2:3) to (3:4).

The images are 800px x 1200px currently. I need them to be 600px x 800px eventually without any cropping.

May I know what libraries are available for me to do padding and resizing without cropping in Java?

  • See `Image.getScaledInstance(...)` – camickr Dec 09 '18 at 03:14
  • *"May I know what libraries are available for me to do padding and resizing without cropping in Java?"* I have voted to close this, but the short answer is AWT (classes in the `java.awt` & `java.awt.image` packages). – Andrew Thompson Dec 09 '18 at 06:13
  • Hi, thanks for your reply, i have tried your method. When resizing to a smaller image, the image is squeeze such that the person in the image becomes thinner. When resizing to a bigger image, the image is stretch such that the person in the image becomes fatter. – SomeoneInNeedOfHelp Dec 11 '18 at 09:15

3 Answers3

4

From your current Image (assuming a java.awt.Image) you can use :

And these steps:

  • compute the ratios in width and in height
  • depending on their values (padding width or padding height)
    • compute the width and height to obtain the scaled image
    • compute the padding required
  • write the image at the good position
static BufferedImage pad(BufferedImage image, double width, double height, Color pad) {
    double ratioW = image.getWidth() / width;
    double ratioH = image.getHeight() / height;
    double newWidth = width, newHeight = height;
    int fitW = 0, fitH = 0;
    BufferedImage resultImage;
    Image resize;

    //padding width
    if (ratioW < ratioH) {
        newWidth = image.getWidth() / ratioH;
        newHeight = image.getHeight() / ratioH;
        fitW = (int) ((width - newWidth) / 2.0);

    }//padding height
    else if (ratioH < ratioW) {
        newWidth = image.getWidth() / ratioW;
        newHeight = image.getHeight() / ratioW;
        fitH = (int) ((height - newHeight) / 2.0);
    }

    resize = image.getScaledInstance((int) newWidth, (int) newHeight, Image.SCALE_SMOOTH);
    resultImage = new BufferedImage((int) width, (int) height, image.getType());
    Graphics g = resultImage.getGraphics();
    g.setColor(pad);
    g.fillRect(0, 0, (int) width, (int) height);
    g.drawImage(resize, fitW, fitH, null);
    g.dispose();

    return resultImage;
}

To use as

BufferedImage image = ...;
BufferedImage result = pad(image, 600, 800, Color.white);
azro
  • 53,056
  • 7
  • 34
  • 70
  • Hi, thanks for your reply, i have tried your method. When resizing to a smaller image, the image is squeeze such that the person in the image becomes thinner. When resizing to a bigger image, the image is stretch such that the person in the image becomes fatter. – SomeoneInNeedOfHelp Dec 11 '18 at 09:13
  • @SomeoneInNeedOfHelp try my code with a scale factor, because you may use dimensions that are not proportionnal, If you don"t preserve proportionnality it won't look the same that's normal – azro Dec 11 '18 at 13:33
  • I need to resize alot of images from the ratio aspect (2:3) to (3:4). Your code for is resizing the image and keeping ratio aspect. I need to pad the image to change the ratio aspect. – SomeoneInNeedOfHelp Dec 13 '18 at 07:14
  • @SomeoneInNeedOfHelp Found out. Edit with a method that works for both kind of padding, respect proportion and required size ;) – azro Dec 13 '18 at 15:52
1

Managed to do it using below code: 'w' is the amount of padding you need on each side.

BufferedImage newImage = new BufferedImage(image.getWidth()+2*w, image.getHeight(), 
image.getType());

Graphics g = newImage.getGraphics();

g.setColor(Color.white);
g.fillRect(0,0,image.getWidth()+2*w,image.getHeight());
g.drawImage(image, w, 0, null);
g.dispose();
0

I think ffmpeg can help you to do anything with image. e.g. Use ffmpeg to resize image

  1. You can keep ffmpeg binaries in some conf folder.
  2. Create sh script for ffmpeg command.
  3. Use CommandLine from (Apache Commons exec library) to run the script.
dpatil
  • 9
  • 2