80x80 이미지를 2bpp의 그레이 스케일 56x56 이미지로 변환하려고합니다.
80x80 이미지는 색상이 있으며 아마도 최대 16 개의 색상이있을 수 있습니다. 그들은 또한 투명한 배경을 가지고 있습니다.
흰색이 가장 밝고 검은 색이 가장 어두운 4 가지 색상의 그레이 스케일이 필요합니다.
각 이미지에는 여러 색상이 있지만 각 색상에는 어두운 색상, 중간 색상 및 밝은 색상의 3 가지 색상 팔레트가 있습니다.
이미 이미지에있는 검은 색을 유지하면서 모든 어두운 항목을 어두운 회색으로, 중간 항목을 밝은 회색으로, 밝은 항목을 흰색으로 변환해야합니다.
이 명령으로 이미지를 그레이 스케일로 성공적으로 변환하고 캔버스를 자르고 배경을 채울 수 있습니다.
convert input.png +dither -flatten -trim -set colorspace Gray -
separate -average output.png
이제 색상을 제한해야하지만 올바른 색상을 변환하지 않습니다. 밝은 색상이 흰색 대신 밝은 회색으로 변환됩니다. -level 옵션을 변경하면 일부 이미지에서만 작동합니다.
-자동 레벨은 내가 원하는 것을 수행하지 않습니다.
어쨌든 내 요구 사항에 맞게 자동으로 레벨을 맞추기 위해 중간 범위의 색상을 설정할 수 있습니까? 충분히 설명하지 않으면 죄송합니다.
이것은 내가 조작 한 코드이지만 몇 개의 이미지에서만 작동합니다. 감마 옵션을 엉망으로 만들면 더 많은 이미지에서 작동하지만 원본 작업 이미지가 손상됩니다.
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
Btw, ^는 for 루프에 있으므로 변수입니다. gamme 및 black / white theshold 값을 변경하면 더 가까워 지지만 매우 지루하고 하나의 이미지를 가져와 다른 중단을 올바르게 변환 할 수 있습니다. nidoking.png는 내 리맵 파일입니다. remap은 완벽하게 작동합니다. 다시 매핑하기 직전에 색상이 제대로 분리되거나 필터링됩니다.
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
기본적으로 아이디어는 RGB 색상 공간 (회색조 색상 공간이 아닌)에서 4 색을 줄여 최상의 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
그래서 기본적으로 46이 두 번째로 어두운 색상이기 때문에 # 094152를 짙은 회색으로 대체합니다. 그런 다음 119가 다음으로 밝은 색상이기 때문에 # A95244를 밝은 회색으로 대체하고 # EF9E3C를 흰색으로 대체합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다