Skip to main content

Changing the colors on a image to red where the pixel is black

2 replies [Last post]
cingram007
Offline
Joined: 2006-01-10

Hi, does anyone know how to convert an image to a different color, based on the color of the pixel it encounters.

What I would like the code to do is change the pixel color to red, if the pixel is black or green.

Currently my code works like this.

//Select image from the db
rs = cs.executeQuery();

if(rs.next()){
//Get the image as a byte array from the database
byte[] bytes = rs.getBytes(1);

//use getReaderFormatNames() to ensure ImageIO will recognize the image type String [] strNames = ImageIO.getReaderFormatNames();

//read the image into a ByteArrayInputStream
ByteArrayInputStream byteArrayInputStream = new
ByteArrayInputStream(bytes);

//read the byteArrayInputStream, in tiff format, into a awt
BufferedImage BufferedImage bufi = ImageIO.read(byteArrayInputStream);

//right here I'd like to do the color conversion
BufferedImage bi = new BufferedImage(bufi.getWidth(), bufi.getHeight(), BufferedImage.TYPE_INT_RGB);

int red = 0;
int green = 0;
int blue = 0;

//Loop through the image and set the color to red
for(int x = 0; x < bufi.getWidth(); x++){
for(int y = 0; y < bufi.getHeight(); y++){
int pixel = bufi.getRGB(x, y);
if(pixel > 0){
red = 0x00ff0000;
bi.setRGB(x,y,0xff00ffff);
}
else{
pixel = 0;
}
}
}

//test the buffer is not null
if (bi != null){
//convert to png format ??????
ImageIO.write(bi, "png", outputfile);
}
}

Currently the image is not being recoloured.

Thanks in advance,
Chris

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ken Warner

Don't know why it's not changing color but here's a coding suggestion --

int red = 0x00ff0000;
> int green = 0x0000ff00;
> int blue = 0x000000ff;
>
> //Loop through the image and set the color to red
> for(int x = 0; x < bufi.getWidth(); x++){
> for(int y = 0; y < bufi.getHeight(); y++){
> int pixel = bufi.getRGB(x, y);
> if(pixel > 0){
> // red = 0x00ff0000; you are setting red but not using it and you don't have to
> // set it each time though the loop
> //bi.setRGB(x,y,0xff00ffff); and here you are setting the red value to 0
> bi.setRGB(x,y,red);
> }
> // pixel must be 0 here because the conditional above tests for non-zero
> //else{
> //pixel = 0;
> }
> }
> }

jai-imageio@javadesktop.org wrote:
> Hi, does anyone know how to convert an image to a different color, based on the color of the pixel it encounters.
>
> What I would like the code to do is change the pixel color to red, if the pixel is black or green.
>
> Currently my code works like this.
>
> //Select image from the db
> rs = cs.executeQuery();
>
> if(rs.next()){
> //Get the image as a byte array from the database
> byte[] bytes = rs.getBytes(1);
>
> //use getReaderFormatNames() to ensure ImageIO will recognize the image type String [] strNames = ImageIO.getReaderFormatNames();
>
> //read the image into a ByteArrayInputStream
> ByteArrayInputStream byteArrayInputStream = new
> ByteArrayInputStream(bytes);
>
> //read the byteArrayInputStream, in tiff format, into a awt
> BufferedImage BufferedImage bufi = ImageIO.read(byteArrayInputStream);
>
> //right here I'd like to do the color conversion
> BufferedImage bi = new BufferedImage(bufi.getWidth(), bufi.getHeight(), BufferedImage.TYPE_INT_RGB);
>
> int red = 0;
> int green = 0;
> int blue = 0;
>
> //Loop through the image and set the color to red
> for(int x = 0; x < bufi.getWidth(); x++){
> for(int y = 0; y < bufi.getHeight(); y++){
> int pixel = bufi.getRGB(x, y);
> if(pixel > 0){
> red = 0x00ff0000;
> bi.setRGB(x,y,0xff00ffff);
> }
> else{
> pixel = 0;
> }
> }
> }
>
> //test the buffer is not null
> if (bi != null){
> //convert to png format ??????
> ImageIO.write(bi, "png", outputfile);
> }
> }
>
> Currently the image is not being recoloured.
>
> Thanks in advance,
> Chris
> [Message sent by forum member 'cingram007' (cingram007)]
>
> http://forums.java.net/jive/thread.jspa?messageID=100712
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

cravi24
Offline
Joined: 2012-03-06

I am using the same code but I am unable to recolor my image.

for(x=0;x<Image.getHeight();x++){

for(y=0;y<Image.getWidth();y++){

image.setRGB(x,y, 0x00ff0000);

int clr= image.getRGB(x,y);

}

}

Value of color still comes ot to be -32986 and there is no change in image colors as well. I am unable to figure out why is it so?? I have removed all permissions from the image and tried with jpg,bmp,png and gif formats as well but no success. What am I doing wrong here??