I am generating a GIF from a sequence of JPEGs.
I am using the brightness-contrast option for each image like so:
convert -loop 0 -layers optimize
-delay 10 thing1.jpg -brightness-contrast 5x5
-delay 10 thing2.jpg -brightness-contrast 5x5
-delay 10 thing3.jpg -brightness-contrast 5x5
-delay 10 thing4.jpg -brightness-contrast 5x5
thing.gif
What I'm noticing is that the brightness and contrast changes seem to be applied cumulatively, with the first image being the most affected. What I'm looking for is to apply the same brightness-contrast adjustment to all images, but instead, the first image appears to be increased by 40%, the second by 30%, etc.
Has anyone else experience this? Is there a way to apply the same change equally to all individual images that are used to build the GIF?
See my answers for these questions:
ImageMagick Command-Line Option Order (and Categories of Command-Line Parameters)
ImageMagick “color to alpha”
Because -brightness-contrast is an image operator (not an image setting) it is applied immediately to all currently loaded images (and then forgotten):
When you apply it the first time, only thing1.jpg is loaded. The operator is applied to this one image.
When you apply it the second time, thing2.jpg is loaded, but also the (already modified!) thing1.jpg is still loaded. The operator is applied to both these images.
To explain how your + my versions of the command works, be aware of this:
-loop 0 : is an image setting
-delay 10 : is an image setting
-brightness-contrast 5x5 : is an image operator
-layers optimize : is an image sequence operator
Therefor, you should try this:
convert -loop 0 \
-delay 10 \
thing1.jpg \
thing2.jpg \
thing3.jpg \
thing4.jpg \
-brightness-contrast 5x5 \
-layers optimize \
thing.gif
If you need to apply different values, but non-accumulatively, controlling each level of brightness-contrast separately, you should use the \(.....\) bracketing for 'aside'-processing of images:
convert -loop 0 \
\( thing1.jpg -delay 10 -brightness-contrast 5x5 \) \
\( thing2.jpg -delay 20 -brightness-contrast 10x20 \) \
\( thing3.jpg -delay 100 -brightness-contrast 10% \) \
\( thing4.jpg -delay 1 -brightness-contrast 0x50 \) \
-layers optimize \
thing.gif
Related
I have a set of images, and I can use the Imagemagick montage command on them to produce a montage image file with transparent background (let's call this fgimg). Now I have another existing image (let's call this bgimg) that I'd like to use (after some special processing with the convert command) as the background for fgimg, which can be achieved within the same convert command. At this point it seems trivial to avoid writing the temporary fgimg to disk, simply by piping the standard output of montage to the standard input of convert.
My problem is that the special processing I'm applying to bgimg will require some knowledge of the image properties of fgimg (e.g., resizing bgimg to have the same size as fgimg), which I don't know in advance. How can this information be retrieved and used in the convert command?
Note: I'm using Imagemagick version 6.9.7-4 on Linux.
I'll include some commands below to further illustrate the problem in detail.
The following command produces the montage image fgimg from a set of input images. The output is in the 'special' miff format (which seems best for temporary output to be worked on later), and has transparent background so that the actual background can be applied later. Most of the other options here are not important, but the point is that the output size (dimensions) cannot be determined in advance.
montage input_*.jpg -tile 5x -border 2 -geometry '200x200>+20+20' \
-gravity center -set label '%f\n%G' -background none -fill white \
-title 'Sample Title' miff:fgimg
Next, I have another input image bgimg.jpg. I want to perform some processing on it before using it as background to fgimg. The processing can be quite complex in general, but in this example, I want to:
resize bgimg.jpg to fit inside the dimensions of fgimg without any cropping;
apply a fade-to-black effect around the edges;
make it the same size as fgimg, with a black background;
combine this with fgimg to produce the final output.
Notice that I need the size of fgimg in two places. I can first extract this into a shell variable:
size=$(identify -format '%G' miff:fgimg)
Then I can do all the steps above in one convert command (note that $size is used twice):
convert "bgimg.jpg[$size]" -gravity center \
\( +clone -fill white -colorize 100% -bordercolor black \
-shave 20 -border 20 -blur 0x20 \) -compose multiply -composite \
-background black -compose copy -extent $size \
miff:fgimg -compose over -composite final_out.jpg
Now here is the problem: I want to avoid writing the temporary file fgimg to disk.
I could replace miff:fgimg with miff:- in both the montage and convert commands and then just pipe one to the other: montage ... | convert .... But how do I deal with the $size?
I tried to use file descriptors (miff:fd:3) but this does not seem to work, which is confirmed by the comments to this question.
Is there a way to do this (in Imagemagick v6) without creating a temporary file?
This example command uses ImageMagick v6 on a bash shell. Instead of "montage" it starts by using "convert" to create a "logo:", one of IM's built-in sample images, then pipes it out as a MIFF and into the "convert" command that follows. You can pipe the output of "montage" just as easily. And it uses another IM built-in image "rose:" as your "bgimg.jpg"...
convert logo: miff:- | convert - rose: \
+distort SRT "%[fx:t?min(u.w/v.w,u.h/v.h):1] 0" \
-shave 1 +repage -gravity center -bordercolor black \
\( -clone 1 -fill white -colorize 100 -shave 6 -border 6 \
-blur 0x6 -clone 1 -compose multiply -composite \) -delete 1 \
\( -clone 0 -alpha off -fill black -colorize 100 \
-clone 1 -compose over -composite \) -delete 1 \
+swap -composite final_out.jpg
That reads the piped image "-" and the background image "rose:".
Then it uses "+distort" with an FX expression to scale "rose:" to the maximum dimensions that still fit within the original piped input image. That operation adds a pixel all around so we use "-shave 1" to get rid of that.
Next inside parentheses it clones that re-scaled background image, makes an edge blur mask, and composites them to make the fade-to-black edge on the background image. Right after the parentheses it deletes the non-edged background image.
In the next parentheses it clones the input image, makes it black, clones the modified background image, and composites it centered over the black one. Again the non-extended background image is discarded after the parentheses with "-delete 1".
Finally the modified background and the input image are put in the proper order with "+swap" and composited for the final output. Run this command without the last "-composite" to see the two images that result from the prior parts of the command.
Both the main input image and background image can be any size, any dimensions, and any aspect ratio. This works for me on v6.8.9 on a bash shell. It should work on any ImageMagick newer. It should work on Windows by removing all the backslashes that escape parentheses and changing the continued-line backslashes "\" to carets "^".
EDITED TO ADD:
You can use that FX expression to find the scaling amount, save it as a variable, then isolate the background image inside parentheses and use that variable to do the scaling and shaving there. That way it only affects the background image. There may be a rounding error with that, but the main image, which determines the exact final output dimensions, will be unaffected. Note the difference in the first few lines of this command...
convert logo: miff:- | convert - rose: \
-set option:v1 "%[fx:min(u.w/v.w,u.h/v.h)]" \
\( -clone 1 +distort SRT "%[v1] 0" -shave 1 \) -delete 1 \
+repage -gravity center -bordercolor black \
\( -clone 1 -fill white -colorize 100 -shave 6 -border 6 \
-blur 0x6 -clone 1 -compose multiply -composite \) -delete 1 \
\( -clone 0 -alpha off -fill black -colorize 100 \
-clone 1 -compose over -composite \) -delete 1 \
+swap final_out.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.
Given an animated gif over a solid background color
I'd like to trim away the padding. Concretely, I'd like to crop the image to the maximum extent of the foreground object over all frames:
I can't seem to find the right combination of -alpha, -background to achieve this with a single convert command. For example, if I issue
convert -dispose 2 input.gif -trim -layers TrimBounds fail.gif
I get random "background" colors for frames whose individual trimmed extents are smaller than the maximum extent over all frames:
I can achieve the correct output with a long string of commands:
convert input.gif -trim -layers TrimBounds out-%03d.miff
mogrify -background "rgb(20%,30%,80%)" -layers flatten out-*.miff
convert out-*.miff output.gif
rm out-*.miff
This is slow, writes a bunch of temporary files, and requires me to know the background color ("rgb(20%,30%,80%)") explicitly.
Is there a simpler way to trim an animated gif?
This related question considers explicit cropping rather than automatic trimming.
Finally, this seems to work in ImageMagick with one line to get the background color and one line of processing. No temp files are needed.
bgcolor=`convert input.gif[0] -format "%[pixel:u.p{0,0}]" info:`
convert -dispose previous -delay 10 -background "$bgcolor" input.gif -trim -layers TrimBounds -coalesce -layers optimize -loop 0 output.gif
You can accomplish this sort of trimming using IM's "-distort" with a defined viewport.
convert oHBWq.gif -coalesce +repage -background none \
\( -clone 0--1 -trim -flatten -trim \) \
-set option:distort:viewport %[fx:u[-1].w]x%[fx:u[-1].h]+%[fx:u[-1].page.x]+%[fx:u[-1].page.y] \
-delete -1 -distort SRT 0 +repage output.gif
That clones the input frames, trims them individually, and flattens them keeping their original alignment. Then it trims that flattened one again to get rid of the excess transparent background. The result will be the right size and have the correct page offsets for the finished images. You don't have to know the background color.
Now you can easily get those dimensions and offsets into a distort viewport setting and do a no-op distort. Delete the cloned flattened one that was used to get the measurements, "+repage" the rest, and finish with whatever other GIF settings you need.
This is an interesting question. At the moment, I do not see how to improve it so that extra file(s) are not needed. But I will consider it further. But I can clean your code up a bit and make it easier for you and make the output.gif look correct.
bgcolor=`convert oHBWq.gif[0] -format "%[pixel:u.p{0,0}]" info:`
convert oHBWq.gif -trim -layers TrimBounds out-%03d.miff
mogrify -background "$bgcolor" -layers flatten out-*.miff
convert -dispose previous -delay 10 out-*.miff -loop 0 output.gif
rm out-*.miff
This does the same thing as above, but only requires saving 1 multi-frame miff file. The subshell loop processing does similar to your mogrify.
bgcolor=`convert oHBWq.gif[0] -format "%[pixel:u.p{0,0}]" info:`
num=`convert oHBWq.gif -format "%n\n" info: | head -n 1`
convert oHBWq.gif -trim -layers TrimBounds tmp.miff
(for ((i=0; i<num; i++)); do
convert tmp.miff[$i] -background "$bgcolor" -layers flatten miff:-
done ) |\
convert -dispose previous -delay 10 - -loop 0 output2.gif
rm tmp.miff
This also works without having to save any temp files, but is has to repeat the -trim -layers trim bounds for each loop iteration.
bgcolor=`convert oHBWq.gif[0] -format "%[pixel:u.p{0,0}]" info:`
num=`convert oHBWq.gif -format "%n\n" info: | head -n 1`
echo "num=$num"
(for ((i=0; i<num; i++)); do
convert oHBWq.gif -trim -layers TrimBounds miff:- |\
convert -[$i] -background "$bgcolor" -layers flatten miff:-
done ) |\
convert -dispose previous -delay 10 - -loop 0 output3.gif
This is close but for one frame:
bgcolor=`convert oHBWq.gif[0] -format "%[pixel:u.p{0,0}]" info:`
convert -dispose previous -delay 10 oHBWq.gif -trim -layers TrimBounds -background "$bgcolor" -layers optimize -loop 0 output5.gif
I want to make an animated gif from those .png image:
I do it with this command:
convert -layers OptimizePlus -delay 25x100 ps1-*.png -loop 0 ps1.gif
It made an animated gif successfully, however, the output has very low quality and smaller than input images:
After some search, I got -quality
convert -layers OptimizePlus -delay 25x100 -quality 99 ps1-*.png -loop 0 ps1.gif
But it seems like imagemagick just ignore the parameter.
The problem is that your source PNGs have an alpha channel which is not supported by GIFs. So you have to remove transparency from your source images first. Since you're dealing with multiple source images, you can't use the -flatten method. With newer ImageMagick versions the following should work:
convert -background white -alpha remove -layers OptimizePlus -delay 25x100 ps1-*.png -loop 0 ps1.gif
If your version of ImageMagick is older than 6.7.5, you can try:
convert -bordercolor white -border 0 -layers OptimizePlus -delay 25x100 ps1-*.png -loop 0 ps1.gif
I got the following result with the latter command:
I'm looking for the most efficient way to stitch multiple images together in ImageMagick, on top of a background image, such that the spacing / padding between the overlaid images is consistent?
I've investigated use of +append, convert -composite, and convert with -page and -layers merge.
The following command (convert -composite) works, but requires precalculation of image dimensions in order to specify absolute offsets. Really, I want a 10 pixel gap between the end of the FIRST layered image and the start of the second layered image, but the only way I can see to achieve that is by specifying the absolute offset from the top-left corner of the canvas.
convert \
background.jpg \
first.jpg -gravity Northwest -geometry +10+10 -composite \
second.jpg -geometry +300+10 -composite \
third.jpg -geometry +590+10 -composite \
output.jpg
I am looking for some sort of operator so that the horizontal offset can be interpreted relative to the "last" image in the layering, so instead of specifying +300+10 for the second image and +590+10 for the third, I can somehow specify a +10+10 offset.
I thought gravity would allow me to achieve that (-gravity Northwest), in the same way that float: left; works in CSS positioning, but that is not the case.
I have also had some success with the following:
convert \
-page +10+10 first.jpg \
-page +300+10 second.jpg \
-page +590+10 third.jpg \
-background transparent \
-layers merge \
layered.png
convert background.jpg layered.png -gravity Center -composite output.jpg
Both the techniques described require pre-calculation of absolute offsets, which is a bit of a pain. Is there a better way to do this?
You've overlooked the montage command.
The most simple command to add the wanted spacing with it would be to set a -frame 5 option with -mattecolor none. This works with images of different width values and spaces them all apart with a distance of 10 pixels:
montage \
-alpha on \
-background none \
-mode concatenate \
-tile x1 \
-frame 5 \
-mattecolor none \
*.jpg \
output1.png
You'll easily notice however, that the resulting image's border is only 5 pixels wide on top, right, bottom and left. To remove these 5 pixels all around use:
convert output1.png -shave 5 output2.png
To overlay this result on your background.jpg, use:
convert \
background.jpg \
output2.png \
-gravity Northwest \
-geometry +10+10 \
-composite \
final.jpg
You can also use Kurt's transparent frame trick with append. Using append instead of montage has the advantage that you can use gravity settings to align your images top (north) bottom (south) or center.
Here's an example of how to append images horizontally with a 10 pixel gap between them, and with the images top aligned:
convert \
-frame 5 \
-mattecolor none \
-background none \
-gravity north \
first.jpg second.jpg third.jpg \
+append \
png:- | convert - -shave 5
output.png
To append images vertically use -append instead of +append. I've used a pipe | to shave off the outside frame in the same command.