简体   繁体   中英

How to convert image to sepia in java

Im trying to take an image and make it sepia. For some reason the code isn't working even though I got it from the book I bought to teach myself java. Is there something Im missing or something thats in an incorrect spot? Every question Ive seen like this has some crazy formulas and symbols that I dont even know yet. Can someone help me?

import images.APImage;
import images.Pixel;

public class Sepia{

public static void main(String[] args){

    APImage image = new APImage("DogStockPhoto.jpg");
    //image.draw();
    //converts to grayscale
    for(Pixel p: image){
        int red = p.getRed();
        int green = p.getGreen();
        int blue = p.getBlue();
        int average = (red + green + blue)/3;
        p.setRed(average);
        p.setGreen(average);
        p.setBlue(average);

        //converts to sepia
        if(red < 63){
            red = (int)(red * 1.1);
            blue = (int)(blue * 0.9);
        }else if(red < 192){
            red = (int)(red * 1.15);
            blue = (int)(blue * 0.85);
        }else{
            red = Math.min((int)(red * 1.08), 255);
            blue = (int)(blue * 0.93);
        }
    }

    image.draw();       

}
}

Also, after I try to draw the Image, its still in grayscale.

The comment is correct. You average out the pixel values making them gray. Then you calculate the sepia value for the pixel, but never assign it back to the pixel. Therefore your image remains in grayscale.

After the if...else block:

p.setRed(red);
p.setBlue(blue);

OK! So thank you two for the feedback. I figured out a couple things. I should have been telling it to replace the gray with the new "red" and "blue". However, that doesn't work all in the same place, meaning you need two for statements, one for grayscale and the other for sepia. After figuring that out it all went well! Here's my finished code:

import images.APImage;
import images.Pixel;

public class Sepia{

public static void main(String[] args){

    APImage image = new APImage("DogStockPhoto.jpg");
    //image.draw();

    //converts to grayscale
    for(Pixel p: image){
        int red = p.getRed();
        int green = p.getGreen();
        int blue = p.getBlue();
        int average = (red + green + blue)/3;
        p.setRed(average);
        p.setGreen(average);
        p.setBlue(average);
    }

    //converts to sepia
    for(Pixel p: image){
        int red = p.getRed();
        int blue = p.getBlue();
        if(red < 63){
            red = (int)(red * 1.1);
            blue = (int)(blue * 0.9);
        }else if(red < 192){
            red = (int)(red * 1.15);
            blue = (int)(blue * 0.85);
        }else{
            red = Math.min((int)(red * 1.08), 255);
            blue = (int)(blue * 0.93);
        }
        p.setRed(red);
        p.setBlue(blue);
    }

    image.draw();       
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM