def get_bounding_box_list_render_image(pdf_file_name, input_doc):
    """Calculate the bounding box list by directly rendering each page of the PDF as
    an image file.  The MediaBox and CropBox values in input_doc should have
    already been set to the chosen page size before the rendering."""

    program_to_use = "pdftoppm" # default to pdftoppm
    if args.gsRender: program_to_use = "Ghostscript"

    # Threshold value set in range 0-255, where 0 is black, with 191 default.
    if not args.threshold: args.threshold = 191
    threshold = args.threshold
    if not args.numSmooths: args.numSmooths = 0
    if not args.numBlurs: args.numBlurs = 0

    temp_dir = ex.program_temp_directory # use the program default; don't delete dir!

    temp_image_file_root = os.path.join(temp_dir, ex.temp_file_prefix + "PageImage")
    if args.verbose:
        print("\nRendering the PDF to images using the " + program_to_use + " program,"
              "\nthis may take a while...")

    # Do the rendering of all the files.
    render_pdf_file_to_image_files(pdf_file_name, temp_image_file_root, program_to_use)

    # Currently assuming that sorting the output will always put them in correct order.
    outfiles = sorted(glob.glob(temp_image_file_root + "*"))

    if args.verbose:
        print("\nAnalyzing the page images with PIL to find bounding boxes,"
              "\nusing the threshold " + str(args.threshold) + "."
              "  Finding the bounding box for page:\n")

    bounding_box_list = []

    for page_num, tmp_image_file_name in enumerate(outfiles):
        curr_page = input_doc.getPage(page_num)

        # Open the image in PIL.  Retry a few times on fail in case race conditions.
        max_num_tries = 3
        time_between_tries = 1
        curr_num_tries = 0
        while True:
            try:
                # PIL for some reason fails in Python 3.4 if you open the image
                # from a file you opened yourself.  Works in Python 2 and earlier
                # Python 3.  So original code is commented out, and path passed.
                #
                # tmpImageFile = open(tmpImageFileName)
                # im = Image.open(tmpImageFile)
                im = Image.open(tmp_image_file_name)
                break
            except (IOError, UnicodeDecodeError) as e:
                curr_num_tries += 1
                if args.verbose:
                    print("Warning: Exception opening image", tmp_image_file_name,
                          "on try", curr_num_tries, "\nError is", e, file=sys.stderr)
                # tmpImageFile.close() # see above comment
                if curr_num_tries > max_num_tries: raise # re-raise exception
                time.sleep(time_between_tries)

        # Apply any blur or smooth operations specified by the user.
        for i in range(args.numBlurs):
            im = im.filter(ImageFilter.BLUR)
        for i in range(args.numSmooths):
            im = im.filter(ImageFilter.SMOOTH_MORE)

        # Convert the image to black and white, according to a threshold.
        # Make a negative image, because that works with the PIL getbbox routine.

        if args.verbose:
            print(page_num+1, end=" ") # page num numbering from 1
        # Note that the point method calls the function on each pixel, replacing it.
        #im = im.point(lambda p: p > threshold and 255) # create a positive image
        #im = im.point(lambda p: p < threshold and 255)  # create a negative image
        # Below code is easier to understand than tricky use of "and" in evaluation.
        im = im.point(lambda p: 255 if p < threshold else 0)  # create a negative image

        if args.showImages:
            im.show() # usually for debugging or param-setting

        # Calculate the bounding box of the negative image, and append to list.
        bounding_box = calculate_bounding_box_from_image(im, curr_page)
        bounding_box_list.append(bounding_box)

        # Clean up the image files after they are no longer needed.
        # tmpImageFile.close() # see above comment
        os.remove(tmp_image_file_name)

    if args.verbose:
        print()
    return bounding_box_list
Calculate the bounding box list by directly rendering each page of the PDF as
    an image file.  The MediaBox and CropBox values in input_doc should have
    already been set to the chosen page size before the rendering.