![](/img/trans.png)
[英]Rails: Any gems to automatically generate thumbnails from given link?
[英]Generate a Photographic mosaic from a given set of thumbnails
攝影馬賽克是一種將現有圖像重新生成為縮略圖馬賽克的技術。 原始像素的顏色應大致類似於覆蓋圖塊的顏色。
例如,一個扮演角色的游戲玩家從用戶的縮略圖中重新生成了世界地圖 。
該圖像的源代碼在github上共享 ,但是它是為特定的世界地圖任務量身定制的。
是否存在用於將現有圖像重新生成為一組給定縮略圖的拼貼/馬賽克的通用解決方案?
隨后是概念證明,它是使用ImageMagick進行圖像處理的簡單bash
腳本。
#!/bin/bash
# Take all JPEGS in current directory and get their average RGB color and name in "tiles.txt"
for f in *.jpg; do convert $f -depth 8 -resize 1x1! -format "%[fx:int(mean.r*255)] %[fx:int(mean.g*255)] %[fx:int(mean.b*255)] $f\n" info: ; done > tiles.txt
# Create empty black output canvas same size as original map
convert map.png -threshold 100% result.png
# Split map into tiles of 10x10 and get x,y coordinates of each tile and the average RGB colour
convert map.png -depth 8 -crop 10x10 -format "%X %Y %[fx:int(mean.r*255)] %[fx:int(mean.g*255)] %[fx:int(mean.b*255)]\n" info: |
while read x y r g b; do
thumb=$(awk -v R=$r -v G=$g -v B=$b '
NR==1{nearest=3*255*255*255;tile=$4}
{
tr=$1;tg=$2;tb=$3
# Calculate distance (squared actually but sqrt is slow)
d=((R-tr)*(R-tr))+((G-tg)*(G-tg))+((B-tb)*(B-tb))
if(d<nearest){nearest=d;tile=$4}
}
END{print tile}
' tiles.txt)
echo $x $y $r $g $b $thumb
convert result.png -draw "image copy $x,$y 10,10 \"$thumb\"" result.png
done
我沒有無休止的縮略圖供應,但是這個概念似乎行得通。 顏色之間距離的數學運算是在awk
完成的,顯然可以在感知上更統一的色彩空間中完成,並且還可以大大加快操作速度。 另一種認為,為避免重復,可能是箱瓷磚到相似的顏色,然后取一個隨機從最近的倉,而不是絕對的最近的一個。
文件tiles.txt
如下所示:
111 116 109 0.jpg
82 88 81 1.jpg
112 110 95 10.jpg
178 154 150 100.jpg
190 169 163 101.jpg
187 166 163 102.jpg
...
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.