Author Topic: Python code to stitch together many screenshots  (Read 5867 times)

0 Members and 1 Guest are viewing this topic.

Offline denilsonsa

  • Newbie
  • *
  • Posts: 2
Python code to stitch together many screenshots
« on: November 24, 2023, 05:10:17 am »
As part of the creation of one specific map, I wrote a Jupyter Notebook with some Python code to try to semi-automatically crop and align and stitch screenshots. The code isn't perfect, but worked well enough for my needs. It may require a lot of RAM and a lot of patience if you have too many screenshots.

Here → https://gist.github.com/denilsonsa/2922060be4fcddcf7a4e3745a78b5752 ← here!

This notebook is provided as-is. It requires tinkering to make it work! You need to adapt it to your own needs.

If you want to use it, go ahead! No support is provided. Make sure you have Python on your machine, and you know how to use it. It is wise to use virtual environments to install additional packages. Jupyter is optional (you can just use Python code by itself) but recommended, as it provides a very nice notebook interface that aids with debugging and viewing images.

The provided notebook includes the following functions:

  • load_image() → Loads an image from the disk, optionally cropping a known amount of the image (to remove HUD or the player character). Please adapt it to your needs.
  • diff_images() → Given two images and one (x,y) offset, it computes the amount of differences between those images. While that naïve formula worked well enough for my use-case, you may need to tweak it to your needs.
  • find_best_offset() → Given two images, on initial (x,y) offset, and ranges of X and Y variations, it tries to find the best alignment of those two images. It works by nudging the initial offset around, and picking the best match.
  • compute_all_offsets() → You certainly need to edit this function. I used it to iterate over all my screenshots and compute the offset of each one. If you have a multi-core machine, you may want to try to parallelize this function (which is left as an exercise to the reader).
  • save_composite_image() → Given a list of "layers", writes an OpenRaster image. You may need to tweak the image-loading logic in this function.
  • make_batches() and composite_last_image() → You certainly need to edit these functions. They overlay multiple cropped screenshots onto one larger multi-layer image. I decided to split it in multiple batches to prevent having too many layers at once. I'm also skipping a few screenshots to further reduce the amount of layers.
The end result of the code was an OpenRaster image that can be loaded in GIMP. Afterwards I did some manual cleaning up. Since there were many overlapping layers of screenshots, it was easy to discard one of the screenshots in favor of another (due to different animation frames on each screenshot, or any similar issue).

Enjoy!
« Last Edit: November 24, 2023, 10:40:32 am by denilsonsa »