<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christos Kavallaris</title>
    <description>The latest articles on DEV Community by Christos Kavallaris (@christoskv).</description>
    <link>https://dev.to/christoskv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4267539%2Fde04ab28-cb3e-46c7-bf71-0d8f2ce29f52.png</url>
      <title>DEV Community: Christos Kavallaris</title>
      <link>https://dev.to/christoskv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/christoskv"/>
    <language>en</language>
    <item>
      <title>Drawing the real sun into a phone photo, to within 2 degrees</title>
      <dc:creator>Christos Kavallaris</dc:creator>
      <pubDate>Thu, 26 Nov 2026 22:36:54 +0000</pubDate>
      <link>https://dev.to/christoskv/drawing-the-real-sun-into-a-phone-photo-to-within-2-degrees-43n</link>
      <guid>https://dev.to/christoskv/drawing-the-real-sun-into-a-phone-photo-to-within-2-degrees-43n</guid>
      <description>&lt;p&gt;I make a Windows app for location scouts, the people who find streets and buildings for film shoots. They take hundreds of phone photos of a place, and the first question back at the office is always about light: &lt;em&gt;is that façade lit in the morning or the evening, and is the sun in shot when the crew can get there?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So the app has a key, &lt;strong&gt;S&lt;/strong&gt;, that draws the sun's path into the photo: today's arc with the hours marked, plus the June and December solstices and the equinox, so you can see how high and low the sun gets over the year.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9cjcdy47jpdw6bi2t7vt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9cjcdy47jpdw6bi2t7vt.jpg" alt="The sun's path drawn into a street photo in Athens: the day's arc in yellow with 7h and 8h marked, the 21 June arc in red, the 23 September arc in green, and the horizon as a green line" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first version looked right and was wrong. Against photos with the real sun in the frame it was &lt;strong&gt;4.3° off on average and 8.2° at worst&lt;/strong&gt;. That's more than the sun's own width many times over, enough to put it on the wrong side of a chimney. This post is about what it took to get to &lt;strong&gt;1.9° average, 2.8° worst&lt;/strong&gt;, roughly in the order each fix mattered.&lt;/p&gt;

&lt;h2&gt;
  
  
  The easy part: where the sun is
&lt;/h2&gt;

&lt;p&gt;The sun's azimuth and elevation for a place and a moment have a closed-form answer. I used the formulas behind NOAA's solar calculator (from Jean Meeus's &lt;em&gt;Astronomical Algorithms&lt;/em&gt;): Julian century, mean longitude and anomaly, equation of centre, obliquity, equation of time, hour angle, then a refraction correction near the horizon. It's about 60 lines of standard-library Python, with no dependencies and no network, so it works offline forever. It matches published sunrise and sunset times to within about two minutes.&lt;/p&gt;

&lt;p&gt;None of my errors came from there. They all came from the camera.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting the sun on a pixel
&lt;/h2&gt;

&lt;p&gt;Once you know where the sun is in the sky, you need to know where the camera was pointing and how wide it saw. The projection is a plain pinhole: turn the sun's direction into camera axes and divide by depth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;az&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cam&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;wrap180&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;az&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;cam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;yaw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pitch&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;ce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ce&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ce&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nc"&gt;Zc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;Z&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// depth after the pitch&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Zc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;1e-9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                  &lt;span class="c1"&gt;// behind the camera&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nc"&gt;Yc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="no"&gt;Z&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nc"&gt;Zc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Yc&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nc"&gt;Zc&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cam&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// roll, in the image plane&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fromTan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cam&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// tangents -&amp;gt; 0..1 of the frame&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My first version was linear in angle (&lt;code&gt;x = rel / hfov&lt;/code&gt;). That's fine for one dot near the middle of the frame, but an arc has to bend toward the edges, and a linear mapping can't do that. Two details matter here. A point behind the camera has to &lt;em&gt;break&lt;/em&gt; the line, not be clamped to the edge, or the arc draws a chord straight across the sky. And roll is the last rotation, applied in the image plane, so the horizon stays straight under any roll.&lt;/p&gt;

&lt;p&gt;The inputs are yaw (the compass heading), pitch, roll and the field of view. Each one turned out to be wrong in its own way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 1: the camera wasn't level
&lt;/h2&gt;

&lt;p&gt;The first version assumed the phone was held level. People don't hold it level, and scouts point down at streets and up at rooftops. The median tilt in my own test library was 8°, and every degree of pitch moves the sun a degree up or down the frame.&lt;/p&gt;

&lt;p&gt;It turns out an iPhone records which way was down. Apple's maker note, tag &lt;code&gt;0x0008&lt;/code&gt;, holds the gravity vector at the moment of capture, in g, in the phone's own axes. The lens looks along −Z, the sensor's down is −X and its right is −Y. I worked those axes out from real photographs, one held landscape and one portrait, because I couldn't find them documented anywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;attitude_from_gravity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orientation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;pitch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# gravity across the sensor, turned into the photo *as displayed*&lt;/span&gt;
    &lt;span class="n"&gt;du&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_DISPLAY_FROM_SENSOR&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;orientation&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hypot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;du&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="c1"&gt;# pointing almost straight up or down&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pitch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pitch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atan2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;du&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Axes you've guessed from two photos need checking, and I wanted a check that didn't share any assumptions with the code. Two consecutive frames taken from the same spot differ by a pure rotation. Match features between them (SIFT, then a homography &lt;code&gt;H&lt;/code&gt;), and the rotation falls out as &lt;code&gt;R = K⁻¹ H K&lt;/code&gt;, where &lt;code&gt;K&lt;/code&gt; is the camera matrix. Compare that with the rotation between the two gravity readings. Across 83 frame pairs they agreed to &lt;strong&gt;under a degree&lt;/strong&gt;. With roll's sign flipped the disagreement doubled, and with pitch's sign flipped it was twenty degrees. That's how I know the signs are right, not just that the tests pass.&lt;/p&gt;

&lt;p&gt;Front cameras look out of the other face of the phone, so their gravity isn't used. Mirrored EXIF orientations (2, 4, 5, 7) come from front cameras, so those are skipped too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 2: the field of view was 16° too wide
&lt;/h2&gt;

&lt;p&gt;EXIF gives you &lt;code&gt;FocalLengthIn35mmFilm&lt;/code&gt;, and I assumed that meant a frame 36 mm wide. It doesn't. A 35mm-equivalent focal length matches the &lt;strong&gt;diagonal&lt;/strong&gt;, 43.27 mm, and a phone's frame is 4:3, not 3:2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fov_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;aspect&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f35&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;43.27&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aspect&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;aspect&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# frame width at the photo's aspect&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;aspect&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a landscape 4:3 frame, assuming 36 mm across was two degrees too wide, a small error. For a portrait frame it was &lt;strong&gt;sixteen&lt;/strong&gt;, because the narrow side is the one across. There was a second bug hiding in the same place: I read the image size &lt;em&gt;before&lt;/em&gt; applying the EXIF orientation, so a portrait photo (orientation 6 or 8) reported a landscape aspect. You can't see that on a single dot in the middle. Once a whole arc is drawn, it's obvious.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 3: some clocks were an hour out
&lt;/h2&gt;

&lt;p&gt;The sun moves about 15° an hour, so the time zone has to be exactly right. Photos store local time. The first version read &lt;code&gt;OffsetTimeOriginal&lt;/code&gt; when the photo had it, and recent phones write it. Photos without it fell back to a guess from longitude, which put Athens in August an hour out. Now there are three sources, tried in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;OffsetTimeOriginal&lt;/code&gt;&lt;/strong&gt;. It's exact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The GPS block's own UTC timestamp&lt;/strong&gt;, compared with the local capture time. It's also exact, and it includes summer time, with no time-zone database needed. That matters because Python's &lt;code&gt;zoneinfo&lt;/code&gt; on Windows has no system data to read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Longitude&lt;/strong&gt;, as a last resort, and it's only a guess. It puts Athens in August at UTC+2 when it's +3, and Madrid at 0 when it's +2 all summer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The GPS clock gets a sanity check: the offset it implies has to land within minutes of an offset that actually exists somewhere. Otherwise a stale GPS fix can invent a time zone of +3:43. When the app does fall back to longitude, the overlay says "time zone guessed", because no calibration can fix that error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 4: true north
&lt;/h2&gt;

&lt;p&gt;The sun is placed against true north, but some phones write the heading against magnetic north (&lt;code&gt;GPSImgDirectionRef = "M"&lt;/code&gt;). My iPhone writes true north, so this one didn't change my numbers, but other phones need it. I implemented the World Magnetic Model (WMM2025), again in standard-library Python, and tested it against NOAA's hundred published test values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking it against the real sun
&lt;/h2&gt;

&lt;p&gt;Unit tests can tell you the code does what you meant. They can't tell you that what you meant is right. So I measured against the actual sun. I picked five photos with the sun in the frame, found the saturated blob (connected components on pixels where every channel is at least 250), and compared its centre with where the overlay put it. The overlay was rendered by the app's real front end in headless Playwright, not by a reimplementation.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Mean error&lt;/th&gt;
&lt;th&gt;Worst&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First version (level camera, 36 mm across)&lt;/td&gt;
&lt;td&gt;4.3°&lt;/td&gt;
&lt;td&gt;8.2°&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;With the phone's tilt and roll, and the diagonal field of view&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.9°&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2.8°&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's with no calibration at all, straight from what the phone recorded. These five photos all had &lt;code&gt;OffsetTimeOriginal&lt;/code&gt;, so the time-zone fix doesn't show up in this table. It matters for photos that don't carry it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's left: the compass
&lt;/h2&gt;

&lt;p&gt;Most of the remaining error is the phone's compass: 2–3° per photo as a rule, with occasional jumps of 8–13° near metal. The worst photos in my set were taken from a balcony with an iron railing.&lt;/p&gt;

&lt;p&gt;You can't fix that from the photo's data, so the app lets you fix it by hand. If the sun is in the frame, click it, and the heading is solved exactly from the sun's known position. If it isn't, draw along a shadow on level ground. The shadow version has a trap I walked straight into: when the sun is in front of the camera, shadows run &lt;em&gt;toward&lt;/em&gt; you, and their vanishing point is the sun's own, not the point opposite it. Pick the wrong one and the heading comes out 180° off, for half of all photos. The solver now checks which way the drawn shadow runs relative to the horizon before choosing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I'd tell myself at the start
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Measure against the world, not against your own expectations.&lt;/strong&gt; Each of these bugs had passing tests. The frame-pair rotations and the real-sun photos are what found them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Draw the whole curve.&lt;/strong&gt; A single dot near the centre hid the projection bug, the portrait field of view and the orientation swap. All three showed up as soon as an arc ran edge to edge.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Render once.&lt;/strong&gt; "Save annotated copy" re-renders the same SVG at full size in the browser and only composites it on the Python side. A second renderer in Pillow would slowly drift from the one on screen, and the whole point is that the saved file matches what you checked.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The app is &lt;a href="https://locationsorter.eu/guides/sun-position-from-a-photo/" rel="noopener noreferrer"&gt;Location Sorter&lt;/a&gt;. It sorts folders of phone photos into places by GPS, and the sun overlay is one part of it. It's Windows-only, in the Microsoft Store, with a 7-day free trial. I built it with Claude Code as a pair programmer, and Claude helped draft this post. The measurements, the photos and the mistakes are mine.&lt;/p&gt;

&lt;p&gt;Happy to answer questions about any of the maths, or the parts of iPhone EXIF that aren't documented.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>python</category>
      <category>javascript</category>
      <category>math</category>
    </item>
    <item>
      <title>My NAS disks never rested. The cause was Docker healthchecks.</title>
      <dc:creator>Christos Kavallaris</dc:creator>
      <pubDate>Thu, 26 Nov 2026 18:48:56 +0000</pubDate>
      <link>https://dev.to/christoskv/my-nas-disks-never-rested-the-cause-was-docker-healthchecks-487o</link>
      <guid>https://dev.to/christoskv/my-nas-disks-never-rested-the-cause-was-docker-healthchecks-487o</guid>
      <description>&lt;p&gt;I run about 90 containers in 60 stacks on a Synology DS923+: four hard drives, btrfs, RAID 5. For months the drives ticked all night. Disk 1 sat at &lt;strong&gt;77–88 % busy&lt;/strong&gt; while nothing was happening, and the IO load average hovered between 5 and 8.&lt;/p&gt;

&lt;p&gt;It wasn't Jellyfin, Immich or a backup job. It was healthchecks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a healthcheck writes to disk
&lt;/h2&gt;

&lt;p&gt;Every time Docker runs a healthcheck probe, it records the result in the container's state and saves that state to disk. That's a small write plus an fsync. One container probing every 30 seconds is nothing.&lt;/p&gt;

&lt;p&gt;But many compose examples ship with 5- or 10-second intervals, and once you have forty containers with healthchecks you have a steady stream of small synchronous writes. On btrfs (copy-on-write, checksummed metadata) over RAID 5 (a partial-stripe write means read, modify, write), each of those tiny writes turns into several physical ones. The disks never get to rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure your own
&lt;/h2&gt;

&lt;p&gt;List every container's healthcheck interval:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps -q &lt;span class="o"&gt;|&lt;/span&gt; xargs docker inspect \
  --format &lt;span class="s1"&gt;'{{.Name}} {{if .Config.Healthcheck}}{{.Config.Healthcheck.Interval}}{{else}}none{{end}}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0s&lt;/code&gt; means Docker's default of 30 seconds. Add up &lt;code&gt;60 / interval&lt;/code&gt; for every container and you have your probes per minute. Mine was &lt;strong&gt;232&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then watch the disks for a few minutes with nothing else running: &lt;code&gt;iostat -x 5&lt;/code&gt; if you have it, or the disk utilisation graph in your NAS's resource monitor.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Give everything that doesn't need second-by-second detection a 60-second interval:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"CMD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"wget"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-qO-"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"http://localhost:8080/health"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;60s&lt;/span&gt;
  &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
  &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;60s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;start_period&lt;/code&gt; keeps a slow first start from counting as a failure. If your Docker Engine is version 25 or newer, &lt;code&gt;start_interval: 5s&lt;/code&gt; lets it probe quickly during that start period only.&lt;/p&gt;

&lt;p&gt;Don't delete the healthchecks. They're what &lt;code&gt;depends_on: condition: service_healthy&lt;/code&gt; waits for, and what tells you a container is up but not actually working.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;Moving 15 stacks from 5–10 s intervals to 60 s:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Healthcheck probes per minute&lt;/td&gt;
&lt;td&gt;232&lt;/td&gt;
&lt;td&gt;94&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Write IOPS at idle&lt;/td&gt;
&lt;td&gt;~380–440&lt;/td&gt;
&lt;td&gt;~295&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Disk 1 busy at idle&lt;/td&gt;
&lt;td&gt;77–88 %&lt;/td&gt;
&lt;td&gt;57 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IO load average&lt;/td&gt;
&lt;td&gt;5–8&lt;/td&gt;
&lt;td&gt;2–3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It was the biggest single improvement I made to that NAS, and it took ten minutes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What else made the box calm
&lt;/h2&gt;

&lt;p&gt;Healthchecks were chapter one of a longer list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Redeploying one stack at a time&lt;/strong&gt; and checking it's running and healthy before the next. A batch of 30 Portainer updates at once took down about 70 containers on this box.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The DSM boot-order trap&lt;/strong&gt;: Container Manager starts containers one by one after a restart, so an MQTT broker can come up 25 minutes late. &lt;code&gt;restart: always&lt;/code&gt; on the few that matter fixes it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory caps&lt;/strong&gt; on every container, so one leak can't take the house down.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No dashboard mounts &lt;code&gt;docker.sock&lt;/code&gt;&lt;/strong&gt;: a read-only socket proxy answers their questions instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I wrote all of it up as &lt;strong&gt;The Quiet NAS&lt;/strong&gt;: a 44-page guide plus 26 ready-to-use files (compose stacks, scripts, a Home Assistant disk-health alert and checklists). Most of it works on any Docker host with hard drives; the Synology-only parts are marked.&lt;br&gt;
→ &lt;a href="https://christoskv.gumroad.com/l/synology-docker-guide?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=launch" rel="noopener noreferrer"&gt;The Quiet NAS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code &lt;strong&gt;LAUNCH25&lt;/strong&gt; takes 25 % off until Monday 30 November.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm the author. Happy to answer questions in the comments, especially if your numbers look different from mine.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>homelab</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
