2014-10-06 140 views
0

背景,我將圖像轉換爲ASCII藝術。這可以很好地工作,甚至可以使用24位顏色,將顏色轉換爲正確的rgb值。不過,我現在想用4位調色板而不是24位渲染ascii藝術。PHP將24位顏色轉換爲4位

如何使用PHP將24位顏色轉換爲4位?

更具體地說,我有我需要將任何給定的十六進制或RGB值轉換爲標準的IRC顏色托盤。當轉換爲4位顏色時,顏色最好儘可能匹配。

其他的想法是將圖片本身轉換爲4位調色板(使用GD,現在我用它來讀取顏色),然後嘗試從中獲取顏色。另一個想法可能是爲每種顏色定義一個顏色範圍,並檢查給定的24位顏色是否在範圍內,但是我不知道如何將所有顏色的範圍都放到該調色板中。

enter image description here

回答

0

最後,儘管圍繞imagemagick的精彩建議我發現了一個使用直接php的好方案。我能計算出通過採用ΔE的2000的用php-色差庫的修改版本在GitHub上找到的最接近的顏色,這是我的叉:https://github.com/nalipaz/php-color-difference

相關的例子是:

<?php 
include('lib/color_difference.class.php'); 

$palette = array(
    '00' => array(255, 255, 255), 
    '01' => array(0, 0, 0), 
    '02' => array(0, 0, 139), 
    '03' => array(0, 128, 0), 
    '04' => array(255, 0, 0), 
    '05' => array(139, 0, 0), 
    '06' => array(128, 0, 128), 
    '07' => array(255, 165, 0), 
    '08' => array(255, 255, 0), 
    '09' => array(50, 205, 50), 
    '10' => array(0, 128, 128), 
    '11' => array(173, 216, 230), 
    '12' => array(0, 0, 255), 
    '13' => array(255, 105, 180), 
    '14' => array(128, 128, 128), 
    '15' => array(211, 211, 211), 
); 

$color_rgb = array(255, 255, 128); 
$color_delta_e = new color_difference($color_rgb); 
$match_index = $color_delta_e->getClosestMatch($palette); 
$color = $palette[$match_index]; 

我很滿意這個解決方案和更少的開銷。感謝您的建議傢伙。

0

我覺得ImageMagick的(或GraphicsMagick工具)可以用-depth選項做到這一點。這裏有一個討論:http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=15395

更新:我應該補充說,ImageMagick不是一個PHP庫,但是它有一個PECL包裝(imagick)在http://pecl.php.net/package/imagick

+0

ImageMagick可以成爲一個沒有太多麻煩的要求。我猜我需要在使用此權限之前將任何jpe?g和gif轉換爲png的? – 2014-10-06 23:02:28

+0

我認爲-depth選項適用於各種格式 - 請參閱http://www.imagemagick.org/script/command-line-options.php#depth。 – cjs1978 2014-10-06 23:12:30

+0

深入研究深度變化。這可能會有所幫助,但這並不代表我需要用上面的調色板中的顏色替換顏色。生成的圖像只需要是上面調色板中的顏色。也許如果我將深度改變降低到16,然後用上面的16種顏色替換,那麼它可能會工作。然而,我將需要研究顏色的替換,並在結果圖像中將上述每種顏色與感性顏色進行匹配。 – 2014-10-07 01:55:50

0

imagetruecolortopalette允許您減少顏色,但結果可能會有很大差異,我不知道是否存在正確映射顏色或指定調色板的方法。

測試圖像(24位):

Bliss

減少到4位(無抖動):

$img = imagecreatefrompng('Bliss.png'); 
imagetruecolortopalette($img, false, 16); 
imagepng($img, 'Bliss2.png'); 

Bliss, without dithering

減少到4位(機智^ h抖動):

$img = imagecreatefrompng('Bliss.png'); 
imagetruecolortopalette($img, true, 16); 
imagepng($img, 'Bliss3.png'); 

Bliss, with dithering

正如你所看到的,結果是遠遠不夠完善。但也許這對你而言是一個好的開始。

0

我認爲您需要使用remap將圖像中的顏色映射到色板中的顏色調色板。我這樣做是在這樣的命令行:

convert image.jpg -remap palette.jpg out.jpg 

您可能,或者可能不希望dither選項 - 檢查出來。

原圖是在這裏:

enter image description here ,這是我palette.jpg(你只需要一個非常小的圖像,這是太大了 - 我會盡快解決這個問題)

enter image description here

和結果 enter image description here

你也可以根據你的顏色創建你的調色板使用ImageMagick。我手工編碼以下,並沒有太在意,所以你想假設之前入住這裏的RGB值,他們是正確的:

#/bin/bash 
cat<<EOF | convert txt:- palette.png 
# ImageMagick pixel enumeration: 8,2,256,rgb 
0,0: (255,255,255) 
1,0: (0,0,0) 
2,0: (0,0,255) 
3,0: (255,255,0) 
4,0: (255,0,0) 
5,0: (128,128,128) 
6,0: (255,105,180) 
7,0: (173,216,230) 
0,1: (50,205,50) 
1,1: (139,0,0) 
2,1: (255,165,0) 
3,1: (128,0,128) 
4,1: (0,0,139) 
5,1: (0,128,128) 
6,1: (0,128,0) 
7,1: (211,211,211) 
EOF 

基本上,上面的腳本讓ImageMagick中的RGB值文本,並要求其作出看起來像這樣小的8x2圖片:

enter image description here

那麼你可以使用這個調色板與remap操作。