Completed
Push — master ( c72288...746188 )
by Anas
32s
created

remove_temp()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
import subprocess
4
import os
5
6
7
# Cut & resize before processing
8
def cut_img(path, filename, extension):
9
    cut = "convert " + path + filename + extension + " ( +clone +level-colors white \
10
           ( +clone -rotate 90 +level-colors black ) \
11
           -composite -bordercolor white -border 1 -trim +repage ) +swap -compose Src \
12
           -gravity center -composite -resize 612x612 -extent 612x612 " + path + "temp" + extension
13
    subprocess.run(cut, shell=True)
14
15
16
# Add border of chosen color
17
def border(path, filename, extension, color):
18
    border = "convert  " + path + "temp" + extension + " -bordercolor " + color + " -border 20x20  " + path + "temp" + extension
19
    subprocess.run(border, shell=True)
20
21
22
# Add frame
23
def frame(path, filename, extension, file):
24
    frame = "convert  " + path + "temp" + extension + " resources/frames/" + file + ".png -geometry +0+0 \
25
             -composite  " + path + "temp" + extension
26
    subprocess.run(frame, shell=True)
27
28
29
# Vignette
30
def vignette(path, filename, extension, color1, color2):
31
    vignette = "convert " + path + "temp" + extension + " -size 918x918 radial-gradient:" + color1 + "-" + color2 + " \
32
                -gravity center -crop 612x612+0+0 +repage -compose multiply -flatten " + path + "temp" + extension
33
    subprocess.run(vignette, shell=True)
34
35
36
# Resize (if modified) and rename
37
def finish(path, extension, result_name):
38
    finish = "convert " + path + "temp" + extension + " -resize 612x612 " + path + result_name + extension
39
    subprocess.run(finish, shell=True)
40
41
42
# Filters to apply
43
def filt_Gotham(path, filename, extension):
44
    cut_img(path, filename, extension)
45
    primary_filter = "convert  " + path + "temp" + extension + " -modulate 120,10,100 -fill #222b6d \
46
                      -colorize 20 -gamma 0.5 -contrast -contrast  " + path + "temp" + extension
47
    subprocess.run(primary_filter, shell=True)
48
    border(path, filename, extension, "black")
49
    finish(path, extension, filename+"-gotham")
50
    remove_temp(path)
51
52
53
def filt_Grayscale(path, filename, extension):
54
    cut_img(path, filename, extension)
55
    primary_filter = "convert  " + path + "temp" + extension + " -colorspace Gray " + path + "temp" + extension
56
    subprocess.run(primary_filter, shell=True)
57
    finish(path, extension, filename+"-grayscale")
58
    remove_temp(path)
59
60
61
def filt_Kelvin(path, filename, extension):
62
    cut_img(path, filename, extension)
63
    primary_filter = "convert " + path + "temp" + extension + " -auto-gamma -modulate 120,50,100 \
64
              -size 612x612 -fill rgba(255,153,0,0.5) -draw \"rectangle 0,0 612,612\" \
65
              -compose multiply " + path + "temp" + extension
66
    subprocess.run(primary_filter, shell=True)
67
    frame(path, filename, extension, "kelvin")
68
    finish(path, extension, filename+"-kelvin")
69
    remove_temp(path)
70
71
72
def filt_Lomo(path, filename, extension):
73
    cut_img(path, filename, extension)
74
    primary_filter = "convert " + path + "temp" + extension + " -channel R -level 25% \
75
             -channel G -level 25% " + path + "temp" + extension
76
    subprocess.run(primary_filter, shell=True)
77
    vignette(path, filename, extension, "none", "black")
78
    finish(path, extension, filename+"-lomo")
79
    remove_temp(path)
80
81
82
def filt_Nashville(path, filename, extension):
83
    cut_img(path, filename, extension)
84
    primary_filter = "convert  " + path + "temp" + extension + " -contrast \
85
                      -modulate 100,150,100 -auto-gamma " + path + "temp" + extension
86
    subprocess.run(primary_filter, shell=True)
87
    color_overlay = "convert  " + path + "temp" + extension + " ( -clone 0 -fill #222b6d -colorize 100% ) \
88
                    ( -clone 0 -colorspace gray -negate ) -compose blend \
89
                    -define compose:args=50,50 -composite  " + path + "temp" + extension
90
    subprocess.run(color_overlay, shell=True)
91
    color_overlay2 = "convert  " + path + "temp" + extension + " ( -clone 0 -fill #f7daae -colorize 100% ) \
92
                    ( -clone 0 -colorspace gray ) -compose blend \
93
                    -define compose:args=120,-20 -composite  " + path + "temp" + extension
94
    subprocess.run(color_overlay2, shell=True)
95
    frame(path, filename, extension, "nashville")
96
    finish(path, extension, filename+"-nashville")
97
    remove_temp(path)
98
99
100
def filt_Toaster(path, filename, extension):
101
    cut_img(path, filename, extension)
102
    color_overlay = "convert  " + path + "temp" + extension + " ( -clone 0 -fill #330000 -colorize 100% ) \
103
                    ( -clone 0 -colorspace gray -negate ) -compose blend \
104
                    -define compose:args=50,50 -composite  " + path + "temp" + extension
105
    subprocess.run(color_overlay, shell=True)
106
    primary_filter = "convert  " + path + "temp" + extension + " -modulate 150,80,100 -gamma 1.2 -contrast -contrast " + path + "temp" + extension
107
    subprocess.run(primary_filter, shell=True)
108
    vignette(path, filename, extension, "none", "LavenderBlush3")
109
    vignette(path, filename, extension, "#ff9966", "none")
110
    border(path, filename, extension, "white")
111
    finish(path, extension, filename+"-toaster")
112
    remove_temp(path)
113
114
115
def filt_VHS(path, filename, extension):
116
    cut_img(path, filename, extension)
117
    red = "convert " + path + "temp" + extension + " -page +4+4 -background black -flatten -channel B -fx 0 " + path + "temp_r" + extension
118
    subprocess.run(red, shell=True)
119
    green = "convert " + path + "temp" + extension + " -channel R -fx 0 " + path + "temp_g" + extension
120
    subprocess.run(green, shell=True)
121
    blue = "convert " + path + "temp" + extension + " -page -4-4 -background black -flatten -channel G -fx 0 " + path + "temp_b" + extension
122
    subprocess.run(blue, shell=True)
123
    combine = "convert " + path + "temp_r" + extension + " " + path + "temp_g" + extension + " " + path + "temp_b" + extension + " -average " + path + "temp" + extension
124
    subprocess.run(combine, shell=True)
125
    frame(path, filename, extension, "scan")
126
    finish(path, extension, filename+"-VHS")
127
    remove_temp(path, rgb=True)
128
    
129
130
def remove_temp(path, rgb=False):
131
    os.remove(path+"temp.jpg")
132
    if rgb is True:
133
        os.remove(path+"temp_r.jpg")
134
        os.remove(path+"temp_g.jpg")
135
        os.remove(path+"temp_b.jpg")