How to underlay circle with mogrify in one command - imagemagick

I wrote this script and it's working fine, but I would like to do all of it in one step on the fly, without the extra temp image.
explanation: i have a lot of broken image files and i want to draw a circle underneath each image. for this i have to create a temporary image circle.png and then use "image DstOver" to place it below each of the images:
convert -size 200x200 xc:transparent -fill red -draw 'translate 100,100 circle 0,0 100,0' circle.png
mogrify -draw "image DstOver 0,0 0,0 'circle.png'" images/*.png
Something along the lines of:
mogrify -fill red -draw "DstOver translate 100,100 circle 0,0 100,0" images/*.png
But this is always giving me an error, no matter where i place the DstOver:
mogrify: non-conforming drawing primitive definition `DstOver' # error/draw.c/DrawImage/3169.

Composition operators like "DstOver" are only used with the "image" primitive of "-draw". Just omit it. See the "-draw" entry in the ImageMagick commandline documentation.
You can have multiple "-draw " options, some drawing figures such as "circle ..." and others such as "image DstOver ...".

I am not sure what you are trying to do, but in general, mogrify will have trouble doing anything with multi-image operators or stack operators. The only exception I know of is the -draw image operator, so you need to create your image up-front and then use that:
# Blue rectangle with transparent centre
convert -size 200x200 xc:none -bordercolor blue -border 50 start.png
# Your circle
convert -size 200x200 xc:white -fill red -draw 'translate 100,100 circle 0,0 100,0' circle.png
# Now underlay
mogrify -draw "image DstOver 0,0 0,0 'circle.png'" start.png

Related

How to position the -draw in imagemagick?

I am using imagemagick to draw a border on the top of an image.
THIS IS MY CODE:
convert source.jpg -stroke red -strokewidth 2 -fill transparent -draw \"roundrectangle 10,10 628,151 10,10\" source.jpg
This works fine but i need to be able to position the -draw where i want.
I tried to position the border like using -geometry like so:
convert source.jpg -stroke red -strokewidth 2 -fill transparent -geometry +5+15 -draw \"roundrectangle 10,10 628,151 10,10\" source.jpg
But this does not position it where i want. I also tried using -gravity and that doesn't work either!
Could someone please advise on this?
Thanks in advance.
Bonzo is correct, you cannot use -geometry with -draw.
In ImageMagick with -draw you can also translate to where you want the center to be and then specify +- distances to the corners from the center placement.
Suppose you have a 100x100 size box you want draw and you want it centered at 250,250, then
convert input.jpg -stroke red -strokewidth 2 -fill transparent -draw "translate 250,250 roundrectangle -50,-50 50,50 10,10" output.png
That makes it easier to draw the same size boxes at different locations.
see
http://www.imagemagick.org/Usage/draw/
http://www.imagemagick.org/script/magick-vector-graphics.php

Imagemagick: distorting images on top of background?

I've got one background image: sky.jpg
And two transparent PNG images: gradient.png and tree.png
Now I want to draw the two images on the background with a perspective distortion, like this:
The destination coordinates of the two images are, in clockwise order (starting top left) :
gradient: 62,129 421,218 383,458 147,548
tree: 445,100 765,47 698,368 529,396
I cannot figure out how to start with one image (in this case the sky background) and then take another image and draw that with perspective distortion to specific destination coords within the background. Also doing this with more than one image at a time, within one convert command, troubles me.
For example, when I start with just one image (the gradient) and try this:
convert sky.jpg \( gradient.png -alpha set -virtual-pixel transparent \
+distort Perspective "0,0 62,129 255,0 421,218 255,255 383,458 0,255 147,548" \) \
-compose src-over -composite result.jpg
It gets correctly warped (so the coordinates are relatively correct) but it's drawn in the top left corner, not at the coordinates I specify.
Also I'm a bit unsure if my usage of -compose and -composite is correct (I took this from various IM manual examples).
One other thing that is unclear to me: in case of the 256x256 image, should I use 255,0 and 255,255 and 0,255 as the corner coordinates, or 256,0 and 256,256 and 0,256 ?
Any IM experts who can shed light on these issues?
Add a -geometry just before the -composite like this:
convert -size 800x600 xc:black \( -size 300x200 xc:red \) -geometry +90+10 -composite result.png

Cleaning the left side of an image

Using imagemagick, I want to clean the left side of an image, i.e. make white without cropping. For example cleaning the left-most vertical strip of 25 pixels wide. I figured out how to crop to a given geometry, but I couldn't figure out how to clean without cropping.
Here is my start image, made like this:
convert -size 256x256 gradient:cyan-yellow image.png
Method 1
One way to do it would be to use -fx and set all pixels where the x-coordinate is less than 25 to 1.0 (i.e. white) and leave all other pixels as they are:
convert image.png -fx "i<25?1:u" result.png
Method 2
Another, faster way to do it might be to clone the original image, and scale it down to 25 pixels wide, fill it with white and composite that over the original image:
convert image.png \
\( +clone -scale 25x! -fill white -colorize 100 \) \
-composite result.png
The result is the same.
Method 3
A third way to do it might be to crop the image 25 pixels in from the left side, then splice 25 white pixels back on the left side:
convert image.png -crop +25+0 -background white -gravity west -splice 25x result.png
Method 4
Bit of a kludge, but nearer to what you asked. Here, I guess that your image height doesn't exceed 10,000 pixels and draw a rectangle:
convert image.png -fill white -draw "rectangle 0,0 24,9999" result.png
I guess the proper way to do this is to get the height first then use it:
#!/bin/bash
h=$(convert image.png -format "%[fx:h-1]" info:)
convert image.png -fill white -draw "rectangle 0,0 24,$h" result.png

Replace only background color of PNG

Here is my code:
#! /usr/bin/env sh
# Generate test image.
convert -size 100x60 xc:blue -fill blue -stroke black -draw "circle 50,30 55,55" in.png
# Make background transparent.
convert in.png -fill none -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flop out.png
# Replace transparent background with green.
mogrify -background green -flatten out.png
# The wrong way.
convert in.png -transparent blue oops.png
mogrify -background green -flatten oops.png
It is based on this snippet: https://snippets.aktagon.com/snippets/558-how-to-remove-a-background-with-imagemagick
Starting with this:
I want to get this:
Not this:
Can I achieve this with a single convert command instead of a convert followed by a mogrify?
I am using ImageMagick 6.8.9-9.
Essentially, you are seeking a "floodfill", like this:
convert in.png -fill green -draw 'color 0,0 floodfill' result.png
That will look at the top-left pixel (0,0) and fill all similarly coloured pixels which are connected to it with green. If your background has slight variations in it, e.g. it's a JPEG, add some fuzz factor
convert in.jpg -fuzz 25% ...
Note that if your circle had touched the top and bottom edges, it would prevent the fill from flooding around to the right side of the diagram. So, let's say you had created your circle like this:
convert -size 100x60 xc:blue -fill blue -stroke black -draw "circle 50,30 50,0" in.png
And then you run the above command, you will get:
If that happens, you can add a single pixel wide border all the way around for the colour to "flow" through first, then flood-fill, and finally remove it later:
convert in.png -bordercolor blue -border 1 -fill green -draw 'color 0,0 floodfill' -shave 1x1 result.png

How to create transparent image for rubber stamp in ImageMagick

I have to create an image, which is an overlay texture made up of a white image with some transparency to give it the appearance of a rubber stamp.
For reference see the image "stamp_overlay.png" in the video http://railscasts.com/episodes/374-image-manipulation .
This is what i did:
convert -size 70x70 canvas:white stamp_overlay1.png
and then
convert stamp_overlay1.png -transparent white stamp_overlay1.png
But how do I make it like the image?
I am pretty much new to ImageMagick. Any help is highly solicited.
Updated Answer
Ok, I got that wrong! You want to create a stamp overlay, not overlay a stamp overlay. You can do that like this:
# Create white square, draw a black rectangle, then make black pixels transparent
convert -size 300x300 xc:white \
-fill black -draw "rectangle 20,100 200,280" \
-transparent black out.png
Original Answer
I find your question very hard to understand, but I think I know what you want. First, let's create a solid red image
convert -size 70x70 xc:red red.png
then let's composite the stamp_overlay.png image on top
convert red.png stamp_overlay.png -composite out.png
which gives this
but now you want to make the white areas transparent, so you need to do this:
convert red.png stamp_overlay.png -composite -transparent white out.png
and that still looks the same on this white background, but it isn't :-)

Resources