How to convert image to GIF with only two color index in Imagick -


i want convert image gif format 2 color, black , white

so every pixel in output image black or white

does know way convert in imagick ?

although final conversion write out two-color gif same, there subtly different ways of converting image 2 color black , white.

here methods below:

helper method force images pixels of values 0,0,0 , 255,255,255

function forceblackandwhite(imagick $imagick, $dithermethod = \imagick::dithermethod_no) {     $palette = new imagick();     $palette->newpseudoimage(1, 2, 'gradient:black-white');     $palette->setimageformat('png');     //$palette->writeimage('palette.png');      // make image use these palette colors     $imagick->remapimage($palette, $dithermethod);     $imagick->setimagedepth(1); } 

just use remap palette force image 2 colors without dithering.

function twocolorpaletteonly() {     $imagick = new imagick(__dir__."/../images/biter_500.jpg");     forceblackandwhite($imagick, \imagick::dithermethod_no);     $imagick->setimageformat('gif');     $imagick->writeimage("./outputpalette.gif"); } 

palette output:

enter image description here

using http://phpimagick.com/imagick/posterizeimage allows different control on dithering process.

function twocolorviaposterize() {     $imagick = new imagick(__dir__."/../images/biter_500.jpg");     $imagick->transformimagecolorspace(\imagick::colorspace_gray);     $imagick->posterizeimage(2, \imagick::dithermethod_riemersma);     forceblackandwhite($imagick);     $imagick->setimageformat('gif');     $imagick->writeimage("./outputposterize.gif"); } 

posterize output:

enter image description here

the thresholdimage function allows control @ @ 'level' image changes black white.

function twocolorviathreshold() {     $imagick = new imagick(__dir__."/../images/biter_500.jpg");     $imagick->transformimagecolorspace(\imagick::colorspace_gray);     $imagick->thresholdimage(0.5 * \imagick::getquantum());     forceblackandwhite($imagick);     $imagick->setimageformat('gif');     $imagick->writeimage("./outputthreshold.gif");  } 

threshold output:

enter image description here

using blackthresholdimage , whitethresholdimage functions allows control color threshold per channel

function twocolorviacolorthreshold() {     $imagick = new imagick(__dir__."/../images/biter_500.jpg");     $thresholdcolor = "rgb(127, 100, 100)";     $imagick->blackthresholdimage($thresholdcolor);     $imagick->whitethresholdimage($thresholdcolor);     forceblackandwhite($imagick);     $imagick->setimageformat('gif');     $imagick->writeimage("./outputcolorthreshold.gif"); } 

colorthreshold output

enter image description here

extracting single image channel can produce 'cleaner' looking output image.

function twocolorviacolorchannelthreshold() {     $imagick = new imagick(__dir__."/../images/biter_500.jpg");     $imagick->separateimagechannel(\imagick::channel_red);     $imagick->thresholdimage(0.5 * \imagick::getquantum());     forceblackandwhite($imagick);     $imagick->setimageformat('gif');     $imagick->writeimage("./outputcolorchannelthreshold.gif"); } 

colorchannelthreshold

enter image description here

we can combine rgb channels more precisely using colormatriximage function, gives complete control on how separate r g b values should affect output image.

function twocolorviacolormatrixchannelthreshold() {     $imagick = new imagick(__dir__."/../images/biter_500.jpg");       //      $colormatrix = [         0.6, 0.2, 0.2, 0.0, 0.0,         0.0, 0.0, 0.0, 0.0, 0.0,         0.0, 0.0, 0.0, 0.0, 0.0,         0.0, 0.0, 0.0, 1.0, 0.0,         0.0, 0.0, 0.0, 0.0, 0.0,     ];      // intensity of red channel after applying color matrix takes     // values pixels before transformation of:     // 60% of red, 20% blue, 20% green     $imagick->colormatriximage($colormatrix);     $imagick->separateimagechannel(\imagick::channel_red);     $imagick->thresholdimage(0.5 * \imagick::getquantum());     forceblackandwhite($imagick);     $imagick->setimageformat('gif');     $imagick->writeimage("./outputcolormatrixchannelthreshold.gif"); } 

colormatrixchannelthreshold output

enter image description here

the output images code above are:


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -