aboutsummaryrefslogtreecommitdiff
path: root/filmstripper.py
blob: bdba9528473f05489d74cb0461b9b7cc1469ee90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3

from PIL import Image
import glob
import sys
import random
from os import path


# Reading the input arguments
if len(sys.argv) > 2:
    try:
        print("Reading input folder...")
        input_folder = sys.argv[1]
        print(input_folder)
    except:
        print("ERROR: Input folder argument invalid.")
        sys.exit()

    try:
        print("Reading output filename...")
        output_file = sys.argv[2]
        print(output_file)
    except:
        print("ERROR: Output file argument invalid.")
        sys.exit()
else:
    input_folder = "input/"
    output_file = "output/output.png"

if path.isfile(output_file):
    name_conflict = True
    while name_conflict:
        print("WARNING: Output file already exists. What would you like to do? Overwrite, Cancel or Unique?")
        answer = input("[O/C/U]: ")
        if answer == "O" or answer == "o":
            None
            name_conflict = False
        elif answer == "C" or answer == "c":
            sys.exit()
            name_conflict = False
        elif answer == "U" or answer == "u":
            while path.isfile(output_file):
                index = output_file.find(".")
                new_name = output_file[:index] + str(random.randint(0, 9)) + output_file[index:]
                print("New output filename: "+new_name)
                output_file = new_name
            name_conflict = False
        else:
            print("Please write O, C or U.")

try:
    if "v" in sys.argv[3] or "V" in sys.argv[3]:
        print("Using vertical layout")
        horizontal_rendering = False
    elif "h" in sys.argv[3] or "H" in sys.argv[3]:
        print("Using horizontal layout")
        horizontal_rendering = True
    else:
        print(str('ERROR: "'+sys.argv[3])+'"'+" is not a recognized layout. Use H or V.")
        sys.exit()
except:
    horizontal_rendering = False

# Parsing the input arguments
# And setting up basic variables

file_list = glob.glob(str(input_folder+"*.*"))
file_list.sort()
first_file = file_list[0]
input_extension = str(first_file.partition(".")[1]+first_file.partition(".")[2])
first_file.partition(".")[2]

if input_extension:
    print("Input file extension found: "+input_extension)
else:
    print("ERROR: Input file extension not found.")
    sys.exit()

output_extension = str(output_file.partition(".")[1]+output_file.partition(".")[2])

if output_extension:
    print("Output file extension found: "+output_extension)
elif input_extension == ".jpg":
    print("Output file extension not found, using input file type, .jpg")
else:
    print("Output file extension not found, defaulting to .png")


# Setting up filmstrip buffer

print("Loading first image... "+first_file)
first_image = Image.open(first_file)
width = first_image.width
fs_width = width
height = first_image.height
fs_height = height
colorspace = first_image.mode
print("Image attributes:")
print("Width: "+str(width))
print("Height: "+str(height))
print("Colorspace: "+colorspace)

if horizontal_rendering:
    fs_width = 0
    width_add = width
    height_add = 0
else:
    fs_height = 0
    height_add = height
    width_add = 0

print("Creating filmstrip buffer...")
filmstrip = Image.new(colorspace, (fs_width, fs_height))
print("Width: "+str(filmstrip.width))
print("Height: "+str(filmstrip.height))
print("Colorspace: "+filmstrip.mode)


# Adding each image to filmstrip

for each_file in file_list:
    print("Loading "+each_file+"...")
    each_image = Image.open(each_file)

    # Checking files for compatabililty

    if input_extension not in each_file:
        print("ERROR: Filetype mismatch.")
        sys.exit()
    if horizontal_rendering and each_image.height != fs_height:
        print("ERROR: Image height mismatch.")
        sys.exit()
    if not horizontal_rendering and each_image.width != fs_width:
        print("ERROR: Image width mismatch.")
        sys.exit()
    if each_image.mode != filmstrip.mode:
        print("ERROR: Image colorspace mismatch")

    extended_strip = filmstrip.crop(box=(0, 0, filmstrip.width+width_add, filmstrip.height+height_add))
    extended_strip.paste(each_image, (extended_strip.width-width, extended_strip.height-height))
    filmstrip = extended_strip
    print(each_file+" added to filmstrip.")

print("Exporting filmstrip to "+output_file)
filmstrip.save(output_file)
print("Filmstrip exported.")