RMagick: Setting opacity on a png that already has transparent elements - ruby

I need to compose images in rmagick. If I put a png that has transparent regions on another image and set the opacity of that png to 50% the parts that where transparent become white (with 50% opacity). But I want these regions to stay transparent.
Here's my code:
canvas = Magick::Image.new(1024,768)
canvas.opacity = Magick::MaxRGB
image = Magick::ImageList.new('/tmp/trans.png').first
image.background_color = "none"
image.opacity = Magick::MaxRGB/2
canvas.composite!(image, 50, 50, Magick::OverCompositeOp)
canvas.write('/tmp/composite.png')
Any suggestions?

After 8 hours of Googling I came across this post which allowed me to come up with the answer.
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=19169
convert FileIn.png -channel a -evaluate Multiply 0.5 +channel FileOut.png
-channel a forces the selection of the alpha channel
-evaluate Multiply 0.5 modifies the transparency of the image
+channel changes the selection to include all of the channels in the output
And then to merge the two transparent png's and end up with a 3rd transparent png
(Please note I'm using GraphicsMagick for this operation instead of ImageMagick)
gm convert FileIn1.png FileIn2.png -background transparent -mosaic FileOut.png

Related

No-crop canvas increase to 2:1 aspect ratio with ImageMagick

I am in a situation where I have a set of images with variable width and height and I need to ensure that those images comply with an aspect ratio of 2:1.
The only assumption I can make is that those images have an aspect ratio greater than 2 so I need to expand the canvas.
With ImageMagick I can do this:
magick input.jpg -background black -gravity north -extent 2:1 output.jpg
But this crops instead of adding canvas as desired. Is there an option to avoid cropping, i.e. force image padding? Remember I do not know the image resolution beforehand.
Of course with a more complex script I can extract the image dimensions and compute the desired resolution but I am trying to keep things simple if possible.
P.S.: The images comes from a laser scanner and are going to be used in a 360 panorama viewer hence the need to meet an aspect ration without cropping.
With ImageMagick v7 you can do some inline calculations that will adjust the results of the -extent operation to pad any size input to a 2:1 ratio. See this command as an example...
magick input.png -gravity center -extent "%[fx:max(w,h*2)]x%[fx:max(w/2,h)]" result.png
That will pad any extra space with black. Add -background <somecolor> before the extent operation to change the padding color.

How to convert coloured Captchas to Grey Scale?

I'm trying to make a capcha solver, but I have ran into some trouble. The captcha that I am trying to solve has different coloured backgrounds.
I need to convert it to black text on white background so that it could easily be recognised by tesseract-ocr
I have tried
convert *.png -threshold 50% *.png which only shows some of the digits.
The problem with simple 50% thresholding is that both colours may be lighter than 50% grey and will therefore come out as white. Or, conversely, both colours may be darker than mid-grey and therefore bith come out as black.
You need to do a 2-colour quantisation to get just 2 colours, then go to greyscale and normalize so the lighter colour goes white and the darker one goes black. I am not near a computer, to test, but that should be:
convert input.png -colors 2 -colorspace gray -normalize result.png
Now, you will find some images are inverted (black on white instead of white on black), so you can either test the top left corner pixel and if it is white, then invert the image. Or, you could get the mean of the image and if it is more than 0.5 that would indicate that the image is largely white and therefore needs inverting.
Invert with:
convert input.png -negate output.png
Get top-left pixel with:
convert image.png -format '%[pixel:p{0,0}]' info:-
Get mean value with:
convert image.png -format "%[mean]" info:-

Convert grayscale image to progressive black plus transparency?

I have a grey scale image, from which I wish to convert all grey pixels into half transparent pixels, and white ones into transparent pixels.
How could I process a grey scale raster image via shell ?
Input :
Output (here made via Gimp):
Current Gimp process via GUI:
GIMP 2.6 > Load your shaded relief image (....shaded.tif : are grayscale)
or a screenshoot of your shaded relief (screenshot : RGB colors)
Force it to be RGB: Gimp > Image > Mode > RGB, click.
Delete the grey : Colors > "color to alpha" pop up > uncheck "preview", click on the horizontal color rectangle > "Color to alpha color picker" pop up> bottom right corner, click on the icon eyes dropper > choice you color of to delete (some grey pixel in a flat plain) > validate.
Delete an other color (white, black background) > same.
File > save as > ProjectName_relief_whitened.png (to keep transparency)
[note: SO images display, and image background CSS makes hard to see the subtile differences between files.]
Given the following gray scale input.png :
1a. To make black pixels of this image transparent and linearly keep the white pixels as they are, run this command:
convert source.png -alpha copy -fx '#fff' result.png
1b. To make white pixels transparent and linearly keep the black as they are, use:
convert source.png -alpha copy -channel alpha -negate +channel result.png
Manual:
convert – is the ImageMagic command (one of several)
source.png – is the greyscale source image.
-alpha copy – it copy contents of the previous file into the alpha channel.
-channel alpha – it specify that following operators only should affect the alpha channel.
-negate – it invert the current channel (channel alpha).
+channel – Specify that following operators only affect the opposite channel. For us, it switch focus from the alpha channel, to the color channel. (color channel is initially the default)
-fx '#000' – Replace current channel (for us, the color channel) contents with black pixels, so the end result actually fully depends on the alpha channel. If not included, all semi-transparent pixels in generated image will retain colors, from #FFF (white) to #000 (black).
Result of 1b:
Wiping out plains:
An additional processing could wipe out most of the flat plains, which appears around greys (#DDDDDD) with opacity:~50%. This could be done by :
convert input.png -fuzz 8% -transparent "#DDDDDD" grey_no.8pc.png
convert grey_no.8pc.png -alpha copy -channel alpha -negate +channel result.grey_no.png
so the plains avoid an useless #DDDDDD, opacity:50% overlay.
See also:
ImageMagick options: http://www.imagemagick.org/script/command-line-options.php

Creating a semi-transparent PNG with ImageMagick/convert

I have PNG files that I would like to convert the whole image to be semi-transparent.
The image will be referenced in a KML file as an icon overlay for use in Google Earth/Maps.
The following examples have been suggested to me using the ImageMagick convert command, however neither seems to work.
The first example results in an error,
$ /usr/local/bin/convert 121112142121.png -channel Alpha -evaluate Set 50% 2121112142121.png
convert: no decode delegate for this image format '121112142121.png' # error/constitute.c/ReadImage/550.
convert: option requires an argument '-evaluate' # error/convert.c/ConvertImageCommand/1400.
the second fails to make any observable change to the image:
convert filename.png -fill '#00000080' -draw 'matte 100,100 reset' output.png
While I can find many examples on the web for creating fully transparent images from specific colors or alpha masks, I cannot seem to find any that work for creating semi-transparent images or changing the opacity. I would like to find a way to do this with the convert command, or with ImageMagick Perl.
(ImageMagick version is 6.8.0-4)
Resolution has been found:
convert input.png -alpha set -channel A -evaluate set 50% output.png
The above command makes the entire image (all colors) semi-transparent.
Another problem I had was that the latest version of ImageMagick was compiled from source without all the most recent image libraries installed (in particular libpng). Pay close attention to the output of configure to ensure libpng is found if compiling from source. It also appears that versions of ImageMagick earlier than 6.6 and/or older versions of libpng may not support transparent png generation.
convert input.png -alpha set -channel A -evaluate Divide 2 output.png
Thank you #caw for pointing this out in your comment. This helps in the case where the image already has some transparent pixels. -evaluate set 50% ruins the already-transparent pixels that had alpha 0, setting their alpha to 50%, which is often not desirable.
-evaluate Divide 2 preserves the alpha, since fully transparent pixels are alpha 0, and 0 divided by 2 is still 0.
To followup on Tony Bogdanov's comment. If you already have fully transparent pixels you may want to use the following strategy:
Convert the transparent pixels into a mask
convert -alpha extract input.png mask.png
Perform the command listed in the answer above:
convert input.png -alpha on -channel a -evaluate set 65% output.png
Create a blank canvas the same size as the original image. Example:
convert -size 700x800 xc:none blankcanvas.png
Composite the semitransparent image and the blank canvas together using the transparent pixel mask
composite output.png blankcanvas.png mask.png -compose Src final_output.png

How do I batch resize all images to sqares with Image Magick?

OK. I have a horizontal image, let's say 2000 x 300. I want to overwrite that image and create a new one that is 2000 x 2000 with the old image center in the middle and vertically, with white space around it. I also want to do this in batch with many different images of different sizes. Thanks in advance.
Use -resize to resize the image to 2000x2000, -background to set the background to white, -gravity to center the original image on the canvas, and -extent to resize the canvas itself (to cause the image to sit in the centre if it wasn't square originally).
convert [imagename] -resize 2000x2000 -background white \
-gravity center -extent 2000x2000 [imagename]

Resources