Another posting from my old blog.


I have updated my example in Euler Math Toolbox for the computation of DOF (depth of field). Below is a version of Python, which you can save in a file like „dof.py“.

import math

m=1
mm=1/1000
cm=1/100
useasinf=1e5

"""
These functions compute DOF, hyperfocal distance and Bokeh
of an ideal lens for photographers.

Try: 
lens(D,F,A)

D : focussed distance
F : true focal length
A : true aperture (F-stop)

Add:
ff : form factor (aka crop factor) (def. 1)
c : accepted circle of diffusion (def. 0.03mm)
equiv : if false, D and F are true values times ff (def. False)
"""

def endDOF (D, F, A, c=0.03*mm, ff=1, inf=useasinf):
    res = ff*D*F**2/(ff*F**2+c*A*F-c*ff*A*D)
    if res<0:
        res=inf
    return res

def startDOF (D, F, A, c=0.03*mm, ff=1):
    return ff*D*F**2/(ff*F**2-c*A*F+c*ff*A*D)

def hyperfocal (F, A, c=0.03*mm, ff=1):
    return F*(ff*F+c*A)/(c*ff*A);

def bokeh (D, F, A, ff=1):
    return 2*F**2/(A*(D-F))/math.sqrt(0.024**2+0.036**2)

def lens (D, F, A, c=0.03*mm, ff=1, equiv=False):
    """
    Prints DOF, hyperfocal distance and Bokeh
    
    D : focused distance
    F : true focal length
    A : true aperture (F-stop)

    ff : form factor (aka crop factor) (def. 1)
    c : accepted circle of diffusion (def. 0.03mm)
    equiv : if false, D and F are true values times ff (def. False)
    """
    if ff!=1:
        print(f"{'Crop factor':30} : {ff:10.1f}")
    if not equiv:
        F = F*ff
        A = A*ff
    if ff!=1:
        print(f"{'True focal length':30} : {F/ff*1000:10.1f} mm")
    print(f"{'Equivalent focal length':30} : {F*1000:10.1f} mm")
    if ff!=1:
        print(f"{'True F-stop':30} : F {A/ff:10.1f}")
    print(f"{'Equivalent F-stop':30} : F {A:10.1f}")
    print(f"{'Focussed distance':30} : {D:10.2f} m")
    print("")
    sd = startDOF(D,F,A,c,ff)
    ed = endDOF(D,F,A,c,ff)
    if ed>=useasinf:
        print(f"{'Start of DOF':30} : {sd:10.2f} m")
        print(f"{'End of DOF':30} : infinity")
    elif D<1:
        print(f"{'Start of DOF':30} : {sd*100:10.1f} cm")
        print(f"{'End of DOF':30} : {ed*100:10.1f} cm")
        print(f"{'Total DOF':30} : {(ed-sd)*100:10.1f} cm")
    else:
        print(f"{'Start of DOF':30} : {sd:10.2f} m")
        print(f"{'End of DOF':30} : {ed:10.2f} m")
        print(f"{'Total DOF':30} : {ed-sd:10.2f} m")
    print("")
    print(f"{'Bokeh':30} : {bokeh(D,F,A,ff)*100:10.2f} %")

Here are some examples.


>lens(2,85*mm,4)

Equivalent focal length        :       85.0 mm
Equivalent F-stop              : F        4.0
Focussed distance              :       2.00 m

Start of DOF                   :       1.94 m
End of DOF                     :       2.07 m
Total DOF                      :       0.13 m

Bokeh                          :       4.36 %

>lens(2,42.5*mm,2,ff=2)

Crop factor                    :        2.0
True focal length              :       42.5 mm
Equivalent focal length        :       85.0 mm
True F-stop                    : F        2.0
Equivalent F-stop              : F        4.0
Focussed distance              :       2.00 m

Start of DOF                   :       1.94 m
End of DOF                     :       2.07 m
Total DOF                      :       0.13 m

Bokeh                          :       4.36 %

>lens(4,85*mm,4,ff=2)

Crop factor                    :        2.0
True focal length              :       85.0 mm
Equivalent focal length        :      170.0 mm
True F-stop                    : F        4.0
Equivalent F-stop              : F        8.0
Focussed distance              :       4.00 m

Start of DOF                   :       3.87 m
End of DOF                     :       4.13 m
Total DOF                      :       0.26 m

Bokeh                          :       4.36 %

The Bokeh in percentage just show the proportion between the diameters of the circles of blurriness and the diameter of the sensor. 4% looks somewhat creamy.

Dieser Beitrag wurde am  veröffentlicht.

Ein Gedanke zu “DOF Calculator in Python”

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert