code: def scale_image(in_fname, out_fname, max_width, max_height):
      """Scales an image with the same aspect ratio centered in an
      image with a given max_width and max_height
      if in_fname == out_fname the image can only be scaled down
      """
      # local import to avoid testing dependency on PIL:
      try:
          from PIL import Image
      except ImportError:
          import Image
      img = Image.open(in_fname)
      width_in, height_in = img.size
      scale_w = max_width / float(width_in)
      scale_h = max_height / float(height_in)
  
      if height_in * scale_w <= max_height:
          scale = scale_w
      else:
          scale = scale_h
  
      if scale >= 1.0 and in_fname == out_fname:
          return
  
      width_sc = int(round(scale * width_in))
      height_sc = int(round(scale * height_in))
  
      # resize the image
      img.thumbnail((width_sc, height_sc), Image.ANTIALIAS)
  
      # insert centered
      thumb = Image.new("RGB", (max_width, max_height), (255, 255, 255))
      pos_insert = (
          (max_width - width_sc) // 2,
          (max_height - height_sc) // 2,
      )
      thumb.paste(img, pos_insert)
  
      thumb.save(out_fname)
      # Use optipng to perform lossless compression on the resized image if
      # software is installed
      if os.environ.get("SKLEARN_DOC_OPTIPNG", False):
          try:
              subprocess.call(
                  ["optipng", "-quiet", "-o", "9", out_fname]
              )
          except Exception:
              logger.warning(
                  "Install optipng to reduce the size of the generated images"
              )
  
imports: from __future__ import division, print_function, absolute_import
  from time import time
  import ast
  import codecs
  import hashlib
  import os
  import re
  import shutil
  import subprocess
  import sys
  import traceback
  from distutils.version import LooseVersion
  from io import StringIO
  import matplotlib
  import matplotlib.pyplot
  import sphinx
  from .None import glr_path_static
  from .None import sphinx_compatibility
  from .backreferences import write_backreferences, _thumbnail_div
  from .downloads import CODE_DOWNLOAD
  from .py_source_parser import split_code_and_text_blocks
  from .notebook import jupyter_notebook, save_notebook
label_desc: Use image node_id 7 for calling a global function with example usage: scale_image(in_fname, out_fname, max_width, max_height) without return types