ImageMagick: How to create a black rectangle with opacity 40%? - imagemagick

I work on a longer workflow to design elegant maps (project #Wikiatlas).
With ImageMagick shell command (convert?)...
How to output a 1200px wide, 1000px high black png image with an opacity of 40% ?

Have a look at section Semi-Transparent Colors on the Color Basics and Channels page of the ImageMagick Examples:
convert -size 1200x1000 xc:'rgba(0, 0, 0, 0.4)' out.png
This uses the xc pseudo-format with an RGBA color.

Related

ImageMagick Unable to change colour in shade explicitly for image

I want to use ImageMagick to change colour in shade.
I am able to manage the shade change using :
convert input.png -colorspace HCL -channel R -evaluate set 5% +channel -colorspace sRGB output.png
Using set XX% i am able to get different colours like, red, green, yellow, blue, pink, sky-blue, gray, etc.
The below command works for targeting blue colour :
convert input.png -colorspace HCL -channel R -separate +channel -level 48,52% output.png
But I am unable to target other colour explicitly.
For example, if I want to change green colour with some other colour, resulted image will effect green, yellow,red and sky-blue as well.
Is there a way to explicitly change a single colour in shade for :
yellow
sky-blue
pink
green
white
black
red
I tried changing all -channel : R,G,B,C,M,Y,K,A,O.
Using -separate option I can target RBG, but the problem with RGB is R effect red, yellow and pink, G effect green, sky-blue and yellow and B effect blue, pink and Sky-blue.
sample for output :
RGB image colour change
expected output : In the above output for "output-0" it effect red,yellow and pink. i want the command which will effect only red. similarly for other colours as well.
links I used : https://www.imagemagick.org/discourse-server/viewtopic.php?t=33361
I am using python to run this command. I am also open to use other libraries which will work with all the colours explicitly.
If your image is representative like I requested, it is as simple as this:
magick rgb.png -fill white -opaque red result.png
If you also want to affect hues "close to red", you can apply some fuzz:
magick rgb.png -fuzz 40% -fill white -opaque red result.png
Notice that also affects the edges of the red circle where it is a "feathered red".
If not, your ImageMagick code is essentially doing a "Hue rotation" and, as you have noticed, it affects the entire image. Read the Wikipedia page on HSV before continuing. Here is an HSI Hue wheel for reference:
The solution is to do your Hue rotation, but apply its effects via a mask that only selects the colours/areas you want affected. Remember that OpenCV halves the Hue from the range 0..360 to 0..180 so that it can store a Hue in a np.uint8.
So, if we load the same image as above and select only the greens (where Hue is near 120) we can rotate just those into blues by adding 60 (Hue=240):
#!/usr/local/bin/python3
import cv2 as cv
import numpy as np
# Load the image and convert to HSV colourspace
image = cv.imread("rgb.png")
# Convert to HSV and split channels
hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
H,S,V = cv.split(hsv)
# Shift only greens (Hue near 120) around hue circle by 120 degrees to blues - remembering OpenCV halves all these values - see comment
H[(H>55)&(H<65)] += 60
# Recombine into single 3-channel image and convert back to RGB
result = cv.merge((H,S,V))
result = cv.cvtColor(result,cv.COLOR_HSV2BGR)
cv.imwrite("result.png",result)
If you want to change the blues (Hue=240) into yellows (Hue=60), just change this:
H[(H>55)&(H<65)] += 60
into this:
H[(H>115)&(H<125)] -= 90
If you want to broaden the range of greens affected, decrease the 55 in my code and/or increase the 65. If you want to move greens to a different hue, either increase or decrease the 60.
You can do all the stuff above with PIL/Pillow if you want to - you don't need to install the (massive) OpenCV.
Keywords: Image, image processing, Python, OpenCV, ImageMagick, Hue, HSL, HSV, hue rotation, colour replacement, selective colour, mask.

Set alpha according to gradient with imagemagick

Is it possible to set the alpha channel of an image according to a gradient with ImageMagick?
I'd like the pixels on the left border of an image to be 100% transparent, and the ones on the right border to be 100% opaque, and with the ones in the middle having progressively lower transparency values.
Or in a more general case - given a grayscale image, set the alpha channel of another image as a function of the B&W values (black = 100% alpha, white 0% alpha).
With ImageMagick you can use -sparse-color to apply a gradient only to the alpha channel to get the result you describe.
convert in.png -alpha set -background none -channel A \
-sparse-color barycentric "0,0 none %[w],0 white" +channel out.png
That command starts by activating the alpha channel and setting the background color to transparent. Then it uses -channel A to apply the following operation only to the alpha channel. The -sparse-color operation tells it to start with transparent at the far left edge, pixel 0,0 and graduate to opaque at pixel %[w],0. The %[w] means the width or far right edge.
Although there are many ways to accomplish the effect you've described, by using -sparse-color you can easily make the gradient start and end at any positions on the image without having to create any intermediate masking images.
Simple. You would use -composite CopyOpacity to set the alpha channel from a gradient mask.
Given I have the following images. image.png & transparent_mask.png
We can set the image transparency (where black is alpha, and white is opaque) by copying values from the mask image to the input image alpha channel.
convert image.png transparent_mask.png -compose CopyOpacity -composite output.png

Imagemagick inline border without resizing the original image

I tried to add a border to an image of dimension 200x200 px using the below code.
convert -border 2x2 -bordercolor "#cccccc" old.png new.png
The above code ads a 2 px border around the old.png. Therefore, the image expands to 204x204 px.
However, I want to add an inline border. I have no issues with border overlaying edge portions of the old image. Thus, the new image should be able to retain the dimensions 200x200 px. Please advise how exactly to do that.
You need to shave two pixels all around and then add a two pixel border. You should try this with Imagemagick 6. If using Imagemagick 7, replace convert with magick.
convert old.png -shave 2x2 -bordercolor "#cccccc" -border 2 new.png

Stop ImageMagick from pre-multiplying during composite

ImageMagick is premultiplying transparent pixels. This causes a gray outline to appear during subsequent transformations.
For example:
$ convert -size 1085x558 xc:"rgba(0,0,0,0)" PNG32:temp.png
$ composite -gravity center samples/logo_white.png temp.png PNG32:temp.png
Here are the source and resulting images.
Here is a video showing that the temp.png image has had its transparent pixels turned from white to black.
Is there a way to force ImageMagick to leave fully transparent pixels alone rather than changing them to black?

How do I set a colour to be transparent in a GIF using ImageMagick?

I've had a hard time finding out how to set a colour (black in my case) to be transparent in a GIF using ImageMagick. How can I do that to a set of existing GIF images?
I've tried this option but it doesn't seem to do anything:
mogrify -transparent-color black -transparent black *.gif
I've read through this but it seems a little baffling in what actually makes a colour become transparent in a GIF: http://www.imagemagick.org/Usage/formats/#boolean_trans
For example:
Note that setting "-transparent-color" does NOT add any transparency
to a GIF image, nor does it convert the specified color to become
transparent. All the option does is specify what color should placed
in the color table for the color index that is used representing the
transparent colors in a GIF image.
If you want to change a specific (exact) color to become transparent,
then use the "-transparent" Color Replacement Operator.
but then...
Use +transparent to invert the pixels matched. that is make all
non-matching colors transparent.
The -opaque operator is exactly the same as -transparent but replaces
the matching color with the current -fill color setting, rather than
transparent. However the -transparent operator also ensures that the
image has an alpha channel enabled, as per "-alpha set", and does not
require you to modify the -channel to enable alpha channel handling.
Note that this does not define the color as being the 'transparency
color' used for color-mapped image formats, such as GIF. For that use
-transparent-color
I'd like to warn you from using mogrify. mogrify does convert images by overwriting the original ones. If anything goes wrong, your images are gone.
Rather use convert. You can always delete your original images after you are sure the conversion is ok.
To make black pixels transparent, use this:
convert orig.gif -transparent black transp.gif
Works perfectly for me.
My IM version: ImageMagick 6.7.8-0 2012-07-04 Q16. What's yours?
If it still doesn't work for you, the 'black' in your GIFs may not be black after all, but only very dark gray. ImageMagick also has tools to enumerate all colors in a GIF. The following 2 commands may help you in this case:
identify \
-format "%f: - Uniq Colors: %k - Image transparency channel enabled: %A - Image Depth: %z %c\n" \
*.gif
and
convert \
*.gif \
-format "%f :\n%c\n\n" \
histogram:info:
Use of color black can be identified by name 'black', by RGB-values '(0, 0, 0)' as well as by hex value '#000000'. So pure blacks should appear in the output of the last command as something like:
5000: ( 0, 0, 0) #000000 black
Dark grays could for example be:
100: ( 14, 14, 14,255) #0E0E0E srgba(14,14,14,1)
2100: ( 1, 1, 1,255) #010101 srgba(1,1,1,1)
Once there is transparency enabled in your GIF, the last command should show a tuple of 4 values for each color, the last value representing the Alpha channel. Your formerly black would appear in the output as something like:
5000: ( 0, 0, 0, 0) #00000000 none
Try -background color once you generate it to GIF.
Check the http://www.imagemagick.org/script/mogrify.php url for both mogrify and background options. See the image below.

Resources