How to find the path to ImageMagick config files - imagemagick

I'm attempting to load a ICM color profile file that is stored with the main ImageMagick config files (colors.xml, etc.) using RMagick. I want to make sure that I always provide the correct path to the file across deployments using slightly different ImageMagick setups and/or versions. Is there a way to find to get this location from ImageMagick (something like Magick-config)?
This is what I have now, but it seems brittle:
MAGICK_PREFIX = `Magick-config --prefix`.strip!
MAGICK_VERSION = `Magick-config --version`.strip!.split(/\s/).first
RGB_COLOR_PROFILE = "#{MAGICK_PREFIX}/share/ImageMagick-#{MAGICK_VERSION}/config/sRGB.icm"

I have no perfect anwser, but you can
$ convert -list color | grep Path: | awk '{print $2}'
/usr/lib/ImageMagick-6.3.7/config/colors.xml
If response is [built-in] try another -list value.

try these and parse stdout / stderr. the first one should work always
convert -debug configure rose: info:
convert -list configure

Related

ImageMagick converts out of order

I have 100 images named img0.jpg to img99.jpg to be converted to a pdf file. problem is
convert img*.jpg out.pdf
adds pages in the order of 1,11,2,22,etc. how is order defined in imagemagick?
Either number your pages with zero-padded numbers like this so ImageMagick takes them in order:
img000.jpg
img001.jpg
img002.jpg
...
img098.jpg
Then your original command should work.
Or, have bash enumerate the files in order and feed the names into ImageMagick like this:
magick img{0..99}.jpg result.pdf
Or:
for file in img{0..99}.jpg; do echo $file; done | magick #- result.pdf
Or rename your files as per the first example above, but using Perl rename:
rename --dry-run 's/\D//g; $_=sprintf("f-%05d.jpg",$_)' f*jpg
Sample Output
'f0.jpg' would be renamed to 'f-00000.jpg'
'f1.jpg' would be renamed to 'f-00001.jpg'
'f10.jpg' would be renamed to 'f-00010.jpg'
'f11.jpg' would be renamed to 'f-00011.jpg'
'f12.jpg' would be renamed to 'f-00012.jpg'
You may have ls -v available to you, in which case you can try:
magick $(ls -v img*jpg) result.pdf

Convert multiple images with ImageMagick

I want to convert all my PNGs to PNG using ImageMagick (I need this because AndroidStudio has some issue with the original PNGs. ImageMagick is able to fix this issue by re-exporting the PNG.)
If I do: convert a.png a.png it works.
But how do I do this for many files (including files from sub directories)?
For Windows users -- you can try this in a cmd.exe window:
for %i in (*.png *\*.png *\*\*.png *\*\*\*.png) do (
convert.exe %i %~pni---repaired.png
)
This will loop through the current dir's PNGs as well as the ones in the sub directories 3 levels deep.
You have to make sure that your convert.exe really is the one from ImageMagick -- set up your environment variable %PATH% accordingly. Otherwise you may run into an error when the command wants to use the identically named disk format conversion convert.exe command.
If unsure, use the full path to the IM convert.exe, e.g.:
D:\programs\imagemagick-install-dir\convert.exe %i %~pni---repaired.png
Also, remember: If you put the above command into a *.bat file, you have to double up each occurrence of %. So %i from the direct command becomes %%i in the batch file!
If you are on Vista/Windows7/2008/8 (or on Windows XP with the Resource Kit installed) you'll have the ForFiles.exe available, which can be used to loop through files:
forfiles.exe ^
/p <path> ^
/m *.png ^
/s ^
/C "convert.exe #file #fname---repaired.png"
Try this command. Start it from the top-most directory from where you want to convert all images:
find . -name "*.png" \
| while read image; do \
convert "${image}" "${image/.png/---repaired.png}
done
Caveats: Should you have PNG files with the suffix .PNG or .pNg or similar, the command will not work for these. For such cases, the command needs some modifications...

Can't install GhostScript in ImageMagick

Download latest version of ImageMagick. Unpacked it. Installing Ghostscript like this:
$ sudo apt-get install ghostscript
After that try to configure ImageMagick:
$ ./configure --with-gslib
$ make
$ make install
After that i try to conver PDF to jpg
$ sudo /usr/local/bin/convert in.pdf out.jpg
And i see this mistake
convert: no decode delegate for this image format `/tmp/magick-BzHdr4Kp-00000001' # error/constitute.c/ReadImage/544.
convert: Postscript delegate failed `in.PDF': Нет такого файла или каталога # error/pdf.c/ReadPDFImage/678.
convert: no images defined `out.jpg' # error/convert.c/ConvertImageCommand/3044.
What i'm doing wrong?
Try the following convert commands to see more precisely what's possibly going wrong:
convert a.pdf -debug coder a.jpg
convert a.pdf -debug all a.jpg
There will possibly be a lot of output going to stderr. Amongst the lines you may see where IM is looking for Ghostscript. Also, try
convert -list delegate
convert -list delegate | grep --color -E '(eps|pdf)'
to find with which exact commandlines ImageMagick tries to run Ghostscript (it may call gsx instead of gs, or it may look for it in /usr/local/bin/...). If you find any deviations from your real Ghostscript installation, you can possibly fix it by editing delegates.xml.
convert -list configure
will show you how ImageMagick is configured (and if, for example, gs was during compile-time in the list in DELEGATES variables). Here you also find where to look for delegates.xml:
convert -list configure | grep CONFIGURE_PATH
should list the directory where this (as well as some more) *.xml settings files are located which control how convert et al. behave...

grep question using backslash

I have the following file:
asdasd
asd
asd
incompatible:svcnotallowed:svc\:/network/bmb/clerver\:default
incompatible:svcnotallowed:svc\:/network1/bmb/clerver\:default
incompatible:svcnotallowed:svc\:/network2/bmb/clerver\:default
asdasd
asd
asd
as
And now suppose I have the two variables v1="incompatible:svcnotallowed:" and v2="svc\:/network1/bmb/clerver\:default".
I would like to search the entire file using v1 and v2. I know this is a problem caused due to the file having a'\' in it. I just dont know how to eliminate it. I have tried storing v1 and v2 (both variable contents and grep usage) using single quotes, but in vain.
This is the series of commands I have tried :
grep "$v1$v2" file
grep '$v1$v2' file
I need this to work in KSH
please let me know the right way to use grep in this scenario.
Thanks.
grep -F "$v1$v2" file should do the trick -- with the -F option, it treats the pattern as a fixed string, so backslashes don't get interpreted as escapes or backreferences.
But fgrep "$v1$v2" file would probably be the most portable solution. As tomkaith13 notes in his comment, the -F option to grep isn't universally supported. On Solaris, the default grep doesn't support -F, but the version in /usr/xpg4/bin does.
Since you are using ksh, you can just use it to read the files
v1="incompatible:svcnotallowed:"
v2="svc\:/network1/bmb/clerver\:default"
while read -r line
do
case "$line" in
"$v1$v2" ) echo "$line";;
esac
done < file

How to convert a JPEG image into SVG format using ImageMagick?

How to convert a JPEG image into SVG format using ImageMagick?
you'll need to use potrace and convert to a bitmap first.
$convert input.jpg output.ppm
$potrace -s output.ppm -o svgout.svg
Actually, with a complete installation of a recent version of ImageMagick it should be as easy as:
convert some.jpeg some.svg
Of course, ImageMagick cannot do it all by itself -- it uses delegates (helper programs) to handle SVG input or output. (This has been pointed out by other answers already.)
To see a (partial) list of all delegates (and their respective commands), run
convert -list delegate
To see the config file where all the delegate secrets hide, see
convert -list delegate | grep delegates.xml
To see a (partial) list of SVG handling delegates, run
convert -list delegate | grep -i svg
However, ImageMagick likes to put some of its external helper utilities into 'stealth' mode and doesn't necessarily reveal their presence when using above commands.
Just look into the delegates.xml file itself. On my system it's:
grep -i svg /opt/local/etc/ImageMagick/delegates.xml | grep -i --color stealth
<delegate decode="autotrace" stealth="True" \
command=""/opt/local/bin/convert" "%i" \
"pnm:%u"\n\
"/opt/local/bin/autotrace" \
-input-format pnm \
-output-format svg \
-output-file "%o" "%u""/>
<delegate decode="svg:decode" stealth="True" \
command=""/opt/local/bin/inkscape" "%s" \
--export-png="%s" \
--export-dpi="%s" \
--export-background="%s" \
--export-background-opacity="%s" \
> "%s" 2>&1"/>
As you may see, on my system the ImageMagick installation automatically uses (amongst others)...
...inkscape to convert SVG to PNG;
...autotrace to convert PNM to SVG;
Of course, one could argue the benefits of rather using autotrace directly -- but that would require to manually convert the whatever-input-format to PNM first. So for this preliminary step you'd probably use ImageMagick anyway...
You'll actually need some software or code to vectorize your image in between, as jpg is a raster format, while SVG is a vector format. I don't think imagemagick alone can do that for you.

Resources