Batch Gratscale Image Code --Java Programming: Solving Problems with Software


import edu.duke.*;
import java.io.File;
public class GrayScaleConverter {

    //started with the image i wanted(inImage)

    public ImageResource makeGray(ImageResource inImage){

    //I Made a blank image of the same size

        ImageResource outImage = new ImageResource(inImage.getWidth(),inImage.getHeight());

    //for each pixelin outImage

        for(Pixel pixel : outImage.pixels()){

    //look at the corresponding pixel in inImage

            Pixel inPixel = inImage.getPixel(pixel.getX(),pixel.getY());

    //compute inPixel's red + inPixel's blue + inpixel's green

    //divide that sum by 3(call it average)

    int average = (inPixel.getRed()+inPixel.getGreen()+inPixel.getBlue())/3;

    //set pixel's red to average

    pixel.setRed(average);

    //set pixel's green to average

    pixel.setGreen(average);

    //set pixel's blur to average

    pixel.setBlue(average);

    }

     return outImage;

    }  

    public void SelectAndConvert(){

        DirectoryResource dr = new DirectoryResource();

        for (File f : dr.selectedFiles()){
        ImageResource inImage = new ImageResource(f);
        ImageResource gray = makeGray(inImage);
        gray.draw();
        }

    } 

}

Comments

Popular posts from this blog

Choose Your Social Media Channels Week 5 Quiz