Making half an image transparent from the command line - ffmpeg

I don't really know where to start with this one. I'm trying to do something that I thought would be relatively simple to accomplish with imagemagick, but I don't know the exact command to start with. I need to draw a line through an image, and then make everything above the line transparent in the image, and make everything below the line, the orignal image. What would be the best way to accomplish this using imagemagick?
So what I've come up with for now is to crop the image, and then resize it to the original size, but with a transparent background. The command I use is this, but it always comes out black. I'm not understanding why.
convert -background none -gravity south out.png -resize 400x200 -extent 400x400 result.png
Thanks for all of the help!

Here's a fairly easy way to do it. First, enable an alpha channel in case your image doesn't have one, then select the alpha channel for modification by the following -fx command. In there, if the current j is greater than half the height of the image, make the alpha layer opaque, else transparent. Easier than it sounds!
So, using this start image:
convert bean.jpg -alpha on -channel A -fx "j>h/2?1:0" result.gif
Or, the other way:
convert bean.jpg -alpha on -channel A -fx "j<h/2?1:0" result.gif
Or the other, other way:
convert bean.jpg -alpha on -channel A -fx "i<w/2?1:0" result.gif
Or, if you are feeling particularly silly on a Friday morning...
convert bean.jpg -alpha on -channel A -fx "hypot(i,j)/400-0.8" result.gif

Related

How to change colors of an image using RGBA and more channels independently of their color

Since it is hard to me to explain what I'm trying to do, I'm gonna show you this page to show you what I'm trying to reproduce and understand:
https://optifine.net/showCape?colTop=FF0000&colBottom=00FF00&colText=0000FF&colShadow=FD0000
which outputs this:
and this one https://optifine.net/showCape?colTop=FF00FF&colBottom=0034EE&colText=000000&colShadow=FF00E2 outputs this:
You can modify the hexadecimal colors, basically I'm trying to reproduce something like that. Basically you modify the colors and it gives you an image at the end with the colors you used.
I've tried to make the "possible" template that could be used on the page on Photoshop which can be downloaded here, it is a .psd file, that's because of the Alpha Channel, which I'm not even sure if done correctly. But based of these RGBA channels it should be possible to change the color. Can be downloaded here: https://workupload.com/file/4xYkgQMk
So what I know so far is that there is a template with RGBA channel. Each channel is indepedent so it apperantly doesn't matter if it's RGBA and at the end R channel is used to turn into another color other than red, where I'm not sure about that.
I've asked the developer he told me that these channels get interpolated with the real color after that, probably the one you choose.
Basically what is happening at showCape? and its URL parameters is that, lets assume colTop got assigned to the red channel then when you put a color in colTop it will get a fixed color that it will encode or something.
So the template has 4 Channels RGBA that can be made in Photoshop, the white color 255,255,255 means basically full and the black 0,0,0 means complete black. Like that you can setup brightness scales for the template.
I just don't know how to modify the channels and I don't understand how to use the Alpha channels properly or set them up.
I'm also not sure in which programming languages it is possible to peform and if you can test the template directly in something like Photoshop. Is it possible to do it in JavaScript or something to easy setup and if not on what then, to test it fast?
Ok, so here's how you can generate that sort of thing with ImageMagick, which is included in most Linux distros and is available for macOS and Windows. Note that there are Python, PHP, node.js and other bindings available.
First, generate a red rectangle:
magick -size 200x150 xc:red red.png
Now see how to do the same thing with hex codes:
magick -size 200x150 xc:"#ff0000" red.png
Now, draw a red rectangle with a blue one on top:
magick -size 200x150 xc:red -fill blue -draw "rectangle 10,10 80,140" redandblue.png
Now make the blue transparent:
magick -size 200x150 xc:red -fill blue -draw "rectangle 10,10 80,140" -transparent blue redandtrans.png
Now make a gradient from lime green to magenta:
magick -size 200x150 gradient:lime-magenta gradient.png
Now overlay the red rectangle with transparent window onto the gradient:
magick gradient.png redandtrans.png -composite overlay.png
Now add text:
magick overlay.png -fill "#0000ff" -pointsize 16 -draw "text 90,40 'Coloured text'" result.png
And now do the whole thing again, in one go:
magick -size 200x150 gradient:lime-magenta \
\( xc:red -fill blue -draw "rectangle 10,10 80,140" -transparent blue \) \
-composite \
-fill "#0000ff" -pointsize 16 -draw "text 90,40 'Coloured text'" result.png
Now you have provided a template, I can separate out the channels with ImageMagick like this and append them side-by-side with Red channel on the left, then Green, then Blue then the alpha/transparency channel on the right. I also added a red box around each one so you can see the extent on StackOverflow's white background.
magick template.png -separate -scale 100x +append channels.png
Keywords: ImageMagick, absolute basics, tutorial, transparency, compose, overlay, command line, image processing.
this sounds like indexed colors / palette effect from the VGA days (like plasma, water and fire) Where you change the palette (in a specific way) and image changes with it.
The idea is that Your image/sprite does not contain RGB colors directly but color indexes from palette instead. Where part of the palette for your image/sprite contains a color gradient so gradients on image are also gradients on index (neighboring shades have also neighboring indexes). Many old pixelart sprites and images from the old days are done this way (sorted palette).
Now you can simply chose few colors in that part of palette and interpolate the rest of the gradient (linearly or better).
To mimic this your need:
have a indexed color pixel art with sorted palette
For example You can convert your image into BMP or GIF with palette and sort the colors.
detect the part of palette with color gradient
change/update the gradient
re-render or recolor image.

Merging two transparent gifs as layers

I've found this page on the imagemagick forum: Merging 2 gifs, one is animated and transparent which links to the imagemagick docs on Animation Modification; specifically, the example here:
convert canvas_prev.gif -coalesce \
-gravity NorthEast -draw 'image over 5,5 0,0 "rose:"' \
-layers Optimize draw_over.gif
Here's my attempt. I have these two gifs (i might be feeling a little morbid). The white in the first is actually transparent.
when I run
convert eyes.gif -coalesce -draw ' image over 0,0 0,0 "trump.gif" ' combine.gif
I get this:
which is not the complete effect I desire. The trump animation is no longer playing at all.
I want to see something more like this (this is created in Phaser JS, but this gives me no way to export the result as a new image other than manually recording a screencast):
One way to do it, yet not sure if it's the best, is as follows:
convert both animations into sprite-sheets of same dimensions
paste one image over the another
cut result into smaller frames and convert it into animated GIF
The commands should look like this:
montage -background none t.gif -tile x1# -geometry +0+0 tt.png
montage e.gif[0-16,0-9] -tile x1# -geometry +0+0 ee.png
magick convert -delay 10 -loop 0 ee.png tt.png -coalesce -flatten \
-crop 150x150 +repage output.gif
Tricky part is the second line with eyes image. It has only 17 frames while skull has 27. So the sprite sheet size must be adjusted.
I works, however I am not quite happy with this as solution requires manual entering of some parameters (frames selection and output image dimensions).

Crop part of an image and paste it into another with Imagemagick does not work

I have two images and I want to crop a part of the first one, and paste it into the other one in a specific place using imagemagick. Also the crop part would be gray
I want this: http://imgur.com/3PomJ9k
But I got this: http://imgur.com/5XmhytN
I have tried:
convert source.jpg ( +clone -crop 240x270+595+140 -resize 112x146 -type Grayscale ) -geometry +10+200 -composite destiny.jpg
but as you see, it does not works as expected.
Although the crop part is cloned as gray, the whole first image is cloned too, but I need to keep in background the second image in color and the crop part in gray in front of it.
Some ideas?
From our discussion in chat, I think you need something along these lines - you may need to fiddle with the actual numbers but hopefully the concept is clear:
convert other.jpg \( source.jpg -crop 240x270+595+140 -colorspace gray -resize 116x150! \) -geometry +4+108 -composite result.png

Image background removal algorithms

I've lots of images with a white background and I need to remove it (make it transparent). I've already tried imagemagick convert and for some image that worked quite well, but still cannot be used on dark background.
I'm kind a noob in this branch of programming... Someone know some good algorithms to remove the background?
Plus, would be great if it could correctly process an image like this:
http://dev.addvert.it/cache/24eeab00e5987452d09fbeec0c7678d6_w472_h472_sc.jpg
The problem with convert diff is it erase a lot of the image, while the border algo doesn't touch the central part.
I totally understand how hard is for a calculator to do something like this and I'm pretty amazed with the results of convert, but if there's the chance of a better solution, why not ask? :D
You are not constrained to converting white pixels to transparent. You could maybe take the colour of the top-left corner pixel and make all pixels that colour transparent and it will work for black backgrounds too:
As suggested by #emcconville, the first of the following two options is more succinct:
convert towel.jpg -fill none -fuzz 2% -draw 'matte 0,0 replace' output.png
or, per my original,
convert towel.jpg -alpha on -fill none -fuzz 5% -draw 'color 0,0 replace' output.png
You may find this more readable...
convert towel.jpg -fuzz 5% \
-transparent $(convert towel.jpg -format "%[pixel:p{0,0}]" info:) \
output.png
In the second line, $(convert ...) just gets the colour of the top-left pixel and feeds that into the middle of the outer convert command as the colour to make transparent - but it is only doing the same thing as the first version.

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

Resources