• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert Color JPEG to Grayscale JPEG

 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avi,
The file size is affected by the compression which can be set via the ImageWriteParam class.

Good Luck,
Avi.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try a google search, i saw an article about doing this once. it was about adding a custom tab to JColorChooser
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic