简体   繁体   中英

how can i convert image to binary image using php?

Is there any way to convert Normal image to Binary Image (two possible values for each pixel. ie, a 0 or 1) ?

i have found some solution in Matlab but i am searching for a solution in PHP/JavaScript .

Using a lovely picture I've just taken of my keyboard I whipped up this example for you that converts a source image to a dithered 2-colour palette based image:

$src = imagecreatefrompng('test.png');

imagetruecolortopalette($src, true, 2);

header('Content-type: image/png');
imagepng($src);
imagedestroy($src);

Source:

调色板转换之前的源图像

Result:

在此处输入图片说明

If you don't want the dithering change the second parameter in imagetruecolortopalette($src, true, 2); to false :

在此处输入图片说明


Edited to add:

The picture above is very simple and practically 2-colour already. Depending on the complexity of your source image(s) the result might be significantly worse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM