Focus Bracketing[]

Written for/on: Canon A640
Also works on: Theoretically all Other Canon AXXX (Not all tested),
Tested and working on: PowerShot S3, PowerShot A590IS, Powershot A560, SD600/Ixus 60
Doesn't work on: Ixus 850 IS (crashes camera)

Focus Bracketing by Johan Van Barel (

The Canon A640 has an impressive macro mode. "Disadvantage" can be the very limited DOF (Depth Of Field). The purpose of this script is to take a set of images at different focus settings around the current focus.

So if you take 3 pictures, it will take the first picture at a focus smaller then the current one, the second at the current focus, and the third at a focus bigger then the current one. The steps are calculated so that they are not linear and thus more natural. You can later stitch the images together with an image stacking program like CombineZM (

Documentation/Help (save as a small FocusBracket.txt" file to your /CHDK/SCRIPTS/ folder)

Select P, Tv, Av, M or C mode on your Canon A640.

Set the focus to manual focus and focus your image.

(Note: Some cameras, for example the SD600 don't have a manual focus mode, however focusing the image in the normal way for the first shot seems to work, as the subsequent focusing is done by the script).


  • Number of Pics (Odd) (Default=5)
  • Focus Step Size (x6) (Default=1)
  • Initial Delay (Sec) (Default=1)

Script Code (save as "FocusBracket.bas" to your /CHDK/SCRIPTS/ folder)

rem Johan Van Barel 23-07-2007
rem Focus-Bracketing for Canon A640

@title Focus Bracket (Set Focus Manual)
@param a Number of Pics (Odd)
@default a 5
@param b Focus Step Size (x6)
@default b 1
@param c Initial Delay (Sec)
@default c 1

if a<1 then let a=1
if b<1 then let b=1
if c<0 then let c=0

print "Pics:"; a*2+1 , "Step:"; b

sleep c*1000

get_focus f

for s=-a to a
    if s<0 then gosub "negative"
    if s>0 then gosub "positive"
    print "Pic", s+a+1, "Focus", g; "mm"
    if g<62 then let g=62
    if g>65535 then let g=65535
    set_focus g
next s
set_focus f


    for i=s to -1
    next i

    for i=1 to s
    next i