-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplacer.py
165 lines (155 loc) · 4.92 KB
/
Replacer.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from PIL import Image
from random import seed
from random import randint
import os
import time
import PySimpleGUI as sg
randnO = 0
ImgNbR = 0
randnR = 0
tumbnb = 0
SeeD = 0
imgst = False
dir = "./Replacement Images"
dir_name2 = ".\Replacement Images"
dir_name3 = ".\Images to put in STIPphoto"
fO1 = open("./Seed.txt", "r")
fO1.readline(1)
seed(fO1.readline(1))
fO1.close()
class Replacer():
@staticmethod
def genintO():
global randnO
for _ in range(1):
value = randint(0, 131)
randnO = value
print("Random NBO genereated:")
print(randnO)
MRun.ImgCountR()
@staticmethod
def ImgCountR():
global ImgNbR
ImgR = next(os.walk(dir))[2]
print("Number of Replacement Images Counted:")
ImgNbR = len(ImgR)
print(ImgNbR)
MRun.ImgFilterO()
@staticmethod
def ImgFilterO():
global dir_name
global DIR
fO = open("./IMGFO.txt", "w")
DIR = open("./dir_name.txt", "r")
dir_name = DIR.read()
print(dir_name)
try:
ImgFO = sorted( filter( lambda x: os.path.isfile(os.path.join(dir_name, x)),
os.listdir(dir_name) ) )
for file_name in ImgFO:
fO.write(file_name)
fO.write("\n")
fO.close()
DIR.close()
MRun.ImgFilterR()
except FileNotFoundError:
print("invalid dir.")
sg.popup("Invalid directory was set!")
MRun.crash()
@staticmethod
def ImgFilterR():
fR = open("./IMGFR.txt", "w")
ImgFR = sorted( filter( lambda x: os.path.isfile(os.path.join(dir_name2, x)),
os.listdir(dir_name2) ) )
for file_name in ImgFR:
fR.write(file_name)
fR.write("\n")
fR.close()
MRun.genintR()
@staticmethod
def genintR():
global randnR
for _ in range(1):
try:
value = randint(0, ImgNbR - 1)
randnR = value
print("Random NBR genereated:")
print(randnR)
MRun.ImgIMCREX()
except ValueError:
print("hello crash")
MRun.crash()
#make new method for SRTool
@staticmethod
def ImgIMCREX():
global OImgO
global imgst
f = open("./IMGFO.txt", "r")
o = open("./IMGFR.txt", "r")
OImg = f.readlines()
OImgR = o.readlines()
OImgO = OImg[randnO].rstrip("\n")
OImgRO = OImgR[randnR].rstrip("\n")
im = Image.open(dir_name +"/" + OImgO)
imR = Image.open("./Replacement Images/" + OImgRO)
im.close()
imR = imR.resize((425, 320))
imR = imR.convert("RGB")
if not os.path.exists("./Images to put in STIPphoto/" + OImgO):
imgst = True
print("New image saved!")
imR.save("./Images to put in STIPphoto/" + OImgO)
os.replace("./Replacement Images/" + OImgRO, "./Replacement Images Backup/" + OImgRO)
print("Replacement IMG used moved!")
MRun.tumb()
print("IMG already exists...")
imR.close()
MRun.reset()
@staticmethod
def tumb():
print("tumbnail method called")
OImgO2 = OImgO.rstrip(".jpg")
ImgtO = OImgO2 + "-thumb.jpg"
imt = Image.open("./Images to put in STIPphoto/" + OImgO)
imt = imt.resize((120, 90))
imt = imt.convert("RGB")
imt.save("./Images to put in STIPphoto/Thumbnails/" + ImgtO)
print("Tumbnail saved!")
imt.close()
MRun.reset()
@staticmethod
def reset():
print(SeeD)
ImgR = next(os.walk(dir))[2]
if len(ImgR) == 0:
print("replacement img count == 0, crashing.")
MRun.crash()
elif len(ImgR) != 0:
print("Looping")
time.sleep(1)
MRun.genintO()
@staticmethod
def crash():
try:
print("crashing to return to GUI.py")
fR = open("./crash.txt", "r")
fR.readlines()
except FileNotFoundError:
print("hello fillenotfounderror")
@staticmethod
def SRTOOL(Reimg,Orimg):
global imgst
DIR = open("./dir_name.txt", "r")
dir_name = DIR.read()
Pre = Image.open(str(dir + "/" + Reimg))
Dor = str(dir_name + "\\" + Orimg)
Pre = Pre.resize((425, 320))
Pre = Pre.convert("RGB")
Pre.save("./Images to put in STIPphoto/" + Orimg)
os.replace("./Replacement Images/" + Reimg, "./Replacement Images Backup/" + Reimg)
Pre = Pre.resize((120, 90))
Orimg2 = Orimg.rstrip(".jpg")
OrimgT = Orimg2 + "-thumb.jpg"
Pre.save("./Images to put in STIPphoto/Thumbnails/" + OrimgT)
MRun = Replacer()
#MRun = MRun.reset()