aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Convert Color JPEG to Grayscale JPEG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Convert Color JPEG to Grayscale JPEG" Watch "Convert Color JPEG to Grayscale JPEG" New topic
Author

Convert Color JPEG to Grayscale JPEG

Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Greetings,
I am using Java 5 on Windows 7.
I am converting a color JPEG to a grayscale JPEG using only classes from JDK.
Here are the relevant parts of my code:

After running my code, I open the image files using Windows Photo Viewer
Here are some of the properties of the images as reported by Windows Photo Viewer
Original (color)
Width = 1888 pixels
Height = 864 pixels
Horizontal Resolution = 300 dpi
Vertical Resolution = 300 dpi
Bit Depth = 24
File Type = JPG
File Size = 172 KB

Grayscale
Width = 1888 pixels
Height = 864 pixels
Horizontal Resolution = 96 dpi
Vertical Resolution = 96 dpi
Bit Depth = 8
File Type = JPG
File Size = 304 KB

I assume that the resolution of the grayscale image is 96 dpi because that is probably the resolution of my computer screen.
However, I don't understand why the file size is bigger for the grayscale image.
Please explain to me why the grayscale image's file size is so large compared to the original, color image's file size.

Thank you,
Avi.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Yet again, I reply to my own post ;-)
Even if I don't convert the image to grayscale, i.e. simply read it and then write it, the resulting file is larger than the original.

This leads me to believe that the JDK's default JPEG writer is not optimal, i.e. other JPEG writers can compress images better.
Can someone perhaps point me to a better JPEG writer than that used by class "ImageIO" ?

Cheers,
Avi.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Avi,
The file size is affected by the compression which can be set via the ImageWriteParam class.

Good Luck,
Avi.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

try a google search, i saw an article about doing this once. it was about adding a custom tab to JColorChooser


SCJP
Visit my download page
 
wood burning stoves
 
subject: Convert Color JPEG to Grayscale JPEG