forked from chickens2/pony-comic-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateComic.py
622 lines (551 loc) · 17.4 KB
/
generateComic.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# coding=UTF-8
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# vim: set fileencoding=UTF-8 :
import pyperclip
import random
import sys
import io
from imgurpython import ImgurClient
import configparser
from PIL import Image, ImageFont, ImageDraw
from pprint import pprint
import generatePanel
import findEmote
import textwrap
import re
import praw
import urllib.request, urllib.parse, urllib.error
import os
import json
import getopt
from getch import getch
import utilFunctions
import string
#command line options
textFileChat = None
specifiedBackground = None
specifiedTitle = None
nextToFill = None
for arg in sys.argv:
if arg[0] == '-':
nextToFill = arg[1:].lower()
else:
if nextToFill is not None:
if nextToFill[0] == 'f':
textFileChat = arg
if nextToFill[0] == 'b':
specifiedBackground = arg
if nextToFill[0] == 't':
specifiedTitle = arg
nextToFill = None
config = configparser.ConfigParser(inline_comment_prefixes=(';',))
config.readfp(open('config.cfg'))
# Load things from the config file
selectedBackground = None
fntLarge = ImageFont.truetype(config.get('Fonts','title_font'), int(config.get('Fonts','title_size'))) # used for the title
fntSmall = ImageFont.truetype(config.get('Fonts','cast_font'), int(config.get('Fonts','cast_size'))) # used for the list of characters
anonymousMode = config.get('Options','anonymous_mode').upper()=='TRUE'
uploadImgur = config.get('Options','upload_imgur').upper()=='TRUE'
castIntro = config.get('Options','cast_introduction')
repeatMode = config.get('Options','keep_window_open').upper()=='TRUE'
closeupMultiplier = config.getfloat('Options','closeup_zoom')
allowDuplicates = config.get('Options','allow_duplicates').upper()=='TRUE'
rainbowCast = config.get('Options','rainbow_cast').upper()=='TRUE'
debugprint = config.get('Options','terminal_debug').upper()=='TRUE'
removebot = config.get('Options','remove_bot_commands').upper()=='TRUE'
ignored_users = config.get('Ignore', 'ignored_nicks').split()
defaultseed = config.get('Options','default_seed')
if debugprint is True:
print('Verbose mode activated!')
print(('chat from log: '+str(textFileChat)))
uploadReddit = None
reddit = None
if config.has_section('praw') and len(config.get('praw','clientid'))>2:
#print 'praw stuff:'+config.get('praw','clientid')+"^"
uploadReddit=config.get('praw','upload')
reddit = praw.Reddit(client_id=config.get('praw','clientid'),
client_secret=config.get('praw','clientsecret'),
user_agent='user agent',
username=config.get('praw','username'),
password=config.get('praw','password'))
print(('reddit credentials:'+str(config.get('praw','clientsecret'))+" "+config.get('praw','clientid')))
imgareio = '174becf08a64efc'
imgscrioertu = 'c47422a4a3a7a4aab366b88634bcc03a0ffcaa60'
client = ImgurClient(imgareio, imgscrioertu)
#direct print output to log file
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = open("log.txt", "w")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
sys.stdout = Logger()
# don't call this unless you're calling this routine directly
if __name__ == "__main__":
try:
urllib.request.urlretrieve(
'https://raw.githubusercontent.com/chickens2/pony-comic-generator/master/DEFAULT_ALIAS_DO_NOT_EDIT.cfg',
'DEFAULT_ALIAS_DO_NOT_EDIT.cfg'
)
except:
print('could not retrieve default alias list, using local version')
config2 = configparser.ConfigParser()
config2.readfp(open('DEFAULT_ALIAS_DO_NOT_EDIT.cfg'))
allNames = dict(config2.items('Aliases'))
# Set up names list
allText = ""
lines = []
allNames.update(dict(config.items('Aliases'))) #add aliases from config.cfg, overwrite defaults
allNames2 = {}
for key in list(allNames.keys()):
allNames2[key.lower()] = allNames[key].lower()
allNames = allNames2
if debugprint is True:
print('final alias list: ')
pprint(allNames)
names = {}
# Pick a title for the strip
def getTitle(specifiedTitle=None, seed=None):
if specifiedTitle is not None:
return specifiedTitle
if seed is None:
seed = defaultseed
random.seed(seed)
title = None
print('getting title, lines are: '+str(lines))
if len(lines) == 0:
line = ""
else:
line = random.choice(lines)
words = line['text'].split(" ")
if len(words) > 5:
words = words[-5:]
while len(words) > 2 and random.random() > 0.4:
print((words[0]))
del words[0]
title = string.capwords(" ".join(words))
#title=textwrap.wrap(title, width=15)
if debugprint is True:
print(('title '+str(title)))
return title
# Makes the title panel
def createTitlePanel(panelSize, castlist, specifiedTitle=None):
global comic_title
comic_title = getTitle(
specifiedTitle,
"+".join(list(castlist.keys())) + ':'.join(list(castlist.values()))
)
img = Image.new("RGBA", panelSize, (255,255,255)) # white background
d = ImageDraw.Draw(img)
newh = utilFunctions.drawCenteredText(25, comic_title, d, fntLarge, panelSize)
newh += 17
spacing = 15
d.text(
(spacing, newh),
castIntro,
font = fntSmall,
fill = utilFunctions.rollColor(11, 12, 3, 252)
) # change to (0,0,0,255) for always black text
newh += spacing
print('title panel!!!')
# Go through the cast list and add them to the title panel
for name in list(castlist.keys()):
character = castlist[name][1:]
moreSpacing = 0
text = character
if not anonymousMode:
text += " as " + name
# Add a nice cute icon…
filepath = 'tagicons/' + character + '.png'
print(('icon filepath: '+str(filepath)))
# …if it exists
if os.path.isfile(filepath):
moreSpacing = 15
profile = Image.open(filepath).convert('RGBA')
box = (0, newh, profile.size[0], newh+profile.size[1])
img.paste(profile, box, mask=profile)
if rainbowCast is True:
fntClr = utilFunctions.rollColor(1, 2, 12, 249)
else:
fntClr = (0, 0, 0, 255)
# Print the text
d.text(
(spacing + moreSpacing + 5, newh),
text,
font = fntSmall,
fill = fntClr
) # change to (0,0,0,255) for consistently black text
newh += spacing + moreSpacing
generatePanel.drawBorder(img)
return img#img.show()
prevNames=[] # not sure what this is doing here
#
def createNextPanel(txtLines, panelSize, smallPanels, nameorder, selectedBackground, closeup=True):
global prevNames
dialogueOptions = [0]
generatePanel.setPS(panelSize, "generateComic:createNextPanel")
print(('nameorder: '+str(nameorder)))
print(('gppanelsize '+str(generatePanel.panelSize)+" "+str(panelSize)))
if len(txtLines) < 1:
print("No text sent; drawing empty panel!!")
return generatePanel.drawPanelNoDialogue(
{},
selectedBackground,
str(len(nameorder))
)
if (len(txtLines) > 1 and
txtLines[1]['name'] != txtLines[0]['name'] and
generatePanel.hasRoomForDialogue2(txtLines[0]['text'], txtLines[1]['text'])):
dialogueOptions.append(1)
if (len(txtLines) > 2 and
txtLines[2]['name'] != txtLines[1]['name'] and
txtLines[0]['name'] != txtLines[2]['name'] and generatePanel.hasRoomForDialogue3(
txtLines[0]['text'],
txtLines[1]['text'],
txtLines[2]['text']
)):
dialogueOptions.append(2)
random.seed(str(txtLines))
dialogueChoice = random.choice(dialogueOptions)
panel = None
currentNames = {}
prevNames = {}
print(('prevnames to start '+str(dialogueChoice)+" "+str(list(range(dialogueChoice)))))
pprint(prevNames)
for i in range(dialogueChoice + 1):
name = txtLines[i]['name']
pony = txtLines[i]['pony']
print(('should be removing name '+name))
if name in list(prevNames.keys()):
prevNames.pop(name)
currentNames[name] = pony
print('prevnames after')
pprint(prevNames)
for name in list(prevNames.keys()):
if dialogueChoice >= 2:
break
else:
dialogueChoice += 1
txtLines.insert(0, {'name':name, 'text':'', 'pony':allNames[name]})
#currentNames.append(name)
print('txtLines: ')
pprint(txtLines)
for line in txtLines:
if line.get('pony', None) is None:
raise ModuleNotFoundError(str(line)+" does not contain a pony!!!!!! Quitting.")
sys.exit(7)
if dialogueChoice == 0:
panel = generatePanel.drawPanel1Character(
txtLines[0]['pony'],
txtLines[0]['text'],
selectedBackground,
iscloseup=closeup
)
del txtLines[0]
if dialogueChoice == 1:
print(('iscorrectorder: ' + str(utilFunctions.isCorrectOrder(
txtLines[0],
txtLines[1],
nameorder
))))
if utilFunctions.isCorrectOrder(txtLines[0], txtLines[1], nameorder):
panel = generatePanel.drawPanel2Characters(
txtLines[0]['pony'],
txtLines[1]['pony'],
txtLines[0]['text'],
txtLines[1]['text'],
selectedBackground,
iscloseup=closeup
)
else:
panel = generatePanel.drawPanel2Characters(
txtLines[1]['pony'],
txtLines[0]['pony'],
txtLines[0]['text'],
txtLines[1]['text'],
selectedBackground,
textOrder=1,
iscloseup=closeup
)
del txtLines[0]
del txtLines[0]
if dialogueChoice == 2:
panel = generatePanel.drawPanel3Characters(
txtLines[0]['pony'],
txtLines[1]['pony'],
txtLines[2]['pony'],
txtLines[0]['text'],
txtLines[1]['text'],
txtLines[2]['text'],
selectedBackground,
iscloseup=closeup
)
del txtLines[0]
del txtLines[0]
del txtLines[0]
prevNames = currentNames
print(('returning panel '+str(panel)+" "+str(dialogueChoice)))
return panel
# selects which background image to use
def selectBackground(seed, specifiedBackground=None):
if seed is None:
seed = defaultseed
random.seed(seed)
if specifiedBackground is not None: # let command-line switches specify a background
if debugprint is True:
print("Using pre-selected background")
return specifiedBackground
BAD_FILES = config.get('Ignore', 'banned_backgrounds').split()
if config.has_section('Backgrounds') is False or config.options('Backgrounds') == []:
return utilFunctions.pickNestedFile('backgrounds', BAD_FILES)
else:
folderTable = {}
for folder in config.options('Backgrounds'):
folderTable[folder] = config.getint('Backgrounds', folder)
directory = utilFunctions.weightedDictPick(utilFunctions.genProbabilityDict(folderTable))
if debugprint is True:
print(folderTable)
print((utilFunctions.genProbabilityDict(folderTable)))
print(("Choosing from directory " + directory))
return utilFunctions.pickNestedFile('backgrounds/' + directory, BAD_FILES)
# namelist is an output variable
# returns the number largest number of lines in a row a single nick says
def processLines(file, namelist):
lines = []
for line in file:
newline = utilFunctions.cleanupline(
line,
namelist,
ignored_users=ignored_users,
params={'debug':debugprint,'bot':removebot}
)
if newline is not None:
lines.append(newline)
return lines
#
def getPonyList(namelist, presetList = None):
horse_assignments = {}
processedNicks = [] # exists to eliminate repeated calls to list(horse_assignments.keys())
assignedPonies = [] # exists to eliminate repeated calls to list(horse_assignments.values())
# 1st round: assign ponies from the preset list
for nick in namelist:
nick = nick.lower()
if nick in processedNicks:
continue
if nick in {prenick.lower() for prenick in presetList}:
processedNicks.append(nick)
horsepick = presetList[nick]
horse_assignments[nick] = horsepick
assignedPonies.append(horsepick)
print(("Pre-assigned ponies "+str(assignedPonies)+" for nicks "+str(processedNicks)))
# 2nd round: assign ponies to the rest of the nicks
for nick in namelist:
nick = nick.lower()
if nick.lower() in processedNicks:
continue
pony = findEmote.select_horse(nick, assignedPonies, unique=(allowDuplicates==False))
horse_assignments[nick] = pony
assignedPonies.append(pony)
processedNicks.append(nick)
return horse_assignments
# lines is an output variable
def ponies2lines(ponylist, lines):
currentName = None
mostInRow = 0
currentInRow = 1
# The next 3 lines are the minimal functionality; everything else here is for tracking
for line in lines:
if line is None:
print("Skipping junk line")
continue
name = line["name"]
line["pony"] = ponylist[name]
# Tracking for other uses later
if name == currentName:
currentInRow += 1
if currentInRow > mostInRow:
mostInRow = currentInRow
else:
currentInARow = 1
currentName = name
return mostInRow
# processes the chat log for comic generation
def processChatLog(file, specifiedBackground=None, specifiedTitle=None, debugprint=False):
global lines
global allNames
global transform_D
global undoTransform_D
transform_D,undoTransform_D = utilFunctions.genTransformDict()
if debugprint is True:
print('original allnames:')
pprint(allNames)
text = ''.join(file)
findEmote.defaultSeed = text
mostInARow = 1
currentInARow = 1
nameOrder = []
linenumber = 1
ponylist = list(allNames.values())
lines = processLines(file, nameOrder)
ponyAssignments = getPonyList(nameOrder, allNames)
mostInARow = ponies2lines(ponyAssignments, lines)
generatePanel.names = nameOrder
if debugprint is True:
print('the final names list: ')
pprint(ponyAssignments)
print('Order of names')
pprint(nameOrder)
print(('Empty names? '+str(nameOrder=={})))
print(('most in a row: '+str(mostInARow)))
if anonymousMode:
for line in lines:
line['text'] = utilFunctions.anonWord(
line['text'],
{**allNames, **ponyAssignments},
joiner = ' ',
debugprint = debugprint
)
if debugprint is True:
print(("Processed line: "+line['text']))
# this is a bad hack probably
# idk how else to do it without adding a million extra parameters everywhere
#print 'lines:'
#pprint(lines)
#return
selectedBackground = selectBackground(text, specifiedBackground)
print(("Background is "+selectedBackground))
#pprint(names)
global allText
allText = text
#print 'at1'+str(allText)+'^'
if mostInARow > 3:
smallPanels = True
panelSize = (200, 200)
else:
smallPanels = False
panelSize = (300, 300)
tp = createTitlePanel(panelSize, ponyAssignments, specifiedTitle)
print(('generatecomic panelsize: '+str(panelSize)))
#tp.show()
panels = []
panels.append(tp)
txtLines = lines # rename here since there seems to be a naming convention shift
txtLines2 = lines[:99] # needed for redone opening panels. All those extra lines are to get it to work properly—unexpected results may occur if your comic is over 99 lines long
print(txtLines)
while len(txtLines) > 0:
panels.append(createNextPanel(
txtLines,
panelSize,
smallPanels,
nameOrder,
selectedBackground
))
panelsAcross = 2
if smallPanels:
panelsAcross = 3
# Set random seed for the rest of this function
if len(nameOrder) > 0:
random.seed(selectedBackground.join(nameOrder))
else:
random.seed(defaultseed)
# if it needs an establishing shot with no dialogue
# This is the preference for 2-wide comics
# 3-wide comics should only have an empty opening shot if two extra panels are needed
print((str(len(panels))+" panels, "+str(panelsAcross)+" wide"))
if len(panels) % panelsAcross == 1 or len(nameOrder) == 0:
print("Adding establishing shot")
horselist = []
for name in nameOrder[:min(3,random.randrange(len(nameOrder))+1)]:
horselist.append(ponyAssignments[name])
panels.insert(
1,
generatePanel.drawPanelNoDialogue(
horselist,
selectedBackground,
text+str(len(panels))
))
else: #otherwise redo the first panel as zoomed out
print("Doing a re-shoot of panel 1")
del panels[1]
panels.insert(
1,
createNextPanel(
txtLines2,
panelSize,
smallPanels,
nameOrder,
selectedBackground,
closeup=False)
)
# if there still aren't enough panels, pad the end
while len(panels)%panelsAcross != 0 and nameOrder != {}:
print("Adding an end panel")
horselist = []
random.seed(str(prevNames))
for name in prevNames:
horselist.append(ponyAssignments[name])
if len(horselist) > 1 and utilFunctions.rollOdds(2):
random.shuffle(horselist)
horselist = horselist[:-1]
panels.append(generatePanel.drawPanelNoDialogue(
horselist,
selectedBackground,
str(len(panels))
))
maxWidth = panelSize[0]*panelsAcross
currentHeight = 0
currentWidth = 0
img = Image.new('RGBA', (maxWidth, panelSize[1]*len(panels)//panelsAcross))
print(('comic dimensions: ' + str(img.size)))
print(('small panels: ' + str(smallPanels)))
print('panels:')
pprint(panels)
for panel in panels:
if panel is None:
print("Skipping bugged-out panel!!!!!!!")
continue
panel = utilFunctions.possiblyTransform(panel, 999) # Around 1/200 strips will have a flipped panel
box = (
currentWidth,
currentHeight,
currentWidth + panel.size[0],
panel.size[1] + currentHeight
)
print(('making panel at: ' + str(box)))
img.paste(panel, box)
currentWidth += panel.size[0]
if currentWidth >= maxWidth:
currentWidth = 0
currentHeight += panel.size[1]
#img.show()
img = utilFunctions.possiblyTransform(img, 420) # Rotating the entire comic should be rarest of all
img.save("comic.jpg", "JPEG")
def main():
global comic_title
print("Main method")
chatfile = None
if textFileChat is None:
clipboard = pyperclip.paste()
if debugprint is True:
print(('clipboard is: \n' + str(clipboard)))
chatfile = io.StringIO(clipboard).readlines()
random.seed(clipboard)
else:
chatfile = open(textFileChat).readlines()
random.seed(open(textFileChat))
processChatLog(chatfile, specifiedBackground, specifiedTitle, debugprint)
if uploadImgur:
image = client.upload_from_path('comic.jpg')
pyperclip.copy(image['link'])
print((image['link']))
if uploadReddit:
thetitle = comic_title
print('title ' + thetitle + " link " + image['link'])
reddit.subreddit("beniscity").submit(title=thetitle, url=image['link'])
if repeatMode:
print('press any key to continue')
g=getch()()
if __name__ == "__main__":
main()