-1

I am trying to downscale an pixelart image (of the game stardew valley) consisting of 4x4 pixels per block of the same color to the same image with 1x1 pixel per block.

Photoshop is doing a great job when I just resize it with the NEAREST_NEIGHBOUR interpolation.

But when I use the technique from: How to scale a BufferedImage but with TYPE_NEAREST_NEIGHBOR instead it gets all destorted.

What is going wrong and how should I go about fixing it?

            BufferedImage old = getScreenShot();
            int w = old.getWidth();
            int h = old.getHeight();

            int newWidth = w/4;
            int newHeight = h/4;


            BufferedImage resized = new BufferedImage(newWidth, newHeight, old.getType());
            AffineTransform at = new AffineTransform();
            at.scale(0.25, 0.25);
            AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            resized = scaleOp.filter(old, resized);

            //TODO (remove)  for debugging the screencapture capability
            File outputfile = new File("C:/Users/Kevin/Desktop/imagetestmap/test.jpg");
            try {
                ImageIO.write(resized, "jpg", outputfile);
            } catch (IOException e) {
                e.printStackTrace();
            }


            return resized;

actual screenshot:

actual screenshot

what photoshop sees:

what photoshop sees

what my program sees:

what my program sees

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120

1 Answers1

0

Cris Luengo found the solution, I should have used .png instead of jpg.

This is the working code:

            BufferedImage old = getScreenShot();
            int w = old.getWidth();
            int h = old.getHeight();

            int newWidth = w/4;
            int newHeight = h/4;


            BufferedImage resized = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);

             AffineTransform at = new AffineTransform();
            at.scale(0.25, 0.25);
            AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            resized = scaleOp.filter(old, resized);

            //TODO (remove)  for debugging the screencapture capability
            File outputfile = new File("C:/Users/Kevin/Desktop/imagetestmap/test.png");
            try {
                ImageIO.write(resized, "png", outputfile);
            } catch (IOException e) {
                e.printStackTrace();
            }