compilebackgrounds.py (Source)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# The MIT License (MIT)                                                             #
#                                                                                   #
# Copyright (c) 2015 Bob Rubbens                                                    #
#                                                                                   #
# Permission is hereby granted, free of charge, to any person obtaining a copy      #
# of this software and associated documentation files (the "Software"), to deal     #
# in the Software without restriction, including without limitation the rights      #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell         #
# copies of the Software, and to permit persons to whom the Software is             #
# furnished to do so, subject to the following conditions:                          #
#                                                                                   #
# The above copyright notice and this permission notice shall be included in        #
# all copies or substantial portions of the Software.                               #
#                                                                                   #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR        #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,          #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE       #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER            #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,     #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN         #
# THE SOFTWARE.                                                                     #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

import subprocess
import glob
import random
import math
import sys
import threading

allbg = glob.glob("*.jpg")
allbg.extend(glob.glob("*.jpeg"))
allbg.extend(glob.glob("*.png"))

big = []
small = []
smallest = []
cropped = []
amount = 100

database = []

if len(sys.argv) <= 1:
	print("what? compile or scan")
	exit()
elif sys.argv[1] == "compile":
	print("righy-o, generating backgrounds in results\\")
elif sys.argv[1] == "scan":
	print("okok, just scanning the directory and printing the ones that are sized wrongly")
else:
	print("that makes no sense")
	exit()

for bg in allbg:
	cmd = "identify -format \"%wx%h\" \"" + bg + "\""
	output = subprocess.check_output(cmd).decode("ascii")
	wxh = output.split("x")
	if wxh[0] == str(1920) and wxh[1] == str(1080):
		big.append(bg)
		print("Big: " + bg)
	elif wxh[0] == str(1280) and wxh[1] == str(1024):
		small.append(bg)
		print("Small: " + bg)
	elif wxh[0] == str(1024) and wxh[1] == str(768):
		smallest.append(bg)
		print("Smallest: " + bg)
	else:
		cropped.append("Should be cropped: " + bg + " (" + wxh[0] + " x " + wxh[1] + ")")

for msg in cropped:
	print(msg)

print("---------------------------------------------------------")
print("Smallest: " + str(len(smallest)))
print("Small: " + str(len(small)))
print("Big: " + str(len(big)))
print("Uncropped: " + str(len(cropped)))
print("---------------------------------------------------------")

if sys.argv[1] == "scan":
	exit()

ctr = 0
database.append([])
database.append([])
database.append([])
database.append([])

for s in small:
	for xs in smallest:
		for l in big:
			name = s + xs + l
			command = "montage -mode Concatenate \"" + s + "\" \"" + xs + "\" \"" + l + "\" \"result\\" + name + ".png\""
			database[ctr].append(command);
			ctr = (ctr + 1) % 4


def processBgs(jobs, id):
	ctr = 0
	for job in jobs:
		subprocess.call(job)
		print(str(int(ctr/float(len(jobs))*100)) + " (" + str(id) + ")")
		ctr += 1

threads = []
for i in range(4):
	t = threading.Thread(target=processBgs, args=(database[i], i))
	threads.append(t)
	t.start()

for i in range(4):
	threads[i].join();