【问题标题】:Imagemagick maximum colors and scalingImagemagick 最大颜色和缩放
【发布时间】:2017-07-01 18:07:28
【问题描述】:

我正在尝试将 80x80 图像转换为 2bpp 灰度的 56x56 图像。

80x80 图像是彩色的,其中可能有多达 16 种颜色。它们还具有透明背景。

我需要用 4 种颜色对它们进行灰度处理,白色是最亮的,黑色是最暗的。

每张图片都有多种颜色,但每种颜色都有 3 种颜色的调色板,一种是深色,一种是中等,一种是浅色。

我需要将所有深色转换为深灰色,将中等颜色转换为浅灰色,将浅色转换为白色,同时保持图像中已经存在的黑色。

我可以成功地将图像转换为灰度,修剪画布,并使用此命令填充背景

convert input.png +dither -flatten -trim -set colorspace Gray -
separate -average output.png

现在我需要限制颜色,但它没有转换正确的颜色。浅色正在转换为浅灰色而不是白色。当我更改 -level 选项时,它仅适用于某些图像。

-auto-levels 也不符合我的要求。

是否可以将中档的颜色设置为自动调平以满足我的要求?很抱歉,如果我解释得不够充分。

这是我一直在篡改的代码,但它仅适用于少数图像。使用 gamma 选项可以使其适用于更多图像,但会破坏原始工作图像。

convert "$f" +dither -flatten -trim -set colorspace Gray -separate -
average -gamma 1.37 -level 25%,75% -colors 4 -adaptive-resize 56x56\> 
-background white -gravity center -extent 56x56 -remap nido.png 
"${f%.png}".png2

我无法提供预期的图像,但提供的图像类似于预期的图像。这是原始图像https://img.pokemondb.net/sprites/black-white/normal/charizard.png,这是所需的输出格式图像https://img.pokemondb.net/sprites/red-blue/normal/charizard.png

这是我到目前为止所得到的 https://www.pokecommunity.com/showthread.php?p=9692599#post9692599

convert "$f" +dither -background white -flatten -trim -adaptive-resize 
56x56\> "${f%.png}".png2
convert "${f%.png}".png2 +dither -colorspace gray -separate -average 
"${f%.png}".png2
convert "${f%.png}".png2 +dither -gamma 3.0 -black-threshold 70% 
"${f%.png}".png2
convert "${f%.png}".png2 +dither -gamma 0.45 -white-threshold 90% 
"${f%.png}".png2
convert "${f%.png}".png2 +dither -remap nidoking.png -background white 
-gravity center -extent 56x56 "${f%.png}".png2

顺便说一句,^ 在 for 循环中,因此变量。改变游戏和黑白阈值让我更接近,但它非常乏味,当我得到一个图像来正确转换另一个中断时。 nidoking.png 是我的重映射文件。重映射工作完美,就在重映射之前,颜色被正确分离或过滤。

已解决,感谢 Mark Setchell

这就是我最终做的事情

#!/bin/bash
rm x*
rm colors*
cd images
rm *.png2
rm *.txt
for f in *.png
do

#Fitting canvas to image, setting background color, and removing transparency

    convert "$f" +dither -background white -flatten -trim "${f%.png}".png2

#Converting image to greyscale

    convert "${f%.png}".png2 +dither -colorspace gray -separate -average "${f%.png}".png2

#Resizing without blurring/adding pixels

    convert "${f%.png}.png2" +dither -interpolate Nearest -interpolative-resize 56x56\> "${f%.png}".png2

#Grabbing every color used in image and putting it in a text file

    convert "${f%.png}.png2" txt: | sed '1d' | cut -f 4 -d " " | sort -u > "${f%.png}".txt
done

#Putting all colors into one file

cat *.txt >> ../colors
cd ../

#One last clean up of file/sorting

cat colors | tr " " "\n" | sort -u > colors.txt
rm colors

#Splitting the hex codes into four files for each desired color

file=colors.txt
lines=$(wc -l <${file})
((lpp = (lines + 4 - 1) / 4))
split --lines=${lpp} ${file}

#Going back to images directory

cd images

for f in *.png
do

#Each while loop reads everyone line of the specified file and puts it in variable $i, then I use $i to convert to one of the desired 4 colors.

    cat ../xaa | while read i
    do
    convert "${f%.png}".png2 +dither -fuzz 0% -fill "#000000" -opaque "${i}" "${f%.png}".png2
    done

    cat ../xab | while read i
    do
    convert "${f%.png}".png2 +dither -fuzz 0% -fill "#555555" -opaque "${i}" "${f%.png}".png2
    done

    cat ../xac | while read i
    do
    convert "${f%.png}".png2 +dither -fuzz 0% -fill "#AAAAAA" -opaque "${i}" "${f%.png}".png2
    done

    cat ../xad | while read i
    do
    convert "${f%.png}".png2 +dither -fuzz 0% -fill "#FFFFFF" -opaque "${i}" "${f%.png}".png2
    done

    mv "${f%.png}".png2 ../finished/"${f}"

done

这个脚本变成了这个

进入这个

【问题讨论】:

标签: resize imagemagick pixel grayscale bpp


【解决方案1】:

基本上,我们的想法是在 RGB 色彩空间(而不是灰度色彩空间)中减少 4 种颜色,以获得最佳的四种颜色。然后获取每一个的亮度并将最暗的映射为黑色,将下一个较亮的映射为深灰色,将下一个较亮的映射为浅灰色,将最亮的映射为白色。

在这里它被映射到 RGB 颜色空间中的 4 种最佳颜色:

没有太多错误检查或极端情况处理的代码如下所示:

#!/bin/bash -x

# Do a colour reduction to get best 4 colours in RGB colourspace rather than in grey colourspace
magick pokething.png -alpha off +dither -colors 5 -unique-colors unique.png

# Get hex colours into array "hexcolours[]"
hexcolours=( $(convert unique.png txt: | awk 'NR>1{print $3}') )
echo DEBUG: hexcolours=${hexcolours[@]}

# Get lightness of each colour into array "lightness[]", i.e. H of HSL
# Note ggrep is just GNU grep
lightness=( $(convert unique.png -colorspace HSL -separate -delete 0,1 txt: | ggrep -Po "\d+(?=\)$)") )
echo DEBUG: lightness=${lightness[@]}

# Sort the colours by their lightness
fourshades=( $(for ((i=0;i<4;i++)) ;do
   echo ${lightness[i]} ${hexcolours[i]}
done | sort -n | awk '{print $2}') )
echo DEBUG: fourshades=${fourshades[@]}

# Now change those colours in original image
magick pokething.png -alpha off +dither -colors 5 -fuzz 10%   \
   -fill black  -opaque "${fourshades[0]}"                    \
   -fill gray25 -opaque "${fourshades[1]}"                    \
   -fill gray75 -opaque "${fourshades[2]}"                    \
   -fill white  -opaque "${fourshades[3]}"                    \
   result.png

输出如下:

DEBUG: hexcolours=#000000 #094152 #A95244 #EF9E3C
DEBUG: lightness=0 46 119 150
DEBUG: fourshades=#000000 #094152 #A95244 #EF9E3C

这导致执行:

magick pokething.png -alpha off +dither -colors 5 -fuzz 10% \
   -fill black  -opaque '#000000'                           \
   -fill gray25 -opaque '#094152'                           \
   -fill gray75 -opaque '#A95244'                           \
   -fill white  -opaque '#EF9E3C' result.png

所以,基本上我将 #094152 替换为深灰色,因为 46 是第二个最暗的颜色。然后我将 #A95244 替换为浅灰色,因为 119 是下一个较浅的颜色,然后将 #EF9E3C 替换为白色,因为这是最浅的颜色。

【讨论】:

  • 这需要一些时间来处理,我现在没有时间,但这听起来是个好主意。我不知道您可以使用 imagemagick 做到这一点,我尝试查找类似的内容,但我想我使用了错误的关键字。我确信我可以解决这个问题,我只是希望它与我正在尝试做的事情一致。非常感谢!
  • 这正是我最终在某种程度上做的事情。首先,我拍摄了所有图像并将它们转换为灰度。然后我用插值调整大小缩放它们。然后我为每个图像运行一个 for 循环,并抓取所有使用的颜色并将它们全部放在一个文件中。然后我将颜色均匀地分成 4 个单独的文件,并将 -fill -opaque 命令放在读取每个文件的 while 循环中。这比弄乱关卡等产生的结果要好得多。谢谢!!!!
  • 太棒了!祝你的项目好运:-)
【解决方案2】:

您几乎可以只使用“-remap”来获得您想要的结果。此示例将从白色、65% 灰色、35% 灰色和黑色创建一个四色映射,并将该映射写入临时内存。接下来,它会读取您的输入图像,将背景设置为白色,然后将图像展平。然后它关闭抖动,将输入图像重新映射到您创建的地图,并返回结果。

convert xc:white xc:gray65 xc:gray35 xc:black -append -write mpr:map +delete \
   input.png -background white -flatten +dither -remap mpr:map output.png

当然,您应该在重新映射之前进行任何调整大小、裁剪等操作。您可能不需要先将其转为灰度,因为地图没有颜色。

【讨论】:

  • 我试过这个,但有时较浅的颜色会与浅灰色混合,当它是边框或细节时会破坏图像。我编辑了我的帖子以显示我正在使用的当前代码,但取得了有限的成功。
猜你喜欢
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 2018-05-23
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多