How do i convert any image into a greyscale?
and save?
Regards..
Printable View
How do i convert any image into a greyscale?
and save?
Regards..
I guess you already know how to use the Getpixel and Setpixelv API functions?
Then, use this function to convert the color back to a red, a green and a blue part:
Then do something like this:Code:ripped from an article on vbworld.com
Public Function UnRGB(RGBCol As Long, Part As Integer) As Integer
'Part: 0=Red, 1=Green, 2=Blue
Select Case Part
Case 0
UnRGB = RGBCol And &HFF
' mask 000000000000000011111111 and shift bits right
Case 1
UnRGB = (RGBCol And &HFF00) / &HFF
' mask 000000001111111100000000 and shift bits right
Case 2
UnRGB = (RGBCol And &HFF0000) / &HFFFF
' mask 111111110000000000000000 and shift bits right
End Select
End Function
You can save to a JPEG using this code from vbAccelerator. You can also make your own saving routines, file format specifications of common file types can be found at wotsit.org. It's quite hard to make a saving routine for GIF or JPEG. Saving to a BMP is relatively easy.Code:Dim Average As Integer
Dim NewColor As Long
Average = RedPart + GreenPart + BluePart
NewColor = RGB(Average/3, Average/3, Average/3)
I didn't make working code for you, but I think this will help you a lot on your way. :)
Another greyscale approach is to sep out colors the oetje shows, and then use luminance.
For RGB colors
luminance = 0.299*Red + 0.587*Green + 0.114*Blue
The gray color for the pixel is then
RBG(luminance,luminance,luminance)
Hi,
Thanks :) ,
Will this reduce the size of the original image?
Regards,
LXS
In memory it's not easy to reduce the size. If you save it the right way it reduces the size a lot. If you save it to a 8 bit greyscale BMP, instead of a 24 bit color BMP the size will be divided by 3. If you save it to GIF it won't matter much, because GIF only supports 256 colors. (A 256 color palette) I'm not sure about JPEG.