{"id":115,"date":"2023-04-16T01:58:00","date_gmt":"2023-04-16T01:58:00","guid":{"rendered":"https:\/\/renegrothmann.de\/?p=115"},"modified":"2024-02-24T11:14:43","modified_gmt":"2024-02-24T11:14:43","slug":"dof-calculator-in-python","status":"publish","type":"post","link":"https:\/\/renegrothmann.de\/?p=115","title":{"rendered":"DOF Calculator in Python"},"content":{"rendered":"\n<p>Another posting from my old blog.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>I have updated my&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/euler-math-toolbox.de\/Programs\/Examples\/DOF%20of%20an%20ideal%20lens.html\" target=\"_blank\">example in Euler Math Toolbox for the computation of DOF&nbsp;<\/a>(depth of field). Below is a version of Python, which you can save in a file like \u201edof.py\u201c.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\n\nm=1\nmm=1\/1000\ncm=1\/100\nuseasinf=1e5\n\n\"\"\"\nThese functions compute DOF, hyperfocal distance and Bokeh\nof an ideal lens for photographers.\n\nTry: \nlens(D,F,A)\n\nD : focussed distance\nF : true focal length\nA : true aperture (F-stop)\n\nAdd:\nff : form factor (aka crop factor) (def. 1)\nc : accepted circle of diffusion (def. 0.03mm)\nequiv : if false, D and F are true values times ff (def. False)\n\"\"\"\n\ndef endDOF (D, F, A, c=0.03*mm, ff=1, inf=useasinf):\n    res = ff*D*F**2\/(ff*F**2+c*A*F-c*ff*A*D)\n    if res&lt;0:\n        res=inf\n    return res\n\ndef startDOF (D, F, A, c=0.03*mm, ff=1):\n    return ff*D*F**2\/(ff*F**2-c*A*F+c*ff*A*D)\n\ndef hyperfocal (F, A, c=0.03*mm, ff=1):\n    return F*(ff*F+c*A)\/(c*ff*A);\n\ndef bokeh (D, F, A, ff=1):\n    return 2*F**2\/(A*(D-F))\/math.sqrt(0.024**2+0.036**2)\n\ndef lens (D, F, A, c=0.03*mm, ff=1, equiv=False):\n    \"\"\"\n    Prints DOF, hyperfocal distance and Bokeh\n    \n    D : focused distance\n    F : true focal length\n    A : true aperture (F-stop)\n\n    ff : form factor (aka crop factor) (def. 1)\n    c : accepted circle of diffusion (def. 0.03mm)\n    equiv : if false, D and F are true values times ff (def. False)\n    \"\"\"\n    if ff!=1:\n        print(f\"{'Crop factor':30} : {ff:10.1f}\")\n    if not equiv:\n        F = F*ff\n        A = A*ff\n    if ff!=1:\n        print(f\"{'True focal length':30} : {F\/ff*1000:10.1f} mm\")\n    print(f\"{'Equivalent focal length':30} : {F*1000:10.1f} mm\")\n    if ff!=1:\n        print(f\"{'True F-stop':30} : F {A\/ff:10.1f}\")\n    print(f\"{'Equivalent F-stop':30} : F {A:10.1f}\")\n    print(f\"{'Focussed distance':30} : {D:10.2f} m\")\n    print(\"\")\n    sd = startDOF(D,F,A,c,ff)\n    ed = endDOF(D,F,A,c,ff)\n    if ed>=useasinf:\n        print(f\"{'Start of DOF':30} : {sd:10.2f} m\")\n        print(f\"{'End of DOF':30} : infinity\")\n    elif D&lt;1:\n        print(f\"{'Start of DOF':30} : {sd*100:10.1f} cm\")\n        print(f\"{'End of DOF':30} : {ed*100:10.1f} cm\")\n        print(f\"{'Total DOF':30} : {(ed-sd)*100:10.1f} cm\")\n    else:\n        print(f\"{'Start of DOF':30} : {sd:10.2f} m\")\n        print(f\"{'End of DOF':30} : {ed:10.2f} m\")\n        print(f\"{'Total DOF':30} : {ed-sd:10.2f} m\")\n    print(\"\")\n    print(f\"{'Bokeh':30} : {bokeh(D,F,A,ff)*100:10.2f} %\")<\/code><\/pre>\n\n\n\n<p>Here are some examples.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&gt;lens(2,85*mm,4)\n\nEquivalent focal length        :       85.0 mm\nEquivalent F-stop              : F        4.0\nFocussed distance              :       2.00 m\n\nStart of DOF                   :       1.94 m\nEnd of DOF                     :       2.07 m\nTotal DOF                      :       0.13 m\n\nBokeh                          :       4.36 %\n\n&gt;lens(2,42.5*mm,2,ff=2)\n\nCrop factor                    :        2.0\nTrue focal length              :       42.5 mm\nEquivalent focal length        :       85.0 mm\nTrue F-stop                    : F        2.0\nEquivalent F-stop              : F        4.0\nFocussed distance              :       2.00 m\n\nStart of DOF                   :       1.94 m\nEnd of DOF                     :       2.07 m\nTotal DOF                      :       0.13 m\n\nBokeh                          :       4.36 %\n\n&gt;lens(4,85*mm,4,ff=2)\n\nCrop factor                    :        2.0\nTrue focal length              :       85.0 mm\nEquivalent focal length        :      170.0 mm\nTrue F-stop                    : F        4.0\nEquivalent F-stop              : F        8.0\nFocussed distance              :       4.00 m\n\nStart of DOF                   :       3.87 m\nEnd of DOF                     :       4.13 m\nTotal DOF                      :       0.26 m\n\nBokeh                          :       4.36 %<\/code><\/pre>\n\n\n\n<p>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. <\/p>\n\n\n\n<p>Dieser Beitrag wurde am&nbsp;<a href=\"http:\/\/observations.rene-grothmann.de\/dof-calculator-in-python\/\"><time datetime=\"2022-03-10T15:58:53+01:00\">2022\/03\/10<\/time><\/a>&nbsp;ver\u00f6ffentlicht.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another posting from my old blog. I have updated my&nbsp;example in Euler Math Toolbox for the computation of DOF&nbsp;(depth of field). Below is a version of Python, which you can save in a file like \u201edof.py\u201c. Here are some examples. The Bokeh in percentage just show the proportion between the diameters of the circles of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/renegrothmann.de\/index.php?rest_route=\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/renegrothmann.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renegrothmann.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renegrothmann.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/renegrothmann.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":9,"href":"https:\/\/renegrothmann.de\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/renegrothmann.de\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/renegrothmann.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renegrothmann.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renegrothmann.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}