-
Notifications
You must be signed in to change notification settings - Fork 3
/
transfer_palette_and_time.py
372 lines (328 loc) · 13 KB
/
transfer_palette_and_time.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
from PIL import Image
import os.path
import os
import numpy as np
alpha_limit = 130
from statistics import mode
#Transfers one image's palette to another.
#Usage: python transfer_palette.py sourceImage targetImage
#target can be a folder full of pngs. It will compile them into a gif.
#You can also use generate_palette.py to create a bash script that makes the argument pass easier with drag-and-drop.
def index_image(image,palette=None):
#print(palette.mode)
return image.quantize(colors=256, method=2, kmeans=0, palette=palette, dither=0)
def remove_unused_color_from_palette(image):
#Will also mess up the image, but no care for it for now
#Gif:do it for every frame and group all the colors in one image
palettedata = image.getpalette()
data = np.array(image)
# print(image.getpalette())
# print(len(image.getpalette()))
if(image.info.get("transparency",None) != None):
#Remove transparency from frame
tv = image.info.get("transparency")
# print("Transparent color detected in palette",tv)
fill = int(tv==0)
data[data==tv]=fill
# print("Filled with",fill)
#trgb = palettedata[tv*3:tv*3+3]
#palettedata[tv*3:tv*3+3] = (0,0,0)
uniquecolors = np.unique(data)
uniquergb = [tuple(palettedata[x*3:x*3+3]) for x in uniquecolors]
palettesize = len(uniquergb)
newpalette = list()
for rgb in uniquergb:
newpalette.extend(rgb)
while len(newpalette)<3*256:
newpalette.extend(uniquergb[0])
#newpalette[0:palettesize] are the used colors
# print("Unique colors:",len(uniquecolors))
# print(uniquecolors)
image.info["transparency"]=255
image.putpalette(newpalette)
# print(image.getpalette())
# print(len(image.getpalette()))
#image.show()
return image
def get_outline_color(image):
bgc = get_background_color(image)
colors = list()
for j in range(image.height):
for i in range(image.width):
col = image.getpixel((i,j))
if(col)!=bgc:
colors.append(col)
break
# print(colors)
return mode(colors)
def get_background_color(image):
return mode((image.getpixel((0,0)),image.getpixel((-1,0)),image.getpixel((0,-1)),image.getpixel((-1,-1))))
def deal_transparency(image):
if(image.mode=="RGB"):
image=image.convert("RGBA")
#return image, None
image=image.copy()
data = np.array(image)
# print(data)
#print(image.mode)
#print(data[0][0])
alpha = data[:,:,3:]
#print(np.unique(alpha))
alpha[alpha<=alpha_limit]=0
alpha[alpha>alpha_limit]=255
alphaonly = data[:,:,3].copy()//255
result = Image.fromarray(data)
#print(np.unique(alphaonly))
#mask = None#Image.fromarray(alphaonly)
return result, alphaonly
def unused_color(image):
data = np.array(image)
uniquecolors = set(np.unique(data))
#print(uniquecolors)
for i in range(256):
if(i not in uniquecolors):
# print("Unused color:",i)
# input()
return i
return None
def get_palette_transparency_area(image):
image=image.copy()
tr = image.info.get("transparency",None)
data = np.array(image)
if(tr==0):
data[data!=tr]=1
else:
data[data!=tr]=0
data[data==tr]=2
data[data==0]=1
data[data==2]=0
# print("Transparency mask:",data)
return data
def reset_transparency(paletteimage,mask,transparency=255):
# print("Paletteimage mode:",paletteimage.mode)
data = np.array(paletteimage)
# print(data[0,0])
# print(mask[0,0])
# print("Data:",data)
# print("Mask:",mask)
data = data*mask
mask = -(mask-1)*transparency
data = data+mask
result = Image.fromarray(data,"P")
# print("Result mode",result.mode)
result.putpalette(paletteimage.getpalette())
result.info["transparency"]=transparency
return result
def swap_palette_colors(image, source_id=None, target_id = 0):
#Puts the source_id color at index target_id
image=image.copy()
palettedata = image.getpalette()
if(source_id==None):
source_id = get_background_color(image) #must be a palette ID
else:
source_id = source_id
source_index = source_id*3
target_index = target_id*3
target_color = palettedata[target_index:target_index+3]
source_color = palettedata[source_index:source_index+3]
palettedata[target_index:target_index+3] = source_color
palettedata[source_index:source_index+3] = target_color
if(source_id==target_id):
return image
data = np.array(image)
area_source = data.copy()
area_target = data.copy()
intermediate = (source_id+1+(target_id==source_id+1))%256
#if(source_id!=0):
area_source[area_source!=source_id]=intermediate
area_source[area_source==source_id]=target_id
area_source[area_source==intermediate]=0
area_target[area_target!=target_id]=intermediate
area_target[area_target==target_id]=source_id
area_target[area_target==intermediate]=0
data[data==source_id]=0
data[data==target_id]=0
data+=area_source+area_target
result = Image.fromarray(data)
result.putpalette(palettedata)
return result
from math import log
def transfer_durations(source,images):
im = Image.open(source)
input_durations = list()
try:
while 1:
input_durations.append(im.info.get("duration",0))
im.seek(im.tell()+1)
except EOFError:
pass # end of sequence
max_multiplier = int(len(images)/len(input_durations))
output=list()
output_durations = list()
indexes = list()
print("Max multiplier calc:", len(images),"/",len(input_durations),"=", max_multiplier)
for index,duration in enumerate(input_durations):
imindex = index*max_multiplier
#print(imindex)
#output.append(images[imindex])
imposs = duration/max_multiplier
if(imposs>30):
#First long frame, then slow move
indexes.append("Long first")
for i in range(max_multiplier):
indexes.append(imindex+i)
output.append(images[imindex+i])
start = duration-max_multiplier*20
for i in range(max_multiplier):
output_durations.append(20+start)
start=0
elif(imposs>=20):
#Taper out
indexes.append("Elongate")
for i in range(max_multiplier):
indexes.append(imindex+i)
output.append(images[imindex+i])
for i in range(max_multiplier):
output_durations.append(int(round(duration/max_multiplier))) #Should be round /10 *10 maybe
else:
#Not enough frames
indexes.append("Cut short")
max_interpolation = duration/20
#only allow powers of 2
max_interpolation = max(2,int(2**int(log(max_interpolation)/log(2))))
for i in range(max_interpolation):
try:
finalindex = int(imindex+i*(max_multiplier/max_interpolation))
indexes.append(finalindex)
output.append(images[finalindex])
except Exception as e:
print("max_multiplier",max_multiplier)
print("Current original index:",index)
print("Current starting index for target",imindex)
print("max_interpolation",max_interpolation)
print("Current i in max_interpolation",i)
print("Current index in image:",finalindex)
print("images:",len(images))
raise e
for i in range(max_interpolation):
output_durations.append(max(20,int(round(duration/max_interpolation)))) #Should be round /10 *10 maybe
print(indexes)
return output,output_durations
def create_gif_from_folder(source,foldername,outputname=None,palette=None):
images = list()
durations = list()
previous_time = 0
if(palette!=None):
palette=remove_unused_color_from_palette(palette) #this actually ends up mangling the colors, so only do it once
for file in os.listdir(foldername):
if file.endswith(".png") or file.endswith(".gif"):
im = Image.open(foldername+os.sep+file)
try:
time = int(foldername.split(".")[-1])
if(time!=0):
durations.append(max(time-previous_time,20))
except:
pass
if(im.mode=="RGB" or im.mode=="RGBA"):
im2, mask=deal_transparency(im)
im2=im.convert("RGB")
elif(im.mode=="P"):
mask = get_palette_transparency_area(im)
im2=im.convert("RGB")
else:
print("Unhandled image mode:",im.mode)
#palette=remove_unused_color_from_palette(palette) #this actually ends up mangling the colors
im2 = index_image(im2,palette)
tr=unused_color(im2)
if not(mask is None):
im2=reset_transparency(im2,mask,tr)
im2=swap_palette_colors(im2,tr,0)
images.append(im2)
durations+=[20]*(len(images)-len(durations))
for im in images:
im.info['transparency']=None
del im.info['transparency']
#print(len(images),len(durations))
#print(durations)
images,durations=transfer_durations(source,images)
images[0].save(outputname, "GIF", save_all=True,append_images=images[1:], optimize=False, transparency=0, disposal=2, duration=durations, loop=0)
def create_gif_from_image(filename,outputname=None,palette=None):
im = Image.open(filename)
if(palette!=None):
palette=remove_unused_color_from_palette(palette) #this actually ends up mangling the colors, so only do it once
images = list()
durations = list()
disposals = list()
transparency = list()
try:
while 1:
if(im.mode=="P"):
mask = get_palette_transparency_area(im)
else:
im, mask = deal_transparency(im)
# palette=remove_unused_color_from_palette(palette)
im2=index_image(im.convert("RGB"),palette)
tr=unused_color(im2)
# print("Mode:",im.mode)
# input(str(mask))
if(mask is not None):
im2=reset_transparency(im2, mask,tr)
im2=swap_palette_colors(im2,tr,0)
images.append(im2)
try:
#transparency.append(im.info.get('transparency',transparency))
#print("tp",transparency)
durations.append(im.info["duration"])
disposals.append(im.disposal_method)
# del im.info["transparency"]
except Exception as e:
# print("Exception",e)
durations=None
disposals=None
im.seek(im.tell()+1)
except EOFError:
pass # end of sequence
if(len(images)>1):
#print(transparency)
#if(len(transparency)!=0):
# transparency=get_background_color(images[0]) #or rather, use the mask created earlier
images[0].save(outputname, "GIF", save_all=True,append_images=images[1:], optimize=False, duration=durations, transparency=0, loop=0)
else:
#if(len(transparency)!=0 and transparency[0]!=None):
images[0].save(outputname, "GIF", optimize=False, transparency=0)
# else:
# images[0].save(outputname, "GIF", optimize=False)
try:
if __name__ == "__main__":
import sys
if(len(sys.argv)>1):
source=sys.argv[1]
target=sys.argv[2]
#pal = load_palette(source)
pal = Image.open(source)
if(pal.mode!="P"):
print("Source is not palleted image: quantizing")
pal = pal.quantize(colors=256, method=None, kmeans=0, palette=None, dither=0)
else:
pass
# print("Source image palette:",pal.getpalette())
# print("Source has transparency:",pal.info.get("transparency",None))
name,extension = os.path.splitext(source)
if(os.path.isdir(target)):
tname = os.path.basename(target)
create_gif_from_folder(source,target,name+"_EXT_%s.gif"%tname,palette=pal)
else:
tname = os.path.splitext(target)[0]
sname = os.path.splitext(os.path.basename(source))[0]
create_gif_from_image(target,tname+"_COL_"+sname+".gif",palette=pal)
except Exception as E:
print(E)
import traceback
traceback.print_exc()
input("Failure")
"""def load_palette(filename):
im = Image.open(filename)
pal = im.getpalette()
print(pal)
return pal
#This doesn't work because getpalette doesn't return an useable palette data for quantize..."""