Created
March 30, 2017 08:40
-
-
Save njanakiev/ca5576cab459d3d6f3ffa6402bab2087 to your computer and use it in GitHub Desktop.
Resize and combine images to an n by n grid with ImageMagick commands: morgify and convert
This file contains 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
import os | |
import shutil | |
# Settings | |
n, m = 6, 6 | |
input_folder = "input" | |
tmp_folder = "tmp" | |
output_image = "combined_image.png" | |
size = 300 | |
# Remove existing folder | |
if os.path.exists(tmp_folder): | |
shutil.rmtree(tmp_folder) | |
os.mkdir(tmp_folder) | |
# Resize images | |
os.system("mogrify -resize %ix%i -quality 100 -path %s %s" % \ | |
(size, size, tmp_folder, os.path.join(input_folder, "*.png"))) | |
# Combine images | |
command = "convert " | |
input_files = [path for path in os.listdir(tmp_folder)] | |
for i in range(n): | |
command += "( " | |
for j in range(m): | |
command += os.path.join(tmp_folder, input_files[i*m + j]) + " " | |
command += "+append ) " | |
command += ("-append %s" % output_image) | |
print(command) | |
os.system(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment