code: def save_example_pngs(examples, image_dir, make_thumbnails=True):
      """Save example pngs and (optionally) thumbnails"""
      if not os.path.exists(image_dir):
          os.makedirs(image_dir)
  
      # store hashes so that we know whether images need to be generated
      hash_file = os.path.join(image_dir, "_image_hashes.json")
  
      if os.path.exists(hash_file):
          with open(hash_file) as f:
              hashes = json.load(f)
      else:
          hashes = {}
  
      for example in examples:
          filename = example["name"] + (
              ".svg" if example["use_svg"] else ".png"
          )
          image_file = os.path.join(image_dir, filename)
  
          example_hash = hashlib.sha256(
              example["code"].encode()
          ).hexdigest()[:32]
          hashes_match = hashes.get(filename, "") == example_hash
  
          if hashes_match and os.path.exists(image_file):
              print("-> using cached {}".format(image_file))
          else:
              # the file changed or the image file does not exist. Generate it.
              print("-> saving {}".format(image_file))
              chart = eval_block(example["code"])
              try:
                  chart.save(image_file)
                  hashes[filename] = example_hash
              except ImportError:
                  warnings.warn(
                      "Unable to save image: using generic image",
                      stacklevel=1,
                  )
                  create_generic_image(image_file)
  
              with open(hash_file, "w") as f:
                  json.dump(hashes, f)
  
          if make_thumbnails:
              params = example.get("galleryParameters", {})
              if example["use_svg"]:
                  # Thumbnail for SVG is identical to original image
                  thumb_file = os.path.join(
                      image_dir, example["name"] + "-thumb.svg"
                  )
                  shutil.copyfile(image_file, thumb_file)
              else:
                  thumb_file = os.path.join(
                      image_dir, example["name"] + "-thumb.png"
                  )
                  create_thumbnail(image_file, thumb_file, **params)
  
      # Save hashes so we know whether we need to re-generate plots
      with open(hash_file, "w") as f:
          json.dump(hashes, f)
  
imports: import hashlib
  import os
  import json
  import random
  import collections
  from operator import itemgetter
  import warnings
  import shutil
  import jinja2
  from docutils import nodes
  from docutils.statemachine import ViewList
  from docutils.parsers.rst import Directive
  from docutils.parsers.rst.directives import flag
  from sphinx.util.nodes import nested_parse_with_titles
  from .utils import get_docstring_and_rest, prev_this_next, create_thumbnail, create_generic_image
  from altair.utils.execeval import eval_block
  from tests.examples_arguments_syntax import iter_examples_arguments_syntax
  from tests.examples_methods_syntax import iter_examples_methods_syntax
label_desc: Use image node_id 1 for calling a global function with example usage: save_example_pngs(examples, image_dir, make_thumbnails) without return types