convert image to alternating black and white

So I have a couple of images of logos in a rasterized format (png, no vectors).
I want to convert them to black and white with no grayscale. i.e. binary either a full black (0,0,0) or a full white (255, 255, 255).

The issue is that I do not want to just pick a cutoff threshold and set everything above to white and everything below to black.

Instead I want it to alternate colors.

For example with the lego logo I want to convert it from this

lego

to this

lego bw

But I am having a hard time finding any form of algorithm that does the job.
Is there any suitable algorithm for this?

bonus points if there is a decent implementation of it on npm