Skip to content

Instantly share code, notes, and snippets.

@timlehr
Created May 31, 2017 17:24
Show Gist options
  • Save timlehr/78b1b6c05e1a74e1eceb34e7b8f89768 to your computer and use it in GitHub Desktop.
Save timlehr/78b1b6c05e1a74e1eceb34e7b8f89768 to your computer and use it in GitHub Desktop.
Photo mosaic script (Linux, Python, Imagick)
import os, sys, math, subprocess, shlex
from shutil import copy2
if __name__ == '__main__':
walkdir = "/mnt/tlehr/projects/btb_mosaic/photos/" # folder which contains all photos
destdir = "/mnt/tlehr/projects/btb_mosaic/collection/" # folder to which the specific photos to use will be copied
destfile = "/mnt/tlehr/projects/btb_mosaic/mosaic.jpg" # filepath to the resulting image
# image ratio
ratioW = 16
ratioH = 9
tileSize = 200 # in pixels!
backgroundColor = "black"
# 1. COLLECT PHOTOS FROM GAME FOLDER
if not os.path.exists(destdir):
os.mkdir(destdir)
else:
os
photocounter = 0
for folder, subfolders, files in os.walk(walkdir):
for file in files:
if("capture_") in file:
photocounter += 1
fileSrc = os.path.join(folder, file)
fileDst = os.path.join(destdir, "photo_collection_{}.png".format(str(photocounter).zfill(5)))
#copy2(fileSrc,fileDst)
print "SRC: {} // DST: {}".format(fileSrc, fileDst)
print "TOTAL COUNT: {}".format(photocounter)
# 2. SETUP MONTAGE COMMAND
yCount = math.sqrt(ratioH*photocounter/ratioW)
xCount = ratioW * yCount / ratioH
print "Image Setup | Tile Size: {}px // X: {} // Y: {}".format(tileSize, round(xCount,0),round(yCount,0))
command = "montage -mode concatenate -geometry {}x{}+0+0 -background {} -tile {}x{} {} {}".format(tileSize,tileSize, backgroundColor, int(round(xCount,0)),int(round(yCount,0)), os.path.abspath(os.path.join(destdir, "photo_collection_*.png")), os.path.abspath(destfile))
print "EXECUTE COMMAND: {}".format(command)
print "CALCULATING! This may take a few minutes ..."
# 3. EXECUTE COMMAND IN SEPARATE PROCESS
montage = subprocess.Popen(shlex.split(command))
montage.wait()
print "DONE! Check it out: {}".format(destfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment