javaimageif-statementfor-loopgetpixel

Java Change Color Palette of an Image


Hello fellow programmmers!

I am a Java beginner and I have a Java program that takes an image, displays the unedited version, converts it to grayscale and shows the image, and applies a different color palette to the image and then shows it. I am having trouble applying the color palette to the image though, and it takes and changes the whole image to the color. How can I prevent it from changing the whole images color and just the color of the area I specify? It should turn out something like this:

Results should be similar to this

Please include notes with any code so I can understand what is happening in the program.

And here's the code. Thanks for helping me out!

import java.awt.*;

public class GrayscaleToColor
{
public static void main(String[] args)
{        

    Picture picture = new Picture("WashingtonMonument.jpg");
    picture.show();

    Picture picture2 = new Picture("WashingtonMonument.jpg");
    picture2.show();

    int redValue = 0; int greenValue = 0; int blueValue = 0;

    Pixel targetPixel = new Pixel(picture2, 0,0);
    Color pixelColor = null;

    for(int y=0; y < picture2.getHeight(); y++)
    {
        for(int x = 0; x < picture2.getWidth(); x++)
        {
            targetPixel = picture2.getPixel(x,y);
            pixelColor = targetPixel.getColor();

            redValue = pixelColor.getRed(); 
            greenValue = pixelColor.getGreen();
            blueValue = pixelColor.getBlue();

            redValue = greenValue = blueValue = (redValue + greenValue + blueValue) / 3;

            pixelColor = new Color(redValue, greenValue, blueValue);
            targetPixel.setColor(pixelColor);
        }
    }

    picture2.write("GrayWashingtonMonument.jpg");
    picture2.show();

    Picture picture3 = new Picture("WashingtonMonument.jpg");

    for(int y=0; y < picture3.getHeight(); y++)
    {
        for(int x = 0; x < picture3.getWidth(); x++)
        {
            targetPixel = picture3.getPixel(x,y);
            pixelColor = targetPixel.getColor();

            // It sets the colors, but it sets the color of the whole picture, not
            // the areas I want it to set in the picture.

            if(((redValue > 150) && greenValue > 150) && blueValue > 150)
            {
                blueValue = 130;
                greenValue = 17;
                redValue = 50;
            }
            else if(((redValue < 75) && greenValue < 75) && blueValue < 75)
            {
                redValue = 240;
                blueValue = 43;
                greenValue = 100;
            }
            else if(((redValue < 25) && greenValue < 25) && blueValue < 25)
            {
                redValue = 140;
                greenValue = 250;
                blueValue = 45;
            }

            pixelColor = new Color(redValue, greenValue, blueValue);
            targetPixel.setColor(pixelColor);
        }
    }

    picture3.write("ColorizedWashingtonMonument.jpg");
    picture3.show();

}
}

Solution

  • Just tried to compile your code -- received many errors about references to things that don't exist in awt package, such as: - Picture - Pixel

    Are you trying to get folks here to do your homework for you?

    Anyway, you need to review javax.imageio -- for parsing the image http://docs.oracle.com/javase/8/docs/api/javax/imageio/package-summary.html