-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifToRenPy.py
192 lines (121 loc) · 5.42 KB
/
gifToRenPy.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
####### File stuff ################################################################################
# Lets you pick a file and returns it.
import easygui
def pickFile():
# Open the prompt.
a = easygui.fileopenbox( title = "Pick .gif to split:" )
# Raise exception if a file wasn't picked
if not a:
raise Exception("Nothing selected!")
# Return the file.
return a
# File path with extension.
gifFilePath = pickFile()
# File name with extension.
gifFile = gifFilePath.split("\\")[-1]
# Check if extension is ".gif"
if gifFile.split(".")[-1] != "gif":
raise Exception("That is not a gif!")
# File name without extension.
gifFileName = ".".join(gifFile.split(".")[:-1])
# print(gifFilePath)
# print(gifFile)
# print(gifFileName)
####### Preparing a directory for the output ######################################################
# For checking and creating directories.
import os
# Folder inside which all results are placed.
#
# This is important because this directory is included in paths inside the generated .rpy file.
# "images/" means the result inside should be placed inside a project's "images" folder.
baseOutputDir = "images/"
# If the directory doesn't already exist...
if not os.path.isdir(baseOutputDir):
# ...create it.
os.mkdir(baseOutputDir)
# Folder inside which this result will be placed, in the form of "baseOutputDir/gifFileName/"
gifOutputDir = baseOutputDir + gifFileName + "/"
# This directory must not already exist.
if os.path.isdir(gifOutputDir):
raise Exception("It looks like the directory for output, \"{}\", exists already!".format( baseOutputDir + gifFileName ))
# Create the directory.
os.mkdir(gifOutputDir)
####### Gif into frames ###########################################################################
# For the (gif -> png) conversion.
from PIL import Image
# Load in chosen file.
im = Image.open(gifFilePath)
# List of file paths to individual frames.
# Used in creating the .rpy file.
pathsToFrames = []
# For every frame inside the loaded Image:
for frameIndex in range( im.n_frames ):
# Point at another frame.
im.seek(frameIndex)
# Filename of the saved frame.
# Format is "[chosen file without extension][index of the frame].png"
saveFileName = gifOutputDir + "{}{}.png".format(gifFileName, frameIndex)
# Save the frame.
im.save( saveFileName )
# Save the frame path to a list.
# Used in creating the .rpy file.
pathsToFrames.append(saveFileName)
print("\nSuccessfully saved all frames into \"{}\"\n\n###########################################################\n".format(gifOutputDir))
####### Optionally creating a .rpy file.
createRpy = raw_input("Would you like to create a .rpy file with an image statement, defining the image for you?\nI do this by default, when nothing is typed in. Type \"n\" if you don't want me to. -- ")
# Negative input.
if createRpy == "n":
print("\n###########################################################\n\nSkipped creating the .rpy file.")
# End the script.
exit()
####### Settings for creating a Ren'Py image statement. ###########################################
print("\nYou will now be asked for some settings.\nDefault values are chosen when nothing is typed in.\n")
### Time interval between frames. ###################################
pauseInterval = raw_input("Pause interval between frames? (float, default is 0.1) -- ")
# Default, if nothing given.
if not pauseInterval:
pauseInterval = 0.1
# Convert it to a float.
else:
try:
pauseInterval = float(pauseInterval)
# If cannot be converted:
except:
raise Exception("Pause interval, if given, must be a whole or a decimal number.")
### Whether the animation should repeat. ############################
addRepeat = raw_input("Should the animation repeat? (y/n, default is \"n\") -- ")
# Positive input.
if addRepeat == "y":
addRepeat = True
# Negative or no input.
elif addRepeat == "n" or not addRepeat:
addRepeat = False
# Something else typed in.
else:
raise Exception("Something other than \"y\", \"n\" or \"\" typed in.")
### Properties that will be added onto the first line. ##############
### These will be in effect throughout the whole animation. #########
firstProperties = raw_input("Add some properties onto the first line? (Properties written like you would in ATL, none by default) -- ")
# If none are given, the line won't be added at all.
if not firstProperties:
firstProperties = None
####### Creating a .rpy file with an image statement. #############################################
# Path of the .rpy file with extension.
rpyFilePath = gifOutputDir + gifFileName + ".rpy"
# Create the file.
with open(rpyFilePath, "w+") as f:
# Image statement (first line)
statement = "image " + gifFileName + ":\n"
f.write(statement)
# Add properties onto the first line if any were given.
if firstProperties is not None:
properties = " " + firstProperties + "\n"
f.write(properties)
# Write an image path, followed by a pause of given interval.
for pathToFrame in pathsToFrames:
f.write( " " + "\"" + pathToFrame + "\"\n" )
f.write( " " + "pause " + str(pauseInterval) + "\n" )
# Optionally finish with a repeat.
if addRepeat:
f.write(" repeat")
print("\n###########################################################\n\nSuccessfully saved the .rpy file as \"{}\"".format(rpyFilePath))