image.binary() outputs image in GRAYSCALE instead of BINARY


I am trying to save image in sensor.BINARY format but image.binary saves in sensor.GRAYSCALE format.

I have used example,

Color Binary Filter Example

This script shows off the binary image filter. You may pass binary any

number of thresholds to segment the image by.

import sensor, image, time

sensor.set_pixformat(sensor.GRAYSCALE) # USING GLOBAL SHUTTER CAMERA → changed from sensor.RGB565
sensor.skip_frames(time = 2000)
clock = time.clock()

# Test red threshold
for i in range(100):
    img = sensor.snapshot()
> OUTPUT: {"w":320, "h":240, "type"="grayscale", "size":76800}

Because of this problem, I cannot save image.binary() into extra frame buffer pre-defined as "sensor.BINARY".

I was expecting it would automatically convert image to binary format after image.binary().

Without looping through image and assigning pixel-by-pixel into frame buffer, is there a simple method doing this?

I tried to look for examples but there is no conversion examples for binary image.

I look forward to your reply,
Kind Regards


img.binary([red_threshold], to_bitmap=True)

Binary just thresholds an image. Use to_bitmap() to get a binary image.

Yes, the terminology is confusing because this all wasn’t designed at the same time and we can’t rename things.

Thank you all for the reply.

“to_bitmap()” works like a magic!

Sorry I have missed out reading about “to_bitmap” flag.