How Do I convert an Image to black and white?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Contact:

How Do I convert an Image to black and white?

Post by Fasasoftware » Sat Aug 30, 2014 2:53 pm

Dear friends,

i need a very little script to transform an image in color to black and white...by pushing a button...

i have tryed some script in livecode's site...but it's don't work.... i saw the grayscale script example that works fine.... but i need only to convert an image to black and white....

can you help me please???

I thank you so much in advance.....

Best regards,

Lestroso :oops:

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How Do I convert an Image to black and white?

Post by Klaus » Sat Aug 30, 2014 3:22 pm

Could you please post the script you already found?
I am sure we can tune it to fit your needs!

Basically, you check if the GRAY value of a pixel is greater or smaller than an certain value (the threshold!)
and then turn that pixel white or black accordingly. 8)

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Contact:

Re: How Do I convert an Image to black and white?

Post by Fasasoftware » Sat Aug 30, 2014 4:22 pm

Dear Klaus i found these links but only in livecode's site.....

http://lessons.runrev.com/m/4071/l/2537 ... -grayscale this works very fine!!!

http://runrev.screenstepslive.com/s/352 ... processing <------this script don't work.....

as i told you i need to push a button and convert it from a color image into real black and white image...

Thanks a lot again,

Best regards,

Lestroso :oops:

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How Do I convert an Image to black and white?

Post by Klaus » Sat Aug 30, 2014 4:46 pm

Yes, I knwo what you are looking for! 8)

OK, although this binary stuff is way over my head, I was able to modify the color -> greyscale script according to the logic in my first response :D

Code: Select all

on mouseUp
   # Experiment with this valaue, which decides how grey a pixel must be to trun black or white
   put 128 into tThreshold
   ## 128 = 50% grey!
   
   local tPixel, tImgPosition, tGray, tImage
   put the imageData of image "Image" into tImage
   # repeat for all the pixels in the image
   repeat with tPixel = 0 to (height of image "Image" * width of image "Image") - 1
      put tPixel * 4 into tImgPosition
      
      # calculate the gray level - we are using typical weights here
      put 0.30 * charToNum (char (tImgPosition + 2) of tImage) + \
            0.59 * charToNum (char (tImgPosition + 3) of tImage) + \
            0.11 * charToNum (char (tImgPosition + 4) of tImage) into tGray
      # set the RGB of the pixel to the same value this gives us the gray level
      
      ## Her we decide if the resulting pixel will be balck or white!
      if tGray >= tThreshold then

         ## White
         put 255 into tGray
      else
         ## Black
         put 0 into tGray
      end if
      put numToChar (tGray) into char (tImgPosition + 2) of tImage
      put numToChar (tGray) into char (tImgPosition + 3) of tImage
      put numToChar (tGray) into char (tImgPosition + 4) of tImage
       end repeat
   
      ##assign the updated image data back to the displayed image
      set the imageData of image "Image" to tImage
      set the cHiddenImageData of image "Image" to tImage 
end mouseUp
Tested and works!

Hint:
This will not turn the image into a real B/W BITMAP image, it will only change the RGB values accordingly!


Best

Klaus

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Contact:

Re: How Do I convert an Image to black and white?

Post by Fasasoftware » Sat Aug 30, 2014 5:17 pm

KLAUS...your the best!!! Fast Answer!!!!

You have solved in full my problem!!!!! I thank you really!!! I appriciate so much!!!! Vielen Danke!!!

Best Regards,

Happy Summer,

Lestroso :D :D :D

Post Reply