code: def set_format(self):
          
          f = self.fmt
  
          if f.flexible_height and f.flexible_max_height:
              flexw, flexh = self.fw, f.flexible_max_height
              flex_ratio = float(flexw) / flexh
  
              if abs(flex_ratio - self.image_ratio) < abs(self.format_ratio - self.image_ratio):
                  self.fh = flexh
                  self.format_ratio = flex_ratio
docs: Check if the format has a flexible height, if so check if the ratio
          of the flexible format is closer to the actual ratio of the image. If
          so use that instead of the default values (f.max_width, f.max_height).