Created
July 13, 2022 20:12
-
-
Save alexpreynolds/4d5f11724954ef59776e70f4cc417a3b to your computer and use it in GitHub Desktop.
Make a montage of cropped data browser snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import subprocess | |
import sys | |
from os import listdir | |
from os.path import isfile, join | |
import time | |
# image, crop dimensions | |
img_dir = sys.argv[1] | |
left = sys.argv[2] | |
right = sys.argv[3] | |
top = sys.argv[4] | |
bottom = sys.argv[5] | |
img_out = sys.argv[6] | |
# get list of images in img_dir | |
imgs = [f for f in listdir(img_dir) if isfile(join(img_dir, f))] | |
for img_base in imgs: | |
if 'cropped' in img_base: continue | |
print(img_base) | |
img = '{}/{}'.format(img_dir, img_base) | |
# arrange the output file's name and path | |
img_base = img[:img.rfind(".")] | |
extension = img[img.rfind("."):] | |
path = img[:img.rfind("/")] | |
img_cropped = img_base + ".cropped" + extension | |
# get the current image size | |
data = subprocess.check_output(["identify", img]).decode("utf-8").strip().replace(img, "") | |
size = [int(n) for n in data.replace(img, "").split()[1].split("x")] | |
# calculate the command to resize | |
w = str(size[0] - int(left) - int(right)) | |
h = str(size[1] - int(top) - int(bottom)) | |
x = left | |
y = top | |
# execute the command | |
cmd = ["convert", img, "-crop", w+"x"+h+"+"+x+"+"+y, "+repage", img_cropped] | |
subprocess.Popen(cmd) | |
time.sleep(1) | |
# horizontally stitch slices | |
cmd = ["convert", "+append", '{}/*.cropped.png'.format(img_dir), img_out] | |
subprocess.Popen(cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ImageMagick is required to run the
identify
andconvert
commands needed by the script.This toolkit can be installed on a Mac via
brew install ImageMagick
(Homebrew) or the usual package managers for Linux.To use this script, start with a folder of snapshots (e.g.,
/home/areynolds/snapshots
):The values
400 400 0 0
here are arbitrary and depend on taste. These simply crop 400 pixels from the left and right sides of each snapshot. The top and bottom dimensions are left unchanged (0
). Adjust these depending on how much you want each stripe to show.The output is written to the file
/home/areynolds/montage.png
, which is one image made up of all the vertical stripes created from cropping.