Results 1 to 5 of 5

Thread: grey scale

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    52

    grey scale

    How do i convert any image into a greyscale?
    and save?
    Regards..
    lxs

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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:

    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
    Then do something like this:
    Code:
    Dim Average As Integer
    Dim NewColor As Long
    
    Average = RedPart + GreenPart + BluePart
    NewColor = RGB(Average/3, Average/3, Average/3)
    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.

    I didn't make working code for you, but I think this will help you a lot on your way.
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3
    jim mcnamara
    Guest
    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)

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    52
    Hi,

    Thanks ,

    Will this reduce the size of the original image?

    Regards,
    LXS
    lxs

  5. #5
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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.
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width