EXIF change or FX calculation to return the shutter speed

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

EXIF change or FX calculation to return the shutter speed

Post by Bonzo »

I have been looking at the EXIF data returned from identify -verbose and in my case the returned aperture is 700/100 and shutter speed is 810/100.

To get the F number is easy 700/100 = F7 but the shutter speed is not so easy
Shutter speed. To convert this value to ordinary 'Shutter Speed'; calculate this value's power of 2, then reciprocal. For example, if value is '4', shutter speed is 1/(2^4)=1/16 second.
When I say not so easy I am looking at batch files and I can not use the ^ also the batch file seems to round automaticaly so 8.1 becomes 8 :(

Anyway can ImageMagick be modified to do the calculations or can somebody explain how to use FX to get my shutter speed from:

Code: Select all

identify -format %[EXIF:ShutterSpeedValue]
EDIT: I do not think I can use fx as the value returned from EXIF:ShutterSpeedValue is 810/100 I could probably use it if the value was 810 or 8.1

Out of interest the output from ImageMagick and exiv2 are:
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: EXIF change or FX calculation to return the shutter speed

Post by fmw42 »

In Unix do:

shutter=`identify -format "%[EXIF:ShutterSpeedValue]"`
speed=`convert xc: -format "%[fx:$shutter]" info:`

if shutter=810/100, the result is 8.1

If you want 1/(2^speed), then

shutter=`identify -format "%[EXIF:ShutterSpeedValue]"`
speed=`convert xc: -format "%[fx:$shutter]" info:`
shutterspeed=`convert xc: -format "%[fx:1/(2^$speed)]" info:`


To convert to windows, see http://www.imagemagick.org/Usage/api/#windows, especially about using ^ as an escape so that ^ -> ^^

Alternately without ^, do:

shutter=`identify -format "%[EXIF:ShutterSpeedValue]"`
speed=`convert xc: -format "%[fx:$shutter]" info:`
shutterspeed=`convert xc: -format "%[fx:1/(pow(2,$speed))]" info:`


see
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: EXIF change or FX calculation to return the shutter speed

Post by Bonzo »

Thanks for the answer Fred; I was trying to use this in a windows bat file which has limited functions and I do not now what I am doing at the moment!

I will have a look again when I get a bit of spair time.

Code: Select all

FOR /F %%x IN ('identify -format "%%[EXIF:ShutterSpeedValue]" %INPUTFILE%') DO SET shutter=%%x
:: Output = 810/100
FOR /F %%x IN ('convert xc: -format "%%[fx:%shutter%]" info:') DO SET speed=%%x
:: Output = 8.1
FOR /F %%x IN ('convert xc: -format "%%[fx:1/(pow(2,%speed%))]" ') DO SET shutterspeed=%%x
:: Fails here with Convert: option requires an argument '-format'.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: EXIF change or FX calculation to return the shutter speed

Post by fmw42 »

Bonzo wrote:Thanks for the answer Fred; I was trying to use this in a windows bat file which has limited functions and I do not now what I am doing at the moment!

I will have a look again when I get a bit of spair time.

Code: Select all

FOR /F %%x IN ('identify -format "%%[EXIF:ShutterSpeedValue]" %INPUTFILE%') DO SET shutter=%%x
:: Output = 810/100
FOR /F %%x IN ('convert xc: -format "%%[fx:%shutter%]" info:') DO SET speed=%%x
:: Output = 8.1
FOR /F %%x IN ('convert xc: -format "%%[fx:1/(pow(2,%speed%))]" ') DO SET shutterspeed=%%x
:: Fails here with Convert: option requires an argument '-format'.
Looks like you left out info: in your last statement.

Should be:
FOR /F %%x IN ('convert xc: -format "%%[fx:1/(pow(2,%speed%))]" info:') DO SET shutterspeed=%%x

I am not a windows expert, but Anthony Thyssen recently updated his Windows API notes. If you have not seen them, then take a look at http://www.imagemagick.org/Usage/api/#windows, although it kind of looks like you are doing what he said there. You might also ask el_supremo. I believe that he supplied much information about bat file scripting to Anthony in a recent post.

Fred
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: EXIF change or FX calculation to return the shutter speed

Post by Bonzo »

Well spotted Fred - only one more small problem and that is I need to round the answer as I get 1/274.374 ( I want the speed as normal photographers use it ) and I want 1/274.

Anyway I am supposed to be going out in 1/2hr and I am not ready yet ! So will look at this again tomorrow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: EXIF change or FX calculation to return the shutter speed

Post by fmw42 »

Bonzo wrote:Well spotted Fred - only one more small problem and that is I need to round the answer as I get 1/274.374 ( I want the speed as normal photographers use it ) and I want 1/274.

Anyway I am supposed to be going out in 1/2hr and I am not ready yet ! So will look at this again tomorrow.
Try
FOR /F %%x IN ('convert xc: -format "%%[fx:1/floor((pow(2,%speed%)))]" info:') DO SET shutterspeed=%%x

this gives the value of 1/274=0.00364963. Is this what you want or do you want to actually list out 1/274?

In Unix, the latter would be:

shutter=`identify -format "%[EXIF:ShutterSpeedValue]"`
speed=`convert xc: -format "%[fx:$shutter]" info:`
speedpower=`convert xc: -format "%[fx:floor(pow(2,$speed))]" info:`
shutterspeed="1/$speedpower"
echo $shutterspeed

this reports 1/274
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: EXIF change or FX calculation to return the shutter speed

Post by Bonzo »

Thank you very much for the help Fred; the final code is below although I might try and "Tweek" it a bit. I need to modify the code as Pete suggested to allow for spaces in the file path.

You should be able to modify the conversions to annotate an image with any of the details returned from the data contained in the image.

The code is called in the command line in this case by : file.bat image.jpg

Code: Select all

:: Do not display the code while running
@ECHO OFF

:: Set the variable to the first argument
SET INPUTFILE=%1

:: Select the F number from the EXIF data
FOR /F %%x IN ('identify -format "%%[EXIF:FNumber]" %INPUTFILE%') DO SET FNumber=%%x

:: Image Magick returns the F number in the format of 700/100 - need to double check
:: Set the variable to the F number value
SET /A F_number=FNumber/100

:: Select the shutter speed from the EXIF data  Image Magick returns the shutter speed in the format of 810/100 or 40/10
:: Set the value of the variable to 810 in this case
FOR /F %%x IN ('identify -format "%%[EXIF:ShutterSpeedValue]" %INPUTFILE%') DO SET shutter=%%x

:: Format the speed to 8.1
FOR /F %%x IN ('convert xc: -format "%%[fx:%shutter%]" info:') DO SET speed=%%x

:: Calculate the speed in seconds using the power of 2
FOR /F %%x IN ('convert xc: -format "%%[fx:floor((pow(2,%speed%)))]"  info:') DO SET shutterspeed=%%x

:: Add the F number and shutter speed to the image
convert %INPUTFILE% ^
  -pointsize 18 -fill black -gravity northwest ^
  -annotate +0+0 "Aperture: F%F_number% Shutter speed 1/%shutterspeed% sec" watermarked_%INPUTFILE%
Image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: EXIF change or FX calculation to return the shutter speed

Post by Bonzo »

Modified code with extra comments:

Code: Select all

:: Do not display the code while running
@ECHO OFF

:: Set the INPUTFILE variable to the first argument
SET INPUTFILE=%1

:: Select the F number from the EXIF data and set the FNumber variable
FOR /F %%x IN ('identify -format "%%[EXIF:FNumber]" %INPUTFILE%') DO SET FNumber=%%x

:: Set the FNumber1 variable to the F number value
:: Image Magick returns the F number in the format of 700/100 or 40/10
FOR /F %%x IN ('convert xc: -format "%%[fx:%FNumber%]" info:') DO SET FNumber1=%%x

:: Set the value of the shutter variable to the shutter speed
:: Select the shutter speed from the EXIF data  Image Magick returns the shutter speed in the format of 810/100 
FOR /F %%x IN ('identify -format "%%[EXIF:ShutterSpeedValue]" %INPUTFILE%') DO SET shutter=%%x

:: Format the speed to 8.1 and set the speed variable
FOR /F %%x IN ('convert xc: -format "%%[fx:%shutter%]" info:') DO SET speed=%%x

:: Calculate the speed in seconds using the power of 2 and save it in the shutterspeed variable
FOR /F %%x IN ('convert xc: -format "%%[fx:floor((pow(2,%speed%)))]"  info:') DO SET shutterspeed=%%x

:: Add the F number and shutter speed to the image and save as exif_OriginalImageName
convert %INPUTFILE% ^
  -pointsize 16 -fill black -gravity northwest ^
  -annotate +10+5 "Aperture: F%FNumber1% Shutter speed: 1/%shutterspeed% sec" "exif_%INPUTFILE%"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: EXIF change or FX calculation to return the shutter speed

Post by fmw42 »

You might get some speed up by adding -ping before -format

see
http://www.imagemagick.org/Usage/basics/#controls
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: EXIF change or FX calculation to return the shutter speed

Post by Bonzo »

Not a very scientific timer - mobile phone - on a 3.11MB image:
Without -ping 7.1 seconds second test 4.9 seconds
With -ping 3.3 seconds second test 3.2 seconds

So there is a speed increase using -ping.
Post Reply