Creating map flags with ImageMagick - imagemagick

I have a requirement where I need to create a map flag based on a users avatar:
The users avatar can be any size and aspect ratio. The problem I'm having is getting the image to compose correctly, allowing me to get the transparency around the marker in order to support the tick at the bottom.
How can I do this?

It transpires that this is fairly straightforward with the correct approach.
First you need TWO overlay images. One of these is the size of the result image and is transparent everywhere for except where you want the avatar to show. The second is a simple transparent PNG of the surround on the image.
Then, this seems to work:
composite avatar.jpg -thumbnail 61x68^ -gravity center map_marker.png mask.png output.png

Related

Overlay images on top of another without any fading

I have a series of images. What I wish to do is to literally superimpose these images on top of each other without any other manipulation. The actual example is below
This is the first image
This is the second image
There are five samples like this, each with a tooltip on top of the bar. Essentially, the final image needs have the bars in their full green color with the tool tips showing up on each bar.
The closest I've been able to come up with was using GraphicsMagick which unfortunately does the following with its "average" command.
Any other pointers on how to accomplish this?
Finally: While at this time I just want to get the job done, long term, I will ideally want to use some commandline tool like GraphicsMagick without the need to have a desktop software installed.
You could try using the darken blend mode in ImageMagick:
convert 1.png 2.png 3.png -compose darken -composite result.png
You'd have to tidy up your cropping first though!

How can I convert image background pattern?

On this similar thread they have been proposed solutions to convert the background color of some image to transparent.
But sometimes the background is a simple pattern, like in this case:
Note the square background pattern.
When processing images, the background does often need to be removed or changed, but firstly you need to detect it (i.e: change its color, or making it transparent). For example, on the above image example, I would like to obtain:
How can I detect/change a specified pattern inside an image?
GUI solutions accepted.
Open source solutions preferred (free at least required).
The simplest solution will be preferred (I would like to avoid installing some hundreds of MB program).
Note: I was thinking about posting this question at Photography
StackExchange site, but I would rather say the mind of a programmer (I
could need to edit dozens of such images) is more close to what I
want. I am not an artist.
This is not a fully developed answer, but it does go some way towards thinking about a method - maybe someone else would like to develop it...
If, as you say, your pattern is specified, you know what it is - good, aren't I? So, you could look for the "Minimum Repeating Unit" of your pattern in your image. In your case it is a 16x16 grid like this:
Now you can search for that pattern in your image. I am using ImageMagick at the command-line, but you can use other tools if you prefer. ImageMagick is installed on most Linux distros and is available for OSX and Windows for free. So, I search for that grid in your globe image and ImageMagick gives me an output image showing white dots at the top-left corner of every location where the two images match:
compare -metric ae -dissimilarity-threshold 1.0 -compose src -subimage-search globe.gif grid.png res.png
That gets me this in file res-1.png
Now the white dots are where the 16x16 "Minimum Repeating Unit" is found in the image, but at the top-left corner so I shift them right and down by 8 pixels to the centre of the matching area, then I create a new output image where each pixel is the maximum pixel of the 16x16 grid in which it existed before:
convert res-1.png -roll +8+8 -statistic maximum 16x16 z.png
I can now invert that mask and then use it to set the opacity of the original image, thereby blanking areas that matched the "Minimum Repeating Unit":
convert globe.gif \( z.png -negate \) -compose copy_opacity -composite q.png
No, it's not perfect, but it is an idea for an approach that could be refined...

Determine image content using ImageMagick

Is it possible to easily determine whether or not an image has any actual content beyond just transparency using ImageMagick?
The back story is that I am cropping a image with unknown content, but a known area map (think game skins). The area I'm cropping for part of this will either contain something or it won't. If it contains something, I need to do something with it. If it doesn't, I need to scrap it and move on. Thanks!
I have a couple of ideas you could try, but I have not seen your images, so you will need to test them out.
Firstly, you could count the number of colours in the image, like this:
identify -format "%[k]" input.png
If there is only transparency, the number of colours will be 1, if there are other colours in the image, it will be greater than one. This cannot differentiate between a pure single colour image and a purely transparent image though. That may, or may not, be a problem. I don't speak Ruby, but I believe you access k like this in Ruby:
image["%[k]"]
Secondly, you could calculate the mean pixel colour of the image, like this:
identify -format "%[mean]" input.png
That will be zero in the case of a fully transparent image. However it will also be zero for a fully black image, which may or may not be a problem.
Both of these parameters can also be seen using
identify -verbose input.png
If you wanted a more robust test, for example to differentiate between a fully trasnparent image and a fully black image, you could use one of the tests above, and combine it with getting the value of a single pixel at top left of image to rule out the odd edge cases above, like this:
convert black.png -format "%[pixel:p{0,0}]" info:
black
convert transparent.png -format "%[pixel:p{0,0}]" info:
none
Another option would be to calculate the histogram, like this and see the colours:
convert input.png -format %c histogram:info:-
I ended up using the following code:
def transparent? image
solid_color = image['%[mean]'].to_i == 0 # transparent or solid color
not_opaque = !image['%[opaque]'].to_bool # no transparency remaining
solid_color && not_opaque
end
image['%[mean]'] gives me 0 for as single color or full on transparency, as Mark Setchell mentioned. This got me half way. In order to determine if the single color is transparent or not, I used image['%[opaque]']. If this was "true", then it is a color. Otherwise, transparent. The .to_bool method simply takes the resulting boolean string and converts it into an actual boolean.
I hope this helps someone else! And thanks again for the significant start toward figuring this out, Mark!

Resize lots of images (with different proportions) so each image fit in a predefined rectangle

I have about 60 images uploaded to my site. I'd like to resize them all so they fit in a 150px × 100px box. No cropping, just scaling, but it should preserve the original proportions.
I'd prefer a simple solution using, say the ImageMagick convert command. A solution for a single arbitrary image is perfectly fine. (I know how to loop or use find in bash.)
The images are of different types (eps, jpg, ps etc) so a solution that at the same time rasterizes the image would be awesome.
Ok, it seems it was easier than I expected:
convert image.eps -scale "150x100>" file_resized.jpg
did the trick. Reference page.

Facebook-like resizing of images using ImageMagick

I would like to resize (downscale) some images the way that Facebook does it. ImageMagick, but hey, I'm open for suggestions :)
I believe Facebook is doing this:
Say you have a max width x height of 250x200, Facebook is optimizing the use of this. Tries to use as much of the 250x200 as possible. If for instance you scale down an image and get 220x200, then they cut from the top and the bottom of the image until they use as much as possible of the 250x200 frame. Actually I think they take more from the bottom, than the top (around 1:2.5), which I believe is because most pictures have the head at the top and Facebook realizes this.
Is there any name for this kind of resizing algorithm? And is there any way to have ImageMagick do this?
Thanks in advance!
Edit
It actually appears that Facebook might not be doing this "smart" resizing technique after all. They just resize where they have a minwidth/minheight. Then when they show the image in their album, they cut from the top/bottom or left/right to use as much as possible for the frame (that is how I perceive it at least).
-Tobias
You can use ImageMagick to get the dimensions of an image, scale then crop it. As to whether you are accurately describing the algorithm Facebook uses, I don't know.
I think the following link addresses the problem you're trying to tackle:
http://www.imagemagick.org/Usage/resize/#space_fill
The example they give at the very end is...
convert logo: \
-resize 160x -resize 'x160<' -resize 50% \
-gravity center -crop 80x80+0+0 +repage space_fill_2.jpg
That command resizes an image to be 160 pixels wide, resizes it to be 160 pixels tall, takes the larger of the two resized images and shrinks it by half, and crop it to 80x80.
The following may be of interest to you:
http://www.google.com/search?q=image+entroy+cropping
I've read several documents about using image entropy to choose what part of the image to crop.
Another related link -
Django, sorl-thumbnail crop picture head
edits: added related links, specified an example command for doing a similar task with link to source of example.

Resources