【问题标题】:Generate a Photographic mosaic from a given set of thumbnails从一组给定的缩略图生成照片马赛克
【发布时间】:2016-05-18 01:31:03
【问题描述】:

Photographic mosaic 是一种将现有图像重新生成为缩略图马赛克的技术。原始像素的颜色应该与覆盖图块的颜色大致相似。

例如,role-playing gamer re-generated the world map from thumbnail images of users

source code for this image is shared on github,但它非常适合特定的世界地图任务。

是否有将现有图像重新生成为一组给定缩略图的拼贴/马赛克的通用解决方案?

【问题讨论】:

  • 不可能那么难。将大图像平铺成所需大小的平铺并获得每个平铺的平均颜色 - 这是 ImageMagick 的一行。将所有缩略图调整为所需的图块大小并获取每个图块的平均颜色 - ImageMagick 的另一行。浏览大图像并为其中的每个图块选择最接近的匹配缩略图作为马赛克输出。
  • 不,你是对的,这真的没那么难,但如果有一个不错的、现有的、开源的解决方案(我可能会为此做出贡献),我不喜欢重新发明轮子)。

标签: image dynamic-image-generation image-generation photographic-mosaic


【解决方案1】:

概念证明如下,作为一个简单的bash 脚本,使用 ImageMagick 完成图像处理工作。

#!/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 中完成,显然可以在感知上更均匀的色彩空间中完成,并且可以大大加快速度。为了避免重复,另一种想法可能是将图块 bin 分成相似的颜色,然后从最近的 bin 而不是绝对最近的 bin 中随机取一个。

文件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
...
...

【讨论】:

  • 哇!您已经从 imagemagick、awk 和 bash 中提取了令人惊奇的东西,并将所有内容组合成一段极其紧凑和优雅的代码。谢谢!然而,这真的让我想知道是否有任何开源解决方案,如果没有 - 让我想写一个。
  • 加油!并在您完成后在此处添加一个链接以进行宣传!
猜你喜欢
  • 1970-01-01
  • 2011-07-25
  • 2017-11-20
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
相关资源
最近更新 更多