Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Convert jpg image to black and white?

Reply
Thread Tools

Convert jpg image to black and white?

 
 
Uli Kunkel
Guest
Posts: n/a
 
      02-16-2009

How can I convert an image to black and white?
Do I have to manually calculate each pixel or is there some class that
has this functionality?

Thanks for any suggestions.
 
Reply With Quote
 
 
 
 
blue indigo
Guest
Posts: n/a
 
      02-16-2009
On Mon, 5648 Sep 1993 11:12:14 +0100, Uli Kunkel wrote:

>
> How can I convert an image to black and white?
> Do I have to manually calculate each pixel or is there some class that
> has this functionality?
>
> Thanks for any suggestions.


java.awt.image.ColorConvertOp with java.awt.color.ColorSpace.CS_GRAY ought
to do it.

--
blue indigo
UA Telecom since 1987
 
Reply With Quote
 
 
 
 
Uli Kunkel
Guest
Posts: n/a
 
      02-16-2009
blue indigo wrote:
> On Mon, 5648 Sep 1993 11:12:14 +0100, Uli Kunkel wrote:
>
>> How can I convert an image to black and white?
>> Do I have to manually calculate each pixel or is there some class that
>> has this functionality?
>>
>> Thanks for any suggestions.

>
> java.awt.image.ColorConvertOp with java.awt.color.ColorSpace.CS_GRAY ought
> to do it.
>


Thank you but I need only black and white, and this is gray scale.
Although I will first use this to convert it to gray scale first and
after that I will try to implement one of the existing algorithms.

Does anyone know of any good package that does the black&white conversion?
 
Reply With Quote
 
Casey Hawthorne
Guest
Posts: n/a
 
      02-16-2009
After converting to gray scale, you could set a threshold and all
pixels above that are white.


On Mon, 16 Feb 2009 17:48:36 +0100, Uli Kunkel <(E-Mail Removed)>
wrote:

>blue indigo wrote:
>> On Mon, 5648 Sep 1993 11:12:14 +0100, Uli Kunkel wrote:
>>
>>> How can I convert an image to black and white?
>>> Do I have to manually calculate each pixel or is there some class that
>>> has this functionality?
>>>
>>> Thanks for any suggestions.

>>
>> java.awt.image.ColorConvertOp with java.awt.color.ColorSpace.CS_GRAY ought
>> to do it.
>>

>
>Thank you but I need only black and white, and this is gray scale.
>Although I will first use this to convert it to gray scale first and
>after that I will try to implement one of the existing algorithms.
>
>Does anyone know of any good package that does the black&white conversion?

--
Regards,
Casey
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      02-16-2009
In article <(E-Mail Removed)>,
Casey Hawthorne <(E-Mail Removed)> wrote:

> On Mon, 16 Feb 2009 17:48:36 +0100, Uli Kunkel <(E-Mail Removed)>
> wrote:
>
> >blue indigo wrote:
> >> On Mon, 5648 Sep 1993 11:12:14 +0100, Uli Kunkel wrote:
> >>
> >>> How can I convert an image to black and white? Do I have to
> >>> manually calculate each pixel or is there some class that has
> >>> this functionality?
> >>>
> >>> Thanks for any suggestions.
> >>
> >> java.awt.image.ColorConvertOp with
> >> java.awt.color.ColorSpace.CS_GRAY ought to do it.
> >>

> >
> >Thank you but I need only black and white, and this is gray scale.
> >Although I will first use this to convert it to gray scale first and
> >after that I will try to implement one of the existing algorithms.
> >
> >Does anyone know of any good package that does the black&white
> >conversion?

>
> After converting to gray scale, you could set a threshold and all
> pixels above that are white.


Imagej shows the way:

<http://rsbweb.nih.gov/ij/>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
blue indigo
Guest
Posts: n/a
 
      02-17-2009
On Mon, 5648 Sep 1993 17:48:36 +0100, Uli Kunkel wrote:

> blue indigo wrote:
>> On Mon, 5648 Sep 1993 11:12:14 +0100, Uli Kunkel wrote:
>>
>>> How can I convert an image to black and white?

>>
>> java.awt.image.ColorConvertOp with java.awt.color.ColorSpace.CS_GRAY ought
>> to do it.
>>

>
> Thank you but I need only black and white, and this is gray scale.


I'm sorry, mate -- often when people say "black and white", greyscale is
actually what they have in mind.

Unfortunately, I don't see a true-monochrome java.awt.color.ColorSpace in
the standard library. You could convert to greyscale and threshold it.
That's a fairly simple iteration over the image pixels.

--
blue indigo
UA Telecom since 1987
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      02-17-2009
In article
<(E-Mail Removed)>,
Katja Christiansen <(E-Mail Removed)> wrote:

> On Feb 16, 11:12*am, Uli Kunkel <(E-Mail Removed)> wrote:
> > How can I convert an image to black and white?
> > Do I have to manually calculate each pixel or is there some class that
> > has this functionality?
> >
> > Thanks for any suggestions.

>
> You could also write your image to a BufferedImage of a given type:
>
> BufferedImage blackAndWhiteImage =
> new BufferedImage(originalImage.getWidth(null),
> originalImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
> Graphics2D g = (Graphics2D) blackAndWhiteImage.getGraphics();
> g.drawImage(originalImage, 0, 0, null);
> g.dispose();


Excellent!

<http://sites.google.com/site/trashgod/threshold>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
Uli Kunkel
Guest
Posts: n/a
 
      02-18-2009
Katja Christiansen wrote:
> On Feb 16, 11:12 am, Uli Kunkel <(E-Mail Removed)> wrote:
>> How can I convert an image to black and white?
>> Do I have to manually calculate each pixel or is there some class that
>> has this functionality?
>>
>> Thanks for any suggestions.

>
> You could also write your image to a BufferedImage of a given type:
>
> BufferedImage blackAndWhiteImage =
> new BufferedImage(originalImage.getWidth(null),
> originalImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
> Graphics2D g = (Graphics2D) blackAndWhiteImage.getGraphics();
> g.drawImage(originalImage, 0, 0, null);
> g.dispose();
>
> Katja



Thank you very much.
I tried this and it works, but I need better quality so I'll try some
dithering algorithm like Floyd-Steinberg.
I'm not yet sure how to implement it with java..
 
Reply With Quote
 
Uli Kunkel
Guest
Posts: n/a
 
      02-18-2009
Uli Kunkel wrote:
>
> How can I convert an image to black and white?
> Do I have to manually calculate each pixel or is there some class that
> has this functionality?
>
> Thanks for any suggestions.


I have copied somebody's method and modified it.
It uses a dithering algorithm so it looks good.

The problem now is that I want to save a bmp image with a specified dpi
setting in the header.
I found that this can be done for jpeg with JPEGEncodeParam but nothing
for bmp.
So does anybody know hot to change the dpi when writing a bmp image?




Belove is the code for bw dithering:
------------------------------------------------------------------------------
public static BufferedImage processImage(BufferedImage inputImage) {

// Create a binary image for the results of processing

int w = inputImage.getWidth();
int h = inputImage.getHeight();
BufferedImage outputImage = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_BINARY);

// Work on a copy of input image because it is modified by diffusion

WritableRaster input = inputImage.copyData(null);
WritableRaster output = outputImage.getRaster();

final int threshold = 128;
float value, error;

for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {

value = input.getSample(x, y, 0);

// Threshold value and compute error

if (value < threshold) {
output.setSample(x, y, 0, 0);
error = value;
}
else {
output.setSample(x, y, 0, 1);
error = value - 255;
}

// Spread error amongst neighbouring pixels

if((x > 0) && (y > 0) && (x < (w-1)) && (y < (h-1)))
{
value = input.getSample(x+1, y, 0);
input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));
value = input.getSample(x-1, y+1, 0);
input.setSample(x-1, y+1, 0, clamp(value + 0.1875f * error));
value = input.getSample(x, y+1, 0);
input.setSample(x, y+1, 0, clamp(value + 0.3125f * error));
value = input.getSample(x+1, y+1, 0);
input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error));
}

}
return outputImage;

}

// Forces a value to a 0-255 integer range

public static int clamp(float value) {
return Math.min(Math.max(Math.round(value), 0), 255);
}
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      02-18-2009
In article <gngmde$uia$(E-Mail Removed)>,
Uli Kunkel <(E-Mail Removed)> wrote:

> Katja Christiansen wrote:
> > On Feb 16, 11:12 am, Uli Kunkel <(E-Mail Removed)> wrote:
> >> How can I convert an image to black and white? Do I have to
> >> manually calculate each pixel or is there some class that has this
> >> functionality?
> >>
> >> Thanks for any suggestions.

> >
> > You could also write your image to a BufferedImage of a given type:
> >
> > BufferedImage blackAndWhiteImage =
> > new BufferedImage(originalImage.getWidth(null),
> > originalImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
> > Graphics2D g = (Graphics2D) blackAndWhiteImage.getGraphics();
> > g.drawImage(originalImage, 0, 0, null);
> > g.dispose();

[...]
> Thank you very much.
> I tried this and it works, but I need better quality so I'll try some
> dithering algorithm like Floyd-Steinberg. I'm not yet sure how to
> implement it with java..


Alternatively, you might experiment with these interpolation types and
the related rendering hints:

<http://java.sun.com/javase/6/docs/api/java/awt/image/AffineTransformOp.html>
<http://java.sun.com/javase/6/docs/api/java/awt/RenderingHints.html>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert progressive jpg to non-progressive jpg Sniper Abandon Ruby 0 05-15-2009 10:42 AM
Use imageio to convert a .tif image to a .jpg image brightoceanlight@hotmail.com Java 3 11-18-2005 12:32 AM
Convert wmf to jpeg causes black background in result image. Jonas ASP .Net 1 08-12-2005 02:20 PM
Wrong image size of JPG after TIFF to JPG conversion with JAI Roland Java 0 05-12-2004 03:52 PM
Looking for software to combine 4 JPG images into one JPG image G.M. Durrence Digital Photography 4 04-08-2004 08:04 PM



Advertisments