Converting colors (not images) with ImageMagick
In ImageMagick, you can do the following:
convert xc:"cmyk(0,255,255,0)" -colorspace sRGB -format "%[pixel:u.p{0,0}]\n" info:
red
convert xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93%,11%,14%)
ImageMagick - convert an image's white colors to another, keeping transparency and excluding black
Perhaps this is what you want in Imagemagick 7. Here I make an in-memory copy of the image. I then turn alpha off and process all pure white. Then I extract the alpha channel from the in-memory copy and put it back onto the resulting green image.
Input:
Unix syntax:
magick nCijg.png +write mpr:img \
-alpha off -fill "#009900" -opaque "#FFFFFF" \
\( mpr:img -alpha extract \) \
-alpha off -compose copy_opacity -composite \
result.png
Windows syntax:
magick nCijg.png +write mpr:img ^
-alpha off -fill "#009900" -opaque "#FFFFFF" ^
( mpr:img -alpha extract ) ^
-alpha off -compose copy_opacity -composite ^
result.png
Result:
You can include some fuzz if you want to include non-white to fill some small holes in the white.
Replace every color except a specific one in Imagemagick
This would be the +opaque
option.
convert YiCYA.png -fill black +opaque '#FC00FF' output.png
Convert all opaque pixels to white, keeping transparency
This was a true PEBKAC situation. I only needed the -strip
flag, because what I thought to be all-white images were in fact correctly painted to white with their original transparency data preserved, only the image thumbnails were incorrectly generated on my computer.
Imagemagick replace all colors in image with white
You can do that in ImageMagick using -colorize.
convert input.png -fill white -colorize 100 output.png
It will preserve the transparent pixels and make everything else white.
Imagemagick maximum colors and scaling
Basically, the idea would be to do a reduction to 4 colours in the RGB colourspace (rather than in greyscale colourspace) to get the best four colours. Then get the lightness of each of those and map the darkest one to black, the next lighter to dark grey, the next lighter to light grey and the lightest to white.
Here it is mapped to the 4 best colours in RGB colourspace:
The code, without much error checking or corner-case handling, looks like this:
#!/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
The output is as follows:
DEBUG: hexcolours=#000000 #094152 #A95244 #EF9E3C
DEBUG: lightness=0 46 119 150
DEBUG: fourshades=#000000 #094152 #A95244 #EF9E3C
That results in this being executed:
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
So, basically I am replacing #094152 with dark grey because 46 is the second darkest colour present. Then I am replacing #A95244 with light grey because 119 is the next lighter colour, then replacing #EF9E3C with white because that is the lightest colour.
Using ImageMagick to remove all color except black in an image?
To match all colors except black you can use +opaque "#000000"
.
In order to include a little range around #000000 you can try different percentages with the fuzz
operator:
convert input.png -fill white -fuzz 10% +opaque "#000000" result.png
Tested with ImageMagick 6.6.0-1 on Windows
Re-color a background of a scanned image with ImageMagic
In ImageMagick, you can recolor the background using -fill ... -opaque
. Measure the color of the background, then
convert insect.png -fuzz 5% -fill skyblue -opaque "rgb(235,215,186)" insect_blue.png
The -fuzz X%
permits coloring of close but not exactly the measured color. This allows non-uniform background color to be changed. The larger the X%
, the more it will color parts of the image.
A value of 0%
means only color the exact value measured.
See https://legacy.imagemagick.org/Usage/color_basics/#opaque
Related Topics
Disabling Flash Message Without Disabling Cache on Click on Back Button in Rails
Rspec: How to Write Unit Test Case to Receive an Exception Which Is Getting Raised in Private Method
Replicating Xml Request with Savon/Ruby
Setting Up a Polymorphic Association
Sending Form Data to Remote Rails Application Using Httparty
How to Create Nested Model from Partial Rails 6
Nomethoderror: Undefined Method '[]' for #<Activerecord::Migration:0X000000033Fedc0>
Fetching Second Row from CSV File in Ruby
How to List All Databases with Activerecord
Ruby - How to Retrieve Sum in Array Group by Multiple Keys with Condition Max
Ruby/Rails - Active Record Db Migration to MySQL - Timestamp Type
My Classes Can't Use Shoes Methods Like Para
How to Require Activerecord in Irb
Does Will Pagination Work with Forms Which Have Method="Post"