S
S
savage_me2020-03-02 13:01:57
PHP
savage_me, 2020-03-02 13:01:57

How to convert an image to another one with only certain colors?

$img = imagecreatetruecolor($width,$height);

// Задаю палитру для нового изображения. Ожидаю, что полученная картинка будет состоять только из трех этих цветов.
imageColorAllocate($img, 255, 255, 255);
imageColorAllocate($img, 0, 0, 0);
imageColorAllocate($img, 128, 128, 128);

// Создаю изображение из исходного
$img2 = imageCreateFromJpeg($filename);

// Объединяю исходное изображение и изображение на основе трех цветов.
imageCopyMerge($img, $img2, 0, 0, 0, 0, $width, $height, 100);


As a result, $img is created, but not from three colors, but from more. Where is the mistake? And most importantly, how to get an image from the original image based on the three desired colors?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
U235U235, 2020-03-02
@U235U235

I hope your output is not jpeg?

M
mayton2019, 2020-03-02
@mayton2019

Yes. If you save in JPEG, then he will throw a pig at you. Tk works not with a discrete but with a continuous (sine-cosine) representation of small fragments of the picture.
Realistically, you need a guarantee that the destination will always be in GIF/PNG formats, since they are the only ones that guarantee the exact color match at the input and output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question