I'm looking to efficiently generate various sized thumbnails with ImageMagick's convert utility in Python. Some of my image files are quite large (~15MB JPGs).
One way I could do it would be to take the full-sized image, and to generate the various thumbnails from the full-sized image, as follows:
convert sample_image.jpg -resize 1024x768 sample_image-1024x768.jpg
convert sample_image.jpg -resize 800x600 sample_image-800x600.jpg
convert sample_image.jpg -resize 400x300 sample_image-400x300.jpg
convert sample_image.jpg -resize 200x150 sample_image-200x150.jpg
But another way would be to resize the images from each other:
convert sample_image.jpg -resize 1024x768 sample_image-1024x768.jpg
convert sample_image-1024x768.jpg -resize 800x600 sample_image-800x600.jpg
convert sample_image-800x600.jpg -resize 400x300 sample_image-400x300.jpg
convert sample_image-400x300.jpg -resize 200x150 sample_image-200x150.jpg
Is there any downside to doing this, or perhaps a better way? It seems like this would be a lot more efficient.
As a corollary, are there any flags or "tricks" convert uses to speed up the process?
ImageMagick has a few tricks up its sleeves which help you to optimize for speed when you want to process large images and when you want to create different output from the same original:
Make use of ImageMagick's mpr:{name} feature, which makes it temporarily save the input image into a named memory program register, from which you can later (while processing) read the data much faster than you could do from harddisk.
Do all resize operations in one single process writing out the different output sizes you require.
And the even better news is you can combine both these into one single command.
So you do not need to run multiple processes with all their context-switching overhead -- do it all in one go.
The following example also crops two separate areas from the original image and creates re-sized thumbnails from them, just to show how many different operations IM can do in one commandline. It also, of course outputs the sizes you requested. (You'll need, of course, a really large-dimensioned input image for the cropping parameters to work).
convert \
huge-original.jpg \
-quality 80 \
-colorspace rgb \
+profile '*' \
-filter Lanczos \
-write mpr:copy-of-huge-original \
+delete \
mpr:copy-of-huge-original -crop '3000x2000+0+480' -resize '200x125!>' -write thumb1-extract.jpg +delete \
mpr:copy-of-huge-original -crop '2000x1500+280+220' -resize '75x75!>' -write thumb2-extract.jpg +delete \
mpr:copy-of-huge-original -resize '1024x768' -write sample-1024x768.jpg +delete \
mpr:copy-of-huge-original -resize '800x600' -write sample-800x600.jpg +delete \
mpr:copy-of-huge-original -resize '400x300' -write sample-400x300.jpg +delete \
mpr:copy-of-huge-original -resize '200x150' -write sample-200x150.jpg +delete \
mpr:copy-of-huge-original -resize '163x163!>' -write sample-163x163.jpg
Update
I only now saw the question asked by #JonathanOng: How to stream the output to <stdout>?
Assuming, you want the format going to stdout also be JPEG, you can try this:
convert \
huge-original.jpg \
-quality 80 \
-colorspace rgb \
+profile '*' \
-filter Lanczos \
+write mpr:copy-of-huge-original \
mpr:copy-of-huge-original -crop '3000x2000+0+480' -resize '200x125!>' +write thumb1-extract.jpg \
mpr:copy-of-huge-original -crop '2000x1500+280+220' -resize '75x75!>' +write thumb2-extract.jpg \
mpr:copy-of-huge-original -resize '1024x768' +write jpeg:- \
mpr:copy-of-huge-original -resize '800x600' +write jpeg:- \
mpr:copy-of-huge-original -resize '400x300' +write jpeg:- \
mpr:copy-of-huge-original -resize '200x150' +write jpeg:- \
mpr:copy-of-huge-original -resize '163x163!>' +write jpeg:-
This way each variant will go to stdout. How you deal with this stream of consecutive images then, is up to you...
Note, instead of writing -write filename +delete you can use +write filename. It amounts to the same effect.
I tried timing vipsthumbnail against #KurtPfeifle's excellent answer. I ran this with a 10k x 10k pixel RGB JPG image (about 15MB):
convert \
/data/john/pics/wtc.jpg \
-quality 80 \
-colorspace rgb \
+profile '*' \
-filter Lanczos \
-write mpr:copy-of-huge-original \
+delete \
mpr:copy-of-huge-original -resize '1024x768' -write sample-1024x768.jpg +delete \
mpr:copy-of-huge-original -resize '800x600' -write sample-800x600.jpg +delete \
mpr:copy-of-huge-original -resize '400x300' -write sample-400x300.jpg +delete \
mpr:copy-of-huge-original -resize '200x150' -write sample-200x150.jpg +delete \
mpr:copy-of-huge-original -resize '163x163!>' -write sample-163x163.jpg x.jpg
I found I needed the extra x.jpg at the end, I'm not sure why. On this machine (E5-1650 3.2 GHz, IM 6.8.9-9) I see:
$ time ./m.sh
real 0m6.560s
user 0m31.908s
sys 0m0.264s
peak RES 711MB
This is (I think) the equivalent with vipsthumbnail:
img=/data/john/pics/wtc.jpg
icc=/usr/share/color/argyll/ref/sRGB.icm
for size in 1024x768 800x600 400x300 200x150 163x163; do
vipsthumbnail $img --size $size --eprofile $icc -o vips-sample-$size.jpg[Q=80]
done
vipsthumbnail defaults to Lanczos3. Timing it with vips-8.4.4 I see:
$ time ./n.sh
real 0m2.376s
user 0m2.412s
sys 0m0.108s
peak RES 70MB
So a useful speedup, and a large drop in memory use.
Because memory use is low, you can run many vipsthumbnail in parallel without killing your server. If I change the script to be:
img=/data/john/pics/wtc.jpg
icc=/usr/share/color/argyll/ref/sRGB.icm
parallel vipsthumbnail $img \
--size {} --eprofile $icc -o vips-sample-{}.jpg[Q=80] ::: \
1024x768 800x600 400x300 200x150 163x163
I now see:
$ time ./n.sh
real 0m0.593s
user 0m1.960s
sys 0m0.100s
peak RES 280MB
More than 10x faster than ImageMagick.
For my point of view, and after testing, resizing 1024x768 to 800x600 is bad for rescaling algorithm.
The next resize are more easier, because of multiple of integer (2).
So, for quality reason, I personnaly thing, this is better :
convert sample_image.jpg -resize 1024x768 sample_image-1024x768.jpg
convert sample_image.jpg -resize 800x600 sample_image-800x600.jpg
convert sample_image-800x600.jpg -resize 400x300 sample_image-400x300.jpg
convert sample_image-400x300.jpg -resize 200x150 sample_image-200x150.jpg
15MB JPGs are really large. I would first resize it to reasonable size (say 2500x2500) with fastest "-sample" parameter and this smaller image then resize to different thumbnails.
You can make intelligent decision based on image size and choose the right way of resize.
I would recommend to focus on thumbnail quality instead of conversion speed so please look at different filters (-filter), sharping (-unsharp) and recommended downsampling methods
I'm thumbnailing ~50MB JPG files. The one option which made the biggest difference (~5x speedup) was "-define jpeg:size 128x128" before the input filename. The example given here:
http://www.imagemagick.org/Usage/formats/#jpg_read
...made a huge difference:
convert -define jpeg:size=128x128 jpeg_large.jpg -thumbnail 64x64 jpeg_thumbnail.jpg
-define jpeg:size allows ImageMagick to read in only as much data as it needs from disk, which greatly reduces load time for very large images.
As the linked page suggests, use a jpeg:size= of twice your final thumbnail size to avoid aliasing.
The -thumbnail option, described here:
http://www.imagemagick.org/Usage/resize/#thumbnail
...samples and strips the image, further speeding up the process.
Related
I'n very new to imagemagick and i need to learn how could i combined these two commands to make it work.
I need to add background image to a transparent image. I tried to combine these two commands but was not successful.
magick mogrify -path Output_Path -trim -filter Triangle -define filter:support=2 -thumbnail 450x450 -gravity center -extent 500x500 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip Transparent_Image_Path
magick Background_Image.png Transparent_Image.png -composite output.jpg
Result Should Be Like This :
Image Reference
Thanks in advance!
There are several possible syntaxes, depending on how your brain likes to work ;-)
What you need to bear in mind when trying to understand this is that all processing operations, e.g. -crop or -resize apply to all loaded images. So, you either need to load images that need processing and process them before loading images you don't want affected, or you need to restrict processing to certain images only... using parenthesised "aside" operations.
You can proceed like this:
load transparent foreground image first and process it before loading the background image, then
load background image, then
exchange the order and composite
That looks like this:
magick FOREGROUND.PNG -resize ... -crop ... \
BACKGROUND.PNG \
+swap -composite RESULT.JPG
Alternatively, you could:
load the background image, then
load the foreground image and process it "aside" within parentheses so that the background image is not affected
composite
That looks like this:
magick BACKGROUND.PNG \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
If you need to independently process both background and foreground images prior to compositing, use:
magick \
\( BACKGROUND.PNG -resize ... -crop ... \) \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
Note that if you use Windows:
the backslashes at line-ends become carets (^) and may not have trailing spaces after them,
the backslashes preceding opening and closing parentheses must be omitted,
most single quotes probably need replacing with double quotes - probably not applicable to this specific question.
Note that your command is probably unnecessarily complicated, I would recommend omitting quite a lot of it and seeing how you get on. For example, all the define png:XXX=YYY settings are irrelevant if you aren't creating a PNG as output.
You can carry forward the JPEG-related parameters (quality, interlace, upsampling) and put them in anywhere you like, probably at the start like this, but put the colorspace and strip at the end to ensure both input files are stripped:
magick -quality 82 -define jpeg:fancy-upsampling=off \
BACKGROUND.PNG ... \
FOREGROUND.PNG ... \
-composite -colorspace sRGB -strip RESULT.JPG
I'm trying to use this dataset to do Exposure Meging (Fusion) in Python. Each image in the dataset has an OpenEXR file that can be downloaded (i don't have much experience with this file format).
I want to extract different samples (jpg or png) from the OpenEXR file with different exposures .
I managed to do that in Darktable :
Open the OpenEXR file (image)
Change the Exposure
Save as jpg
redo for each exposure value (-3EV, -2EV, -1EV, 0EV, 1EV, 2EV, 3EV).
The problem : I have 100 images and i want to automate this process. any idea on how to do that ?
Thank you in advance
Since each increment of EV ("Exposure Value") corresponds to doubling the exposure, and EXR files are in linear light (not gamma-encoded), you would expect that you can double the pixel values in an EXR file to add 1EV and halve them to do -1EV...
So, I downloaded the Luxo EXR file from here. Then I went into Photoshop and clicked:
Image -> Mode -> 8-bits/channel
and selected Method = Exposure and Gamma and set exposure=+1 and saved the resulting file as a JPEG with +1 in its name. I repeated that for EV-3, EV-2, EV+0, EV+1, EV+2, EV+3.
I then looked at the resulting files with ImageMagick using commands like the following in the Terminal to examine the mean value of the combined RGB image:
magick identify -verbose image-EV+2.jpg
I then went about producing those same mean values, and found that the following works:
# To increase 1 EV
magick input.exr -evaluate multiply 2 result.jpg
# To increase 2 EV
magick input.exr -evaluate multiply 4 result.jpg
# To increase 3 EV
magick input.exr -evaluate multiply 8 result.jpg
And so on...
So, I wrote a bash script to do that as follows, which you could save in your HOME directory as adjust.sh:
#!/bin/bash
# Default file, if none specified
file=${1:-/Users/mark/Desktop/LuxoDoubleChecker.exr}
# Default EV of +1, if none specified
EV=${2:-1}
# Strip extension
base="${file%.*}"
# Apply given EV to file and save with new name
new="${base}EV${EV}.jpg"
echo "Applying EV $EV to $file, saving as $new"
magick "$file" -evaluate multiply $(bc -l <<< "2^$EV") "$new"
Then, just necessary once, make it executable:
chmod +x $HOME/adjust.sh
And then you run it like this to add +3EV to SomeImage.exr:
~/adjust.sh SomeImage.exr 3
Sample Output
Applying EV 3 to SomeImage.exr, saving as SomeImageEV3.jpg
Alternatively, if you save this script as allEVs.sh, it will load the specified image just once and generate all 7 exposures in one go without re-reading the input EXR file 7 times:
#!/bin/bash
# Default file, if none specified
file=${1:-/Users/mark/Desktop/LuxoDoubleChecker.exr}
# Strip extension to get base without extension
base="${file%.*}"
magick "$file" \
\( +clone -evaluate multiply 0.125 -write "${base}EV-3.jpg" +delete \) \
\( +clone -evaluate multiply 0.25 -write "${base}EV-2.jpg" +delete \) \
\( +clone -evaluate multiply 0.5 -write "${base}EV-1.jpg" +delete \) \
\( +clone -evaluate multiply 1 -write "${base}EV-0.jpg" +delete \) \
\( +clone -evaluate multiply 2 -write "${base}EV+1.jpg" +delete \) \
\( +clone -evaluate multiply 4 -write "${base}EV+2.jpg" +delete \) \
-evaluate multiply 8 "${base}EV+3.jpg"
Please check carefully that this works correctly for you before basing a lifetime's analysis on it...
Keywords: Image processing, HDR, High Dynamic Range, EXR, EV, Exposure Value, f-stop, stop, stops, exposure, increase, decrease, tone map, ImageMagick.
I've been looking at the documentation for imagemagick and looking at examples of scripts others have made, but I haven't been able to get this working.
The goal is to have imagemagick scale, crop, and save (appending the dimensions to the file names) multiple resized images of various aspect ratios.
For example, a folder containing Image1.png and Image2.png would result in:
Image1_1571x2646.png, Image1_1350x2150.png, Image1_1281x2039.png
Image2_1571x2646.png, Image2_1350x2150.png, Image2_1281x2039.png
Visual aid:
The animation above shows the simplest examples: a 1:1 square, a vertical rectangle, and a horizontal rectangle.
The images should scale to fit the longest dimension of the rectangle, and then crop any leftover pixels. The scaling and cropping should be done relative to the image centers.
Here's what I have so far (using macOS Terminal) but it doesn't work:
convert *.png -path /Users/user/Resized \
\( +clone -resize "1571x2646^” -gravity center -crop 1571x2646+0+0 +repage resultimage -write 1571x2646.png +delete \) \
\( +clone -resize "1350x2150^” -gravity center -crop 1350x2150+0+0 +repage resultimage -write 1350x2150.png +delete \) \
-resize "1281x2039^” -gravity center -crop 1281x2039+0+0 +repage resultimage 1281x2039.png
I'm not sure if I should use mogrify or convert, but if I use mogrify clone gives an error. I'm also not sure if multi-line instructions need to be put into a .sh file or something. The ^ denotes the dimension that should take priority (the larger one). I believe -gravity center is supposed to keep scaling and cropping relative to the image centers.
With Imagemagick, you must use convert. Mogrify cannot handle the parenthesis process and clones, nor can it write multiple outputs for a given input. The ^ is the correct way and -gravity center is correct. You will have to loop over each input image. I do not think you can use wild cards to process more than one image at a time with this type of command. I think -path is only for mogrify.
I would write a loop over each of your input images (bash unix syntax):
cd
cd /Users/user/Resized/
list=`ls`
for img in $list; do
name=`convert "$img" -format "%t" info:`
convert "$img" \
\( -clone 0 -resize "1571x2646^" -gravity center -crop 1571x2646+0+0 +repage +write ${name}_1571x2646.png +delete \) \
\( -clone 0 -resize "1350x2150^" -gravity center -crop 1350x2150+0+0 +repage +write ${name}_1350x2150.png +delete \) \
\( -clone 0 -resize "1281x2039^" -gravity center -crop 1281x2039+0+0 +repage +write ${name}_1281x2039.png +delete \) \
null:
done
The above assumes that your input images have no spaces in their names.
I have changed from +clone to -clone 0, since I am not sure if you change aspect ratio from output to output whether that will cause problems. You can try both ways and see which looks best.
I was wondering if I can use imagemagick to produce a result image like this
My initial image will be like this
I am able to create the dark image to be put in the background like this -
convert -brightness-contrast -20x10 GARDENS-ILLUSTRATED_JAN-14.jpg out_lighter2.jpg
But I do not know how to put them in line like that and then generate a reflection.
I have got something close, but have no more time, so maybe you can fiddle with it and get it to what you want:
#!/bin/bash
input=input.png
# Calculate width and height
w=$(convert $input -ping -format "%w" info:)
h=$(convert $input -ping -format "%h" info:)
# Calculate how much to chop off bottom and height of reflection
chop=$(echo $h*60/100|bc)
refl=$(echo $h*20/100|bc)
convert $input -alpha on \
\( +clone -flip -size ${w}x${refl} gradient:gray40-black -alpha off -compose CopyOpacity -composite \) \
-append -background white -compose Over -flatten -gravity South -chop 0x${chop} output1.png
# Darken and reduce for second layer
convert output1.png -brightness-contrast -25x8 -resize 90% output2.png
# Darken and reduce for third layer
convert output2.png -brightness-contrast -25x8 -resize 90% output3.png
# Make big new output canvas
neww=$(echo $w*1.6|bc)
newh=$(echo $h*1.6|bc)
# Splat images onto it with offsets
convert -size ${neww}x${newh} xc:transparent -page +0+80 output3.png \
-page +40+40 output2.png \
-page +80+0 output1.png -flatten output.png
My basic idea is to generate the bright, frontmost image first in output1.png, then darken and reduce it into output2.png and again into output3.png. Then composite them all together into output.png with a little offset.
whenever we apply some transformation using imagemagick convert command , it tries to ensure that the resulting image is of the same size as that of the original image . is there a way such that we get the whole rendered image with transparent/white background .
convert -verbose maanavulu_GIST-TLOTKrishna.tif \
-alpha set -matte -virtual-pixel transparent \
-distort perspective-projection '1.06,0,0.0,0,2.066,0.0,0.0,0.0' \
1.jpg
There's some tricks with fx & repage hinted at by the Distorting Usage documentation. I've found the easiest approach would be to set option:distort:viewport option to something large enough to capture the whole distortion, then -trim it to the finial size.
convert -verbose maanavulu_GIST-TLOTKrishna.tif \
-alpha set -matte -virtual-pixel transparent \
-set option:distort:viewport 1000x1000 \
-distort perspective-projection '1.06,0,0.0,0,2.066,0.0,0.0,0.0' \
-trim 1.jpg