I'm trying to get rid of the backgrounds on a bunch of images using ImageMagick. The ImageMagick website documents a variety of techniques for doing this. They have also conveniently written a shell script, bg_removal.sh, which contains their preferred combination of these methods.
I'm getting an error in the script though. When you call the script you give a couple of fuzziness parameters, plus the specific background color if you know it. Passing an explicit background color, like RGB(37,188,111), results in this fragment of the final command:
( +clone -sparse-color voronoi 0,0,RGB(37,188,111) )
..and it works fine. If you don't pass a bgcolor, though, the expression looks like this:
( +clone -sparse-color voronoi 0,0,%[fx:p{0,0}] )
and it fails with a message about having wrong number of arguments for -sparse-color. I can change it to a different function, and it works:
( +clone -sparse-color voronoi 0,0,%[pixel:p{0,0}] )
I don't know enough about ImageMagick to know what the fx function is doing and whether pixel is a acceptable replacement. I think it's looking to grab the color at coordinates 0,0 to guess the background color, but not exactly sure. Is the syntax 0,0,%[fx:p{0,0}] correct?
-fx is an operator that lets you use a mathematical or IM-specific expression to apply to pixels: -fx 'expression'.
It was only in ImageMagick version 6.2.10 and later that it was introduced you could use an escape sequence like '%[fx:...]' within image properties escaped strings, for the special occasions when they are enclosed in braces. So both these methods should work after v6.2.10:
%[fx:expression]
%[pixel:expression]
so as to your question: yes, 'pixel IS an acceptable replacement' for fx.
And the expression p{0,0} means: 'return the color values for the upper left pixel in the current image'.
So check your version of ImageMagick:
convert -version
As for why the complete expression fails for you because of 'wrong number of arguments' for -sparse-color:
Check if voronoi is amongst the supported methods for -sparse-color:
convert -list sparse-color
(I assume it is -- otherwise you'd probably see a different error message.)
The general syntax for -sparse-color is documented as:
-sparse-color method 'x,y color ...'
This suggests you should maybe use additional quotes to make sparse-color to recognise the correct number of arguments. How about
( +clone -sparse-color voronoi '0,0 %[fx:p{0,0}]' )
??
I ran into the an issue where my fx expressions weren't working either despite using v7+. I changed my command from convert to magick and then it started working. Hope this helps someone else from beating their head against the wall.
Related
I am using 'ImageMagick' command identify to calculate the area of images in pixels:
$ identify -format "%[fx:w*h]\n" example-small.jpg
699392
$ identify -format "%[fx:w*h]\n" example-large.jpg
1.80497e+07
In the second case, the output is in the scientific format: 1.80497e+07.
This is the case if the area is larger than some threshold (>= 1000000).
The option can be used with the 'ImageMagick' commands convert, identify and mogrify.
Is it possible to make the `%[fx: ... ]' format option use a specific number format in itself?
Is it possible to make the `%[fx: ... ]' format option use a specific number format in itself?
Yes and No. When parsing %[fx: ... ] format, ImageMagick stores the result as double data type. This value will be passed to stdlib vsnprintf() method with a %.*g format. The output would result in the scientific notation for "extreme" numbers.
For the "yes" part. Since you're working with two unsigned integers (w*h), you may be able to spoof the behavior of vnsprintf() by setting the precision operator in ImageMagick.
identify -precision 32 -format "%[fx:w*h]\n" example-large.jpg
However this is not a true "fix". It may not always work between systems, and adjusting the overall precision might have unforeseen side effects.
For the "no" part. It might be more practical & reliable to use an external utility to format the results to a format you can control.
printf "%.f" $(identify -format "%[fx:w*h]" example-large.jpg)
True that it is inconvenient, but a quick search for "[bash] scientific notation" offers many alternative ways as this a common task.
I'm talking about this metadata:
I know that this command:
magick mogrify -strip image.jpg
can clear all the info. But I don't know is it possible to write some metadata with ImageMagic?
It seems like magick identify -set can probably do what you have in mind - I just need a minute to test it myself, here's the corresponding part from the man page of identify:
-set attribute value set an image attribute
I'll edit my findings into this answer in a few mins :)
Edit:
This seems to work:
convert input.png -set new_attribute "attribute value" output.png
At least it works according to imagemagick's identify -verbose, I can't test if these properties show up on windows too.
It should also be simple to overwrite other metadata, simply by first finding the actual key with identify -verbose, I guess.
For more information, here's the official documentation:
https://www.imagemagick.org/script/command-line-options.php#set
I am trying to generate an image that is 1px wide and 100px tall of a gradient, which goes from white to #3532CB, and then save it in my directory as topgrad1.png
After changing the directory to the image folder in cmd.exe, I entered:
convert -size 1x100 gradient:white-#3532cb topgrad1.png
When entered, it gives the result of: Invalid Parameter - 1x100
How can I solve this?
That error indicates you're accessing a Windows command "convert.exe", probably in the "C:\Windows\System32\directory". You need to make sure your ImageMagick's "convert" command is in your PATH, or call your IM "convert" with the full path. Or if you're using IM7, you should be using "magick" instead of "convert" anyway, and still make sure it's in your PATH.
In Imagemagick, at least on Unix systems, you need to quote your colors, especially if using hex or rgb(...) colors. So
convert -size 1x100 gradient:"white-#3532cb" topgrad1.png
I'm using ImageMagick programmatically to apply some user-defined transformations to an image. The script I'm using spawns a new process and runs ImageMagick with arguments similar to:
convert /tmp/source -resize 100x /tmp/transformed
And then it reads the transformed image back from /tmp/transformed. I'd like to add the option to convert the image to another image format, but from looking at the IM docs for a while, the only way I can see of doing that is to append the output destination with .<ext>, like this:
convert /tmp/source -resize 100x /tmp/transformed.png
Is there another way? The easiest way for me to do this with the pre-existing script is to supply an argument, but I can't find it. Something like:
convert /tmp/source -resize 100x -format png /tmp/transformed
Is this possible? Or am I stuck with having to append the extension to the output destination?
I'm not sure what your aversion is for appending a suffix, but another alternative to Fred's excellent suggestions is to use a "format specifier" prefix, which would leave your base filename unchanged - if that is what you are trying to achieve.
convert Image -resize 100x PNG:/tmp/transformed/Image
Substitute PNG: with GIF:, JPEG: etc to suit.
-format png is used in mogrify and not convert as I understand it. You need to specify the suffix for the input and the desired suffix for the output in the input and output filenames.
convert /tmp/source.suffx -resize 100x /tmp/transformed.png
assuming png is the desired output format.
Perhaps I misunderstand what you want. If so, please clarify. Are the suffixes in the source and transformed variables? If so, you can use IM to separate the source filename from its suffix using %t and %e in string formats. But for the output, you would have to parse that using your file system. See http://www.imagemagick.org/script/escape.php
Alternately, use mogrify which supports -format png
mogrify -format png -resize 100x *.suffix
That will take every file in the input directory with suffix .suffix and convert that to png. However, I would suggest you create a new directory to hold all your output images, since as it is, it will overwrite your input files. You would then need to add -path path2/newdirectory to the command above. see http://www.imagemagick.org/Usage/basics/#mogrify
What are the actual filenames associates with source and destination? Are these just variable for the real filenames?
I'm generating text images using imagemagick by passing user inputs via command. I'm concerned that a user could enter something malicious.
# regex pattern
[^\s\w\.&!?"]
# image generation code, in Ruby
"convert -quality 100 -background black -fill red -font Times-Bold -size x50 label:'#{#line1}' output.jpg"
No it's a not :)
have a look at this chapter of programming ruby for more security options: http://ruby-doc.org/docs/ProgrammingRuby/html/taint.html