This site requires JavaScript, please enable it in your browser!
Greenfoot back
K_O_P
K_O_P wrote ...

2013/5/14

Scaling an Image

K_O_P K_O_P

2013/5/14

#
I try to scale an Image, because it's large and I need it in different, smaler sizes... The problem is, that when I try to scale the Image with the 'scale'-method in the GreenfootImage-class, the image becomes pixelated. I already scaled the image with GIMP, but it needs a lot of memory, to save all the image in different sizes! Is there another possibility in Java (Maybe an algorithm or anything else), to scale an image smaler without becoming pixelated?
danpost danpost

2013/5/14

#
You are probably scaling and re-scaling the image. Instead, keep the original image in a GreenfootImage field and when needed to scale:
// with an instance field as follows
private GreenfootImage baseImg = new GreenfootImage("filename.png");
// to scale, adjust x and y to new width and height, then
GreenfootImage image = new GreenfootImage(baseImg);
image.scale(x, y); // scaling the original image
setImage(image);
K_O_P K_O_P

2013/5/14

#
Thanks, that you try to help, but this is not the problem... I uploaded a scenario to simulate my problem: http://www.greenfoot.org/scenarios/8352 The big circles on the left represent the big image. The taler circles on the right top show, how the circles get scaled by GIMP and the circles on the right bottom corner show, how the method 'scale' in the GreenfootImage-class scales the image. And I need a method, that scales the 'big'-circles, that they look like the 'small' circles in the right top corner...
Entity1037 Entity1037

2013/5/14

#
A paint editing tool is more accurate at scaling an image than java. Sorry, but I'm pretty sure that's as good as it's going to get. :l
bourne bourne

2013/5/14

#
You can implement any scaling algorithm with java.
Entity1037 Entity1037

2013/5/15

#
Well... yeah I guess, but I'm not to sure with how it will work with Greenfoot.
bourne bourne

2013/5/15

#
Greenfoot is java.
Busch2207 Busch2207

2013/5/15

#
I already wrote such a code, to scale an image. Here it is:
    public static GreenfootImage scale(GreenfootImage gi_Image,int i_width,int i_height)
    {
        java.awt.Image image_=gi_Image.getAwtImage().getScaledInstance(i_width,i_height,java.awt.Image.SCALE_SMOOTH);

        gi_Image=new GreenfootImage(i_width,i_height);
        gi_Image.getAwtImage().createGraphics().drawImage(image_,0,0,null);
        return gi_Image;
    }
The class java.awt.Image has the method getScaledInstance(int,int,int), which returns a new java.awt.Image. The first two parameter of the method getScaledInstance are the sizes (like in the 'scale'-Method of the GreenfootImage-class). The third Number is the algorithm, the method has to use. (if you take look at the java.awt.Image-class, you can see final numbers, which you can call!) For your problem, it might be good, if you choose SCALE_SMOOTH (like in my example) or SCALE_AREA_AVERAGING. But the second algorithm takes a few milliseconds more! If you choose one of the other algorithms, you'll get the same solution like with the 'scale'-method of GreenfootImage. (I think, the class GreenfootImage is using SCALE_FAST) :)
K_O_P K_O_P

2013/5/15

#
Great! That was, what I was searching for! Thank you, Busch2207!
You need to login to post a reply.