There are some scanned image files. The scan is made larger than the actual content, such that white borders are shown in the image files. I would like to (use ImageMagick to auto detect and) trim the borders, while not trimming so hard (in case the content background is white also) by keeping a minimum size of the image.
Same question has been asked about a year ago: ImageMagick: trim but keep a minimum size. I guess the trim:minSize=geometry were not available at that moment.
Documentations are here:
https://imagemagick.org/script/command-line-options.php#trim
-trim
...
Use -define trim:minSize=geometry limit the trim to the specified size.
https://imagemagick.org/script/command-line-options.php#define
-define key{=value}...
https://imagemagick.org/script/defines.php
trim:minSize=geometry
Limit the trim to the specified size.
Previous discussion for adding the trim:minSize feature: https://github.com/ImageMagick/ImageMagick/discussions/4861?sort=new.
My trial:
# magick -verbose input.tif -fuzz 5% -define trim:minSize=2310x2600 -trim output.tif
input.tif TIFF 2368x2633 2368x2633+0+0 8-bit TrueColor sRGB 16.915MiB 0.020u 0:00.025
input.tif=>output.tif TIFF 2368x2633=>2301x2586 2368x2633+67+0 8-bit TrueColor sRGB 17.0247MiB 0.020u 0:00.022
input.tif is a scanned image of 2368x2633px with white borders.
The above command output is trimmed to 2301x2586px, which is not respecting the trim:minSize=2310x2600 constrain.
FYI: The solution in the linked question is adding a border after the trim, which is not ideal. If the content is close to white but not white, it will be trimmed then a white border is added.
Is it that I wrongly used the magick command or parameters, or ImageMagick trim:minSize definition is really not working?
Edit:
More information about the question.
I just updated ImageMagick to version 7.1.0-48 on Gentoo Linux.
# magick -version
Version: ImageMagick 7.1.0-48 Q16 x86_64 20449 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenCL OpenMP(4.5)
Delegates (built-in): bzlib cairo freetype jng jpeg lcms ltdl pangocairo png rsvg tiff x xml zlib
Compiler: gcc (11.3)
The original tiff image I was working on is too large (17MB) for testing and upload. I downsized it to the following 719x800px smaller png. There are still weird results with it.
First, try basic trim. It trimmed to 676x784px.
# magick -verbose input.png -fuzz 15% -trim output.png
input.png PNG 719x800 719x800+0+0 8-bit sRGB 805860B 0.010u 0:00.011
input.png=>output.png PNG 719x800=>676x784 719x800+29+2 8-bit sRGB 805860B 0.210u 0:00.191
Then use -define trim:minSize=710x799 to limit the trim. It trimmed to 707x798px, which is smaller than the minSize.
# magick -verbose input.png -define trim:minSize=710x799 -gravity south -fuzz 15% -trim output.png
input.png PNG 719x800 719x800+0+0 8-bit sRGB 805860B 0.010u 0:00.010
input.png=>output.png PNG 719x800=>707x798 719x800+12+2 8-bit sRGB 805860B 0.220u 0:00.198
If we omit the -gravity south. It even trims to 690x798px, much smaller than the minSize.
# magick -verbose input.png -define trim:minSize=710x799 -fuzz 15% -trim output.png
input.png PNG 719x800 719x800+0+0 8-bit sRGB 805860B 0.010u 0:00.010
input.png=>output.png PNG 719x800=>690x798 719x800+29+2 8-bit sRGB 805860B 0.220u 0:00.193
I am not sure how to use the +gravity and +repage to reset the gravity and virtual canvas. It seems they do not help. In both cases, it still trims to 690x798px.
# magick -verbose input.png -define trim:minSize=710x799 +gravity -fuzz 15% -trim output.png
input.png PNG 719x800 719x800+0+0 8-bit sRGB 805860B 0.010u 0:00.011
input.png=>output.png PNG 719x800=>690x798 719x800+29+2 8-bit sRGB 805860B 0.220u 0:00.192
# magick -verbose input.png +repage -define trim:minSize=710x799 -fuzz 15% -trim output.png
input.png PNG 719x800 719x800+0+0 8-bit sRGB 805860B 0.010u 0:00.010
input.png=>output.png PNG 719x800=>690x798 719x800+29+2 8-bit sRGB 805860B 0.220u 0:00.199
I am not sure if it is legit to use -gravity in combination with -define trim:edges. I find weird result from fmw42's sample picture also. (fmw42's sample picture is more trivial that its border is absolutely blue. It does not need -fuzz.)
# magick -verbose lena_b50.png -define trim:minSize=300x300 -gravity south -define trim:edges=east,south,west -trim lena_b50_trim.png
lena_b50.png PNG 356x356 356x356+0+0 8-bit sRGB 121273B 0.000u 0:00.002
lena_b50.png=>lena_b50_trim.png PNG 356x356=>256x306 356x356+50+0 8-bit sRGB 121273B 0.030u 0:00.025
Lexically this makes sense. -gravity south aligns the content to top. -define trim:edges=east,south,west avoids trimming the north edge.
Note it is trimmed to 256x306px, smaller than minSize of 300x300px.
Trim edges east,south,west with minSize is 330x330px.
It produces 306x330px result, with strange alignment.
# magick -verbose lena_b50.png -define trim:edges=east,south,west -define trim:minSize=330x330 -trim lena_b50_trim.png
lena_b50.png PNG 356x356 356x356+0+0 8-bit sRGB 121273B 0.000u 0:00.002
lena_b50.png=>lena_b50_trim.png PNG 356x356=>306x330 356x356+50+0 8-bit sRGB 121273B 0.040u 0:00.027
With east,south,west and 330x330, add also -gravity south. It produces reasonable result.
# magick -verbose lena_b50.png -define trim:edges=east,south,west -define trim:minSize=330x330 -gravity south -trim lena_b50_trim.png
lena_b50.png PNG 356x356 356x356+0+0 8-bit sRGB 121273B 0.000u 0:00.002
lena_b50.png=>lena_b50_trim.png PNG 356x356=>330x330 356x356+13+0 8-bit sRGB 121273B 0.040u 0:00.025
Yet if the minSize is smaller (300x300px). It fails again. Result image is 256x306px.
# magick -verbose lena_b50.png -define trim:edges=east,south,west -define trim:minSize=300x300 -gravity south -trim lena_b50_trim.png
lena_b50.png PNG 356x356 356x356+0+0 8-bit sRGB 121273B 0.000u 0:00.001
lena_b50.png=>lena_b50_trim.png PNG 356x356=>256x306 356x356+50+0 8-bit sRGB 121273B 0.030u 0:00.026
It works fine for me on Imagemagick 7.1.0.57 Q16 Mac OSX Monterey.
Input (size 356x356):
magick lena_b50.png -define trim:minSize=300x300 -gravity center -trim lena_b50_trim.png
Result (size 300x300)
ADDITIONI
The minSize define is a relatively new feature (7.1.0.30 and higher). So it was not implemented in your older version of Imagemagick 7.
Also one may need to add -fuzz XX% to the command depending upon the image.
I believe that if your minSize values relative to -gravity go beyond the limits of the flat color region, then it will limit to the the image bounds or how much it will be reduced by the -fuzz.
It works for me for trimSize of 270x270 which is still larger than the image size of 256x256. So your comment about not working below 300x300 is not accurate.
magick lena_b50.png -define trim:minSize=270x270 -gravity center -trim lena_b50_trim2.png
Greatly based on fmw42's code snippet here, I have written a script to achieve the -minSize feature, tweaked to my taste.
If gravity=center, the cropped area is based on the centre of the original image, rather than adding even margins from the -trim detected area. This is because, in case the detected area is biased to one side, the produced result will be less biased to one side, which is more reasonably centred.
For other gravity settings, it acts like the gravitational force is applied from that side. For example, for gravity=southwest, the result image is "attracted" to the lower left corner (SW). This is an opposite interpretation of fmw42's code snippet in that link.
Last but not least, it works even if -minSize exceeds the original image size, and also works for -minSize smaller than the -trim detected size.
Here is my script:
#!/bin/sh
# Usage: ebook.trim.minSize input.jpg 75% east,south,west 1024 768 north output.jpg
input=$1; fuzz=$2; trimEdges=$3; minw=$4 ; minh=$5; output=$7
gravity=$( printf '%s' "$6" | tr '[:upper:]' '[:lower:]' | sed 's/^centre$/center/' )
w=$(magick "$input" -format '%w' info:)
h=$(magick "$input" -format '%h' info:)
trimvals=$(magick "$input" -fuzz "$fuzz" -define trim:edges="$trimEdges" -format '%#' info: | tr 'x' '+')
ww=$(printf '%s' "$trimvals" | cut -d+ -f1)
hh=$(printf '%s' "$trimvals" | cut -d+ -f2)
x0=$(printf '%s' "$trimvals" | cut -d+ -f3)
y0=$(printf '%s' "$trimvals" | cut -d+ -f4)
x1=$((w-x0-ww))
y1=$((h-y0-hh))
# Ensure $minw <= $w
if [ "$minw" -gt "$w" ]; then minw=$w; fi
# Ensure $minh <= $h
if [ "$minh" -gt "$h" ]; then minh=$h; fi
# validate user input $gravity
case $gravity in
center) ;;
north) ;;
northeast) ;;
east) ;;
southeast) ;;
south) ;;
southwest) ;;
west) ;;
northwest) ;;
*) printf "error \$gravity=%s\n" "$gravity"; exit;
esac
fixMinSize() {
trimSize=$1 ; minSize=$2 ; imgSize=$3
nearMargin=$4 ; farMargin=$5
nearEnd=$6 ; farEnd=$7 ; adjNear=$8 ; adjFar=$9
# Ensure this validity:
# detected trim size <= defined min size <= image side dimension
# $trimSize <= $minSize <= $imgSize
# if $trimSize >= $minSize then nothing needed to be done.
# if $trimSize < $minSize then need to expand $trimSize.
if [ "$trimSize" -lt "$minSize" ]; then
# if $gravity = << center >> || << north or west >> || << south or east >>
if [ "$gravity" = 'center' ] || [ "$gravity" = "$adjNear" ] || [ "$gravity" = "$adjFar" ]; then
evenMargin=$(( (imgSize - minSize) / 2 ))
# if -trim detected zone is << LEFT / TOP >> of the center zone,
# i.e. it exceeds the center zone at the near end,
# then nothing needed to be done. Will do $trimSize=$minSize at the end.
# No worries, $trimSize <= $minSize <= $imgSize. It won't pop out.
if [ "$evenMargin" -gt "$nearMargin" ]; then
:
# if -trim detected zone is << RIGHT / BOTTOM >> of the center zone,
# i.e. it exceeds the center zone at the far end,
# then shift the trim zone as if gravity={ east or south }.
# This takes care of odd number margins
elif [ $(( imgSize - evenMargin - minSize )) -gt "$farMargin" ]; then
nearMargin=$(( imgSize - farMargin - minSize ))
# else -trim detected zone is << WITHIN >> the center zone
else
nearMargin=$evenMargin
fi
# if $gravity contains << west or north >> i.e. the near end.
elif [ "${gravity#*"$nearEnd"}" != "$gravity" ]; then
# if $(( nearMargin + minSize )) pops out of the image dimension in the far end
if [ $(( nearMargin + minSize )) -gt "$imgSize" ]; then
nearMargin=$(( imgSize - minSize ))
fi
# if $gravity contains << east or south >> i.e. the far end.
elif [ "${gravity#*"$farEnd"}" != "$gravity" ]; then
nearMargin=$(( imgSize - farMargin - minSize ))
# if $nearMargin pops out of the image in the near end
if [ $nearMargin -lt 0 ]; then
nearMargin=0
fi
fi
trimSize=$minSize
fi
}
# Handle latitudinal trim zone shift
fixMinSize "$ww" "$minw" "$w" "$x0" "$x1" 'west' 'east' 'north' 'south'
ww=$trimSize
x0=$nearMargin
# Handle longitudinal trim zone shift
fixMinSize "$hh" "$minh" "$h" "$y0" "$y1" 'north' 'south' 'west' 'east'
hh=$trimSize
y0=$nearMargin
# -trim zone is fixed according to minSize and gravity, actual -crop here.
magick "${input}" -crop "${ww}x${hh}+${x0}+${y0}" +repage "${output}"
To test the script, download fmw42's image. Rename it to lena_b50.png. Run the following test script:
#!/bin/sh
# One liner test
# ~/bin/ebook.trim.minSize lena_b50.png 123 356 northeast output.png
mkdir -pv fit ori out
mkdir -pv fitWidth_oriHeight fitWidth_else oriWidth
mkdir -pv fitHeight_oriWidth fitHeight_else oriHeight
for minWidth in 0 20 240 256 266 300 333 356 380; do
for minHeight in 0 20 240 256 266 300 333 356 380; do
for gravity in center north northeast east southeast south southwest west northwest; do
if [ ! "$minWidth" -gt 256 ] && [ ! "$minHeight" -gt 256 ]; then folder='fit'
elif [ ! "$minWidth" -gt 256 ] && [ ! "$minHeight" -lt 356 ]; then folder='fitWidth_oriHeight'
elif [ ! "$minHeight" -gt 256 ] && [ ! "$minWidth" -lt 356 ]; then folder='fitHeight_oriWidth'
elif [ ! "$minWidth" -gt 256 ]; then folder='fitWidth_else'
elif [ ! "$minHeight" -gt 256 ]; then folder='fitHeight_else'
elif [ ! "$minWidth" -lt 356 ] && [ ! "$minHeight" -lt 356 ]; then folder='ori'
elif [ ! "$minWidth" -lt 356 ]; then folder='oriWidth'
elif [ ! "$minHeight" -lt 356 ]; then folder='oriHeight'
else folder='out'
fi
output_image_name="output_${gravity}_${minWidth}x${minHeight}.png"
~/bin/ebook.trim.minSize lena_b50.png "$minWidth" "$minHeight" "$gravity" "${folder}/${output_image_name}"
output_image_width=$( magick "${folder}/$output_image_name" -format '%w' info:)
output_image_height=$(magick "${folder}/$output_image_name" -format '%h' info:)
expected_width=$minWidth; expected_height=$minHeight
if [ "$minWidth" -lt 256 ]; then expected_width=256; fi
if [ "$minHeight" -lt 256 ]; then expected_height=256; fi
if [ "$minWidth" -gt 356 ]; then expected_width=356; fi
if [ "$minHeight" -gt 356 ]; then expected_height=356; fi
if [ "$output_image_width" -ne $expected_width ] || [ "$output_image_height" -ne $expected_height ]; then
echo 'error!!'; exit
fi
done
done
done
How do I combine the following ImageMagick command line commands into a single instruction:
convert -trim "C:\test\test.webp" -quality 95 "C:\test\testMaxNew.webp"
convert "C:\test\testMaxNew.webp" -resize 750x750 "C:\test\testMediumNew.webp"
convert "C:\test\testMediumNew.webp" -thumbnail 100x100^ "C:\test\testSmallNew.webp"
After some trial and error I came up with this:
convert -trim C:\test\test.webp -quality 95 -write mpr:XY +delete \( mpr:XY +write C:\test\testLargeNew.webp +delete \) \( mpr:XY -resize 750x750 +write C:\test\testMediumNew.webp +delete \) \( mpr:XY -resize 100x100^ -gravity center -extent 100x100 +write C:\test\testSmallNew.webp +delete \)
This does the trick but the following errors are reported in the command line prompt:
> convert: unable to open image '\(': No such file or directory #
> error/blob.c/OpenBlob/2695. convert: no decode delegate for this image
> format `' # error/constitute.c/ReadImage/508. convert: unable to open
> image '\)': No such file or directory # error/blob.c/OpenBlob/2695.
> convert: no decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\(':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\)':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\(':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: no images defined `\)' #
> error/convert.c/ConvertImageCommand/3235.
Can someone explain why I am getting these errors and if my code is correct?
The reason I am trying to combine multiple commands is to minimize processing time.
Version: ImageMagick 7.0.2-0 Q16 x64 on Windows 10
These should do what you want.
Best to read the input right after convert.
I am not sure why you need to resize and thumbnail.
So in Imagemagick try one of the following:
convert "C:\test\test.webp" -trim +repage -quality 95 -resize 750x750 -thumbnail 100x100^ "C:\test\testSmallNew.webp"
or just
convert "C:\test\test.webp" -trim +repage -quality 95 -thumbnail 100x100^ "C:\test\testSmallNew.webp"
Just adding an answer to clarify a couple of things that are not a good fit for comments.
You already have some excellent advice from Fred (#fmw42) as regards reading your input image immediately after convert because that way your command will continue to work when you upgrade to ImageMagick v7 which has already been available a couple of years.
You have added a command that works to your question, but that is a bit clumsy inasmuch as it creates an MPR which you don't need and also creates and destroys images unnecessarily - adding to system load which is undesirable if you have many images to process. I think you can see the following is simpler to understand and maintain, makes fewer copies and deletes and demands on memory, and should achieve the same effect as your command:
convert INPUT.webp -trim +repage -gravity center -quality 95 ^
+write LARGE.webp ^
-resize 350x350 +write MEDIUM.webp ^
-resize 100x100^ -extent 100x100 SMALL.webp
Using Ghostscript 9.53 on Mac with Imagemagick 7.0.10-59.
The EPS has a Tiff Preview on it, which gets added as a frame in gif or as an extra image for PNG. It started happening after updating imagemagick and ghostscript.
Ghostscript 9.51 and imagemagick 7.0.10-0 without ghostscript delegate did not have this issue.
Command: magick convert -verbose -density 200 -channel RGBA -colorspace RGB -alpha On my.eps my.gif
the new output is as follows:
/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-3MLz7XbvUU4OJOQBAqLqB-7bteT5s-061 PNG 890x505 890x505+0+0 8-bit sRGB 23054B 0.020u 0:00.014
EPS:/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-tWS3M8-k1os9hbHqWGRzlibuupNgItzv=>/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-tWS3M8-k1os9hbHqWGRzlibuupNgItzv EPS 890x505 890x505+0+0 16-bit sRGB 23054B 0.000u 0:00.000
TIFF:~/Downloads/my.eps=>~/Downloads/my.eps TIFF 321x182 321x182+0+0 8-bit Palette sRGB 256c 148108B 0.000u 0:00.004
~/Downloads/my.eps[0] EPT 890x505 890x505+0+0 16-bit sRGB 23054B 0.010u 0:00.008
~/Downloads/my.eps[0] TIFF 321x182 321x182+0+0 8-bit Palette sRGB 256c 148108B 0.010u 0:00.000
[ghostscript library 9.52] -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r200x200' -g890x505 -dFirstPage=1 -dLastPage=1 -dEPSCrop '-sOutputFile=/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-3MLz7XbvUU4OJOQBAqLqB-7bteT5s-06%d' '-f/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-vyRuqlNsQcaG9cv4dJXLql9ChPsp95-g' '-f/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-nK22ULA5kPuCN9Yq8dKM7E8NTQxI82vH'~/Downloads/my.eps=>~/Downloads/my.gif EPT 890x505 890x505+0+0 16-bit Palette sRGB 35c 18001B 0.960u 0:00.139
convert: Invalid TIFF directory; tags are not sorted in ascending order. `TIFFReadDirectoryCheckOrder' # warning/tiff.c/TIFFWarnings/1044.
Older version ouput was as follows:
'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r200x200' -g890x505 -dEPSCrop '-sOutputFile=/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084UYHZpdfAwolz%d' '-f/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084Zug0r87QHWuH' '-f/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084JWA1MKLfJ5i7'
/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084UYHZpdfAwolz1 PNG 890x505 890x505+0+0 8-bit sRGB 23054B 0.020u 0:00.013
EPS:/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084Rxd2cgpYXsmR=>/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084Rxd2cgpYXsmR EPS 890x505 890x505+0+0 16-bit sRGB 23054B 0.010u 0:00.000
~/Desktop/my.eps EPT 890x505 890x505+0+0 16-bit sRGB 23054B 0.000u 0:00.000
~/Desktop/my.eps=>~/Desktop/my.gif EPT 890x505 890x505+0+0 16-bit PaletteBilevelAlpha sRGB 3c 9061B 0.830u 0:00.121
Is there a way for me to tell imagemagick to ignore tiff preview?
EPS File here
Bad Gif here
** Updated file links
In ImageMagick 7, use magick not magick convert and not convert. Also if you have only one frame/page and want to keep on that, then add [0] to your input image. So try
magick -verbose -density 200 -channel RGBA -colorspace RGB -alpha On my.eps[0] my.gif
I have a folder of images of varying sizes and quality. Is there a way with ImageMagic that I could automatically resize them to be no bigger 1100px x 1100px and less than 160kb. And not to re-size if they are smaller than those parameters. Also not to distort the image so it fits within but to only re-size so for example an image which is 2200px by 1000px would become 1100px by 500px.
I'm working on Ubuntu with ImageMagick 6.7.7-10.
Try this on a copy of your files:
mogrify -define jpeg:extent=160k -resize 1100x1100\> *.jpg
I am assuming your files are JPEG images. The command will largely work for other image types too, but it can only enforce the 160kB limit for JPEG files - not PNG, or TIFF etc.
Example
With version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-06
convert -size 2000x2000 xc:gray +noise random a.jpg
ls -lrt a.jpg
-rw-r--r-- 1 mark staff 6969601 21 Aug 18:28 a.jpg # <--- 7MB
mogrify -define jpeg:extent=160k -resize 1100x1100\> a.jpg
ls -lrt a.jpg
-rw-r--r-- 1 mark staff 147938 21 Aug 18:28 a.jpg # <--- 160kB
identify a.jpg
a.jpg JPEG 1100x1100 1100x1100+0+0 8-bit sRGB 148KB 0.000u 0:00.000
I've been at this all day. I'm trying to upload images to a Mediawiki and this is the error I get when ImageMagick tries to create the thumbnail:
Error creating thumbnail: convert: no decode delegate for this image format `/tmp/magick-11924QG1rRXzT948I' # error/constitute.c/ReadImage/552.
convert: no images defined `/tmp/s3thumb-cripEh' # error/convert.c/ConvertImageCommand/3127.
I setup a debug file for mediawiki and this is what I get in the log:
BitmapHandler::doTransform: creating 112x120 thumbnail at /tmp/s3thumb-cripEh using scaler im
BitmapHandler::doTransform: called wfMkdirParents(/tmp)
BitmapHandler::getMagickVersion: Running convert -version
wfShellExec: /bin/bash '/var/www/mediawiki-1.21.2/includes/limit.sh' ''\''/usr/local/bin/convert'\'' -version' 'MW_CPU_LIMIT=180; MW_CGROUP='\'''\''; MW_MEM_LIMIT=202400; MW_FILE_SIZE_LIMIT=102400; MW_WALL_CLOCK_LIMIT=180'
BitmapHandler::transformImageMagick: running ImageMagick: '/usr/local/bin/convert' -quality 80 -background white -define jpeg:size=112x120 '' -thumbnail '112x120!' -depth 8 -sharpen '0x0.4' -rotate -0 '/tmp/s3thumb-cripEh' 2>&1
wfShellExec: /bin/bash '/var/www/mediawiki-1.21.2/includes/limit.sh' 'OMP_NUM_THREADS='\''1'\'' '\''/usr/local/bin/convert'\'' -quality 80 -background white -define jpeg:size=112x120 '\'''\'' -thumbnail '\''112x120!'\'' -depth 8 -sharpen '\''0x0.4'\'' -rotate -0 '\''/tmp/s3thumb-cripEh'\'' 2>&1' 'MW_CPU_LIMIT=180; MW_CGROUP='\'''\''; MW_MEM_LIMIT=202400; MW_FILE_SIZE_LIMIT=102400; MW_WALL_CLOCK_LIMIT=180'
[thumbnail] thumbnail failed on ip-10-168-26-167: error 1 "convert: no decode delegate for this image format `/tmp/magick-11924QG1rRXzT948I' # error/constitute.c/ReadImage/552.
convert: no images defined `/tmp/s3thumb-cripEh' # error/convert.c/ConvertImageCommand/3127." from "'/usr/local/bin/convert' -quality 80 -background white -define jpeg:size=112x120 '' -thumbnail '112x120!' -depth 8 -sharpen '0x0.4' -rotate -0 '/tmp/s3thumb-cripEh' 2>&1"
LocalS3File::transform thumb:
LocalS3File::transform thumbTempPath: /tmp/s3thumb-cripEh, dest: wiki-images/thumb/1/19/5ovrDaU.jpg/112px-5ovrDaU.jpg
info:1
LocalS3File::transform return thumb: MediaTransformError Object
(
[htmlMsg] => Error creating thumbnail: convert: no decode delegate for this image format `/tmp/magick-11924QG1rRXzT948I' # error/constitute.c/ReadImage/552.<br />
convert: no images defined `/tmp/s3thumb-cripEh' # error/convert.c/ConvertImageCommand/3127.<br />
[textMsg] => Error creating thumbnail: convert: no decode delegate for this image format `/tmp/magick-11924QG1rRXzT948I' # error/constitute.c/ReadImage/552.<br />
convert: no images defined `/tmp/s3thumb-cripEh' # error/convert.c/ConvertImageCommand/3127.<br />
[width] => 112
[height] => 120
[url] =>
[path] =>
[file] =>
[page] =>
[responsiveUrls] => Array
(
)
[storagePath:protected] =>
)
Tried from the command line, copy pasted the command from the log (but used a test file):
convert -quality 80 -background white -define jpeg:size=112x120 '' -thumbnail '112x120!' -depth 8 -sharpen '0x0.4' -rotate -0 'logo.jpg'
but the process hangs. If I run:
sudo convert logo.png -quality 80 -background white -define jpeg:size=112x120 -thumbnail '112x120!' -depth 8 -sharpen '0x0.4' -rotate -0 logo.jpg
It works.
If I check DELEGATES, I have:
DELEGATES jng jp2 jpeg png ps tiff xml zlib
I tried increasing the default memory on media wiki to
$wgMaxShellMemory = 202400;
I feel like I've tried anything. Any ideas?
EDIT:
This is what I've discovered so far:
I'm pretty sure the shell wasn't executing the ImageMagick command because of the escaped backslashes in :
wfShellExec: /bin/bash '/var/www/mediawiki-1.21.2/includes/limit.sh' ''\''/usr/local/bin/convert'\'' -version' 'MW_CPU_LIMIT=180; MW_CGROUP='\'''\''; MW_MEM_LIMIT=202400; MW_FILE_SIZE_LIMIT=102400; MW_WALL_CLOCK_LIMIT=180'
Those ''\'' are causing the command to not run, and hence the "no decode" error. It can't decode, because the file is not there. I've traced wfShellEec to GlobalFunctions.php. The wfShellExec function is at around line 2778 on my file.
In the if ( php_uname( 's' ) == 'Linux' ) block there is:
escapeshellarg( $cmd )
I removed the escapeshellarg() function and just left the $cmd on its own.
Tried uploading again, the error is gone, the files are created, but now the thumbnail files are 0 bytes.
Any ideas?
In the following, the empty string parameter '' represents what is supposed to be the input image:
convert -quality 80 -background white -define jpeg:size=112x120 '' -thumbnail '112x120!' -depth 8 -sharpen '0x0.4' -rotate -0 'logo.jpg'
When you ran sudo convert logo.png ... from the command line it worked because you had an input image (logo.png), but inside MW the source image parameter was missing. So the problem here is not with convert, ImageMagick naturally cannot convert an image that doesn't exist. The problem is that MW fails to supply the source image file name.
If your case is like mine, the empty '' image source parameter could trace back to permissions in the images directory. Make sure this directory and all its subdirectories are rwx by the server process. Once I opened up everything in this dir to the server, the errors went away and the images and thumbnails appeared perfectly.