cache resources exhausted Imagemagick - ruby-on-rails

I'm using Imagemagick on a rails app with Minimagick and I generate some pictogram with it.
When I launch my process I have this error and I don't find a solution:
MiniMagick::Error (`convert -limit memory 2GiB -limit map 2GiB -limit disk 4GiB -background none -fill #000000 -font ttf/SELIS006N.ttf -pointsize 300 label: S public/pictogram_images/RECINTAN-EL-064-layer-1.png` failed with error:
convert.im6: cache resources exhausted ` S' # error/cache.c/OpenPixelCache/4078.
convert.im6: no images defined `public/pictogram_images/RECINTAN-EL-064-layer-1.png' # error/convert.c/ConvertImageCommand/3044.
):
My process is simple, I have some .tff file and each character is a pictogram. I just want to generate all preview of this character in png.

Find the policy.xml with find / -name "policy.xml"
something like /etc/ImageMagick-6/policy.xml
and change
<policy domain="resource" name="disk" value="1GiB"/>
to
<policy domain="resource" name="disk" value="8GiB"/>
refer to convert fails due to resource limits
Memory issues

The error probably occurs because you run out of memory. You can check for the resources using the following command:
convert -list resource
The output will be somewhat like this:
Resource limits:
Width: 16KP
Height: 16KP
Area: 128MP
Memory: 256MiB
Map: 512MiB
Disk: 1GiB
File: 768
Thread: 8
Throttle: 0
Time: unlimited
Here, you can see that the assigned amounts of disk space and memory are very small. So, in order to modify it you need to change the policy.xml file located somewhere in /etc/ImageMagick-6 directory.
Change <policy domain="resource" name="disk" value="1GiB"/> to <policy domain="resource" name="disk" value="4GiB"/> in the policy.xml file.

sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml //this one is just to solve convertion from .tiff to pdf, you may need it some day
sed -i -E 's/name="memory" value=".+"/name="memory" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
sed -i -E 's/name="map" value=".+"/name="map" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
sed -i -E 's/name="area" value=".+"/name="area" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
sed -i -E 's/name="disk" value=".+"/name="disk" value="8GiB"/g' /etc/ImageMagick-6/policy.xml

Not sure exactly which item is causing you the problem, but it is probably one of these:
1) You need to put your font into ImageMagick's XML-based font file rather than specify a file.ttf in your convert command. To get the list of available fonts, use
identify -list font | more
Path: /Users/mark/.magick/type.xml <--- Edit your font into here
Font: ACaslonPro
family: unknown
style: Undefined
stretch: Undefined
weight: 0
glyphs: /Library/Fonts/ACaslonPro-Regular.otf
Font: ACaslonPro-Semibold
family: unknown
style: Undefined
stretch: Undefined
weight: 0
glyphs: /Library/Fonts/ACaslonPro-Semibold.otf
...
...
At the beginning you will see the path to the config file for your fonts and you need to edit that to include your TTF file you mentioned. If you have lots of fonts to add, you may like to automate the process - see my other post here.
2) You may need to escape the # in your -fill option, or at least surround it by single, or double quotes to hide it from the shell, if your MiniMagick invokes via shell - I don't know the ins and outs of MiniMagick.
3) You may need to quote the letter S that you wish to output inside single or double quotes.
4) You may need to remove the space after the colon following label.
What I am getting at is that your command should maybe be more like this:
convert -limit memory 2GiB -limit map 2GiB -limit disk 4GiB -background none -fill "#000000" -font "TimesNewRoman" -pointsize 300 label:"S" output.png

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

Batch append images in groups of two with Imagemagick

I have a directory of images and need to merge those images horizontally in groups of two, then save the output of each to a new image file:
image-1.jpeg
image-2.jpeg
image-3.jpeg
image-4.jpeg
image-5.jpeg
image-6.jpeg
Using Imagemagick via command line, is there a way to loop through every other image in a directory and run magick convert image-1.jpeg image-2.jpeg +append image-combined-*.jpg?
So the result would be combined pairs of images:
image-1.jpeg image-2.jpeg -> image-combined-1.jpg
image-3.jpeg image-4.jpeg -> image-combined-2.jpg
image-5.jpeg image-6.jpeg -> image-combined-3.jpg
Get them all appended succinctly and in parallel with GNU Parallel and actually use all those lovely CPU cores you paid Intel for!
parallel -N2 convert {1} {2} +append combined-{#}.jpeg ::: *jpeg
where:
-N2 says to take two files at a time
{1} and {2} are the first two parameters
{#} is the sequential job number, and
::: demarcates the start of the parameters
If your CPU has 8 cores, GNU Parallel will run 8 converts at once, unless you specify say 4 jobs at a time by adding -j4.
If you are learning and just finding your way with GNU Parallel add:
--dry-run so you can see what it would do without actually doing anything
-k to keep the outputs in order
So, I mean:
parallel --dry-run -k -N2 convert {1} {2} +append combined-{#}.jpeg ::: *jpeg
Sample Output
convert image-1.jpeg image-2.jpeg +append combined-1.jpeg
convert image-3.jpeg image-4.jpeg +append combined-2.jpeg
convert image-5.jpeg image-6.jpeg +append combined-3.jpeg
On macOS, you can simply install GNU Parallel with:
brew install parallel
If you have thousands, or hundreds of thousands of files, you may run into an error Argument list too long - although this is pretty rare on macOS because the limit is 262,144 characters:
sysctl -a kern.argmax
kern.argmax: 262144
If that happens, you can use this syntax to pipe the filenames in GNU Parallel instead:
find /somewhere -iname "*.jpeg" -print0 | parallel -0 -N2 convert {1} {2} +append combined-{#}.jpeg
If the images are all the same size and orientation, and if your system has the memory to read in all the images in the directory, it can be done as simply as this...
magick *.jpeg -set option:doublewide %[fx:w*2] \
+append +repage -crop %[doublewide]x%[h] +repage image-combined-%02d.jpg
This can be scripted easily using ImageMagick. I could show you how in Unix. But if you have more than 9 images, then you may have to rename with leading zeros, since alphabetically image-10 will come before image-2. You do not mention your IM version or platform and scripting will differ depending upon OS.
Here is a Unix solution. I have images rose-01.jpg ... rose-06.jpg in folder test on my desktop (Mac OSX). Each image has a label under it with its filename so we can keep track of the files.
cd
cd desktop/test
arr=(`ls *.jpg`)
num=${#arr[*]}
for ((i=0; i<num; i=i+2)); do
j=$((i+1))
k=$((i+2))
magick ${arr[$i]} ${arr[$j]} +append newimage_${j}_${k}.jpg
done
Note that arrays start with index 0. So I use j=i+1 and k=i+2 for the images that correspond to 1,2 3,4 5,6 in the filenames from ls in the array.
The result is (newimage_1_2.jpg, newimage_3_4.jpg, newimage_5_6.jpg)
An alternate solution is to montage all the images together two-by-two as an array of 2x3 and then equally crop them into 3 sections vertically. So in ImageMagick, this also works since these images are all the same size.
cd
cd desktop/test
arr=(`ls *.jpg`)
num=${#arr[*]}
num2=`magick xc: -format "%[fx:ceil($num/2)]" info:`
magick montage ${arr[*]} -tile 2x -geometry +0+0 miff:- | magick - -crop 1x3# +repage newimage.jpg
The results are: newimage-0.jpg, newimage-1.jpg, newimage-2.jpg
Ole Tang wrote:
Fails on filenames like My summer photo.jpg
So here is the solution using ImageMagick as modified from my original post.
Images:
rose 1.png
rose 2.png
rose 3.png
rose 4.png
rose 5.png
rose 6.png
OLDIFS=IFS
IFS=$'\n'
arr=(`ls *.png`)
for ((i=0;i<6;i++)); do
echo "${arr[$i]}"
done
IFS=OLDIFS
num=${#arr[*]}
for ((i=0; i<num; i=i+2)); do
j=$((i+1))
k=$((i+2))
magick "${arr[$i]}" "${arr[$j]}" +append newimage_${j}_${k}.jpg
done
This produces:
newimage_1_2.jpg
newimage_3_4.jpg
newimage_5_6.jpg

Trim command /imagemagick in command line to remove white space from iamges in /uploads folder

I can't work out the syntax.
Client has loaded 8000 images, all with huge whitespace around them.
how do i run a imagemagick trim command from the command line, that will trim all images in the /uploads folder and also from all files in subdirectories
Is the whitespace always the same size? If yes determine it and adapt the crop options. Moreover you will probably need to filter on file name.
find . -name *.jpg -type f -print0 | xargs -0 -I{} convert {} -crop 40x30+10+10 {}
Use -trim argument with the ImageMagick's mogrify utility.
mogrify -trim /uploads/*
Repeat for sub-directories, or use find utility.

.CDR to .SVG Convert Using ImageMagick

I am on CentOS 6.4 and trying to convert .CDR to .SVG Convert Using ImageMagick using SSH command.
my 1.cdr file is in /var/www/vhosts/website.com/httpdocs/test/1.cdr
once converted to SVG it should be created in the same folder
Tried the following command:
convert /var/www/vhosts/website.com/httpdocs/test/1.cdr image.svg
The Error I am getting is:
sh: mplayer: command not found convert: Delegate failed "mplayer"
"%i" -really-quiet -ao null -vo png:z=3' #
delegate.c/InvokeDelegate/1032. convert: missing an image filename
image.svg' # convert.c/ConvertImageCommand/2800.
Not sure what does that mean ?
In order to convert CDR files you need to install uniconvertor for CDR delegate.
List of all delegates:
convert -list delegate
By default it outputs:
cdr => "uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o"
Install uniconvertor. For example, on Ubuntu it’s:
sudo apt-get install python-uniconvertor
Then run:
convert image.cdr -flatten -thumbnail '512x512' image.png
Or, with zoom cropping:
convert image.cdr -flatten -thumbnail '512x512^' -gravity center -crop 512x512+0+0 +repage image.png
And you’re done.
I convert to PNG here but you may use your own output format.
python-uniconvertor is part of inkscape.
It does not exist by itself.
Ubuntu/Mint recently removed all the old Python stuff, for Corel Draw I have to fire up the WinXP VM & Corel and export something Linux understands, usually PNG, a favourite
CDR & WMF files are pretty much dead to Linux, ImageMagick can still handle WMF though.

ImageMagick fails on php but works in shell

I've this command:
/usr/local/bin/convert -density 200 /singlePage.pdf -colorspace RGB -verbose -geometry 1155 -quality 10 -limit area 100mb singlePicture.jpg
When executing with php (via browser) it has no result output (executing with php function exec()).
When executing the same command on shell, it works perfectly.
I tried another pdf file, which works on php and shell. The only difference is the filesize.
1,0806 MB => Works
1,0962 MB => Not Works
Any ideas?
So this:
/usr/local/bin/convert -density 200 /singlePage.pdf -colorspace RGB -verbose -geometry 1155 -quality 10 -limit area 100mb singlePicture.jpg
implies that the singlePage.pdf file is located on the root of your filesystem. I doubt that is true. My guess is the "/singlePage.pdf" path is wrong.

Resources