mirror of
https://gitlab.com/oloturia/fumcaso.git
synced 2024-11-01 01:38:16 +01:00
158 lines
5.2 KiB
Python
Executable file
158 lines
5.2 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
from PIL import Image
|
|
from PIL import ImageFont
|
|
from PIL import ImageDraw
|
|
import csv
|
|
import random
|
|
import os
|
|
|
|
fileDir = os.path.dirname(os.path.abspath(__file__))
|
|
fileDir = fileDir +"/"
|
|
|
|
|
|
def replaceText(text):
|
|
"""This function replace $WILDCARD with a word found in subs.csv
|
|
subs.csv definition is 1st colum $WILDCARD, subsequent columns, possible values (chosen at random), delimiter is ;"""
|
|
with open(fileDir+"subs.csv") as subs:
|
|
csvReader = csv.reader(subs,delimiter=";")
|
|
for row in csvReader:
|
|
if text.find(row[0]) != -1:
|
|
text = text.replace(row[0],row[random.randint(1,len(row)-1)],1)
|
|
return text
|
|
|
|
def fetchText(indText):
|
|
"""This function fetch the text for the image with two characters
|
|
rtext.csv definition is: 1st column the name of the file (i.e. B001.png), 2nd number of actors (at the moment
|
|
they are limited to two; then a couple of columns or each actor with x and y coord of the strings; after the coords the outcomes,
|
|
one column for each actor
|
|
Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text)
|
|
It returns two arrays, coords is a tuple (x,y) and result is the outcome"""
|
|
with open(fileDir+"rtext.csv") as rtext:
|
|
csvReader = csv.reader(rtext,delimiter=';')
|
|
for row in csvReader:
|
|
if row[0]==indText:
|
|
noActors = int(row[1])
|
|
if noActors == 0:
|
|
return 0
|
|
else:
|
|
firstElement = 2+(noActors*2)
|
|
lastElement = len(row)-(noActors-1)
|
|
randQuote = random.randrange(firstElement,lastElement,noActors)
|
|
coords = []
|
|
result = []
|
|
for x in range(0,noActors):
|
|
coords.append((row[2+x*2],row[3+x*2]))
|
|
result.append(row[randQuote+x])
|
|
return coords,result
|
|
|
|
|
|
def fetchVign():
|
|
"""This functions fetch an image, randomly, chosen from a markov tree defined in ram.csv
|
|
ram.csv definition is: 1st column the name of the image (without extension), subsequent columns, possible outcomes chosen randomly
|
|
It returns an array with the file names"""
|
|
starts = []
|
|
startdest = []
|
|
nvign = 0
|
|
currVign = "000"
|
|
story = []
|
|
with open(fileDir+"ram.csv") as ram:
|
|
csvReader = csv.reader(ram)
|
|
for row in csvReader:
|
|
starts.append(row[0])
|
|
startdest.append(row)
|
|
while nvign <100:
|
|
story.append(startdest[starts.index(currVign)][random.randint(1,len(startdest[starts.index(currVign)])-1)])
|
|
currVign = story[nvign]
|
|
if currVign == "END":
|
|
return story
|
|
story[nvign]+=".png"
|
|
nvign +=1
|
|
print("tree with no END")
|
|
quit()
|
|
|
|
def addThing(indVign):
|
|
"""This function adds a small image (object) to a larger image
|
|
obj.csv definition is: name of the image (i.e. A001.png), x-coord, y-coord, subsequent columns possible outcomes
|
|
It returns a tuple (object file name, x, y)"""
|
|
with open(fileDir+"obj.csv") as obj:
|
|
csvReader = csv.reader(obj)
|
|
for row in csvReader:
|
|
if row[0] == indVign:
|
|
return row[random.randint(3,len(row)-1)],row[1],row[2]
|
|
return 0
|
|
|
|
def writeStrip(story,fontSize):
|
|
"""This function creates the strip returning an image object that could be saved or viewed. It takes an array with filenames as parameter
|
|
The first image is always 000, then appends to strip the files, then decorates it fetching text and adding objects. If the object is an R, then
|
|
repeats the last object."""
|
|
strip = []
|
|
for indVign in story:
|
|
try:
|
|
vign = Image.open(fileDir+indVign).convert('RGBA')
|
|
addtext = ImageDraw.Draw(vign)
|
|
fnt = ImageFont.truetype(fileDir+"ubuntu.ttf",fontSize)
|
|
textVign = fetchText(indVign)
|
|
|
|
if textVign!=0:
|
|
for x in range(len(textVign[0])):
|
|
text_vign = textVign[1][x]
|
|
while text_vign.find('$') != -1:
|
|
text_vign = replaceText(text_vign)
|
|
text_vign = text_vign.replace('@','\n')
|
|
addtext.multiline_text((int(textVign[0][x][0]),int(textVign[0][x][1])),text_vign,fill="#000000",font=fnt,align="center")
|
|
|
|
obj = addThing(indVign)
|
|
if obj!=0:
|
|
if obj[0] == 'R':
|
|
objImg = Image.open(fileDir+prevObj[0])
|
|
else:
|
|
prevObj = obj
|
|
objImg = Image.open(fileDir+obj[0])
|
|
vign.paste(objImg,(int(obj[1]),int(obj[2])))
|
|
strip.append(vign)
|
|
except FileNotFoundError:
|
|
pass
|
|
image = Image.new('RGBA',(2400,500))
|
|
xshift=0
|
|
for vign in strip:
|
|
image.paste(vign,(xshift,0))
|
|
xshift += 600
|
|
return image
|
|
|
|
def createStrip(name,fontSize=22):
|
|
"""Create strip and save it
|
|
createStrip(str path/filename)"""
|
|
try:
|
|
story = fetchVign()
|
|
finalStrip = writeStrip(story,fontSize)
|
|
if name == "android":
|
|
return finalStrip
|
|
else:
|
|
finalStrip.save(fileDir+name)
|
|
return 0
|
|
except Exception as err:
|
|
return err
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-s','--story',metavar='story',default='',nargs=4,help='name of the images')
|
|
parser.add_argument('-m','--multiple',metavar='multiple',default=[1],nargs=1,type=int,help='multiple output (int >0)')
|
|
parser.add_argument('-x','--xsize',metavar='xsize',default=0,type=int,nargs=1,help='resize image x')
|
|
args = parser.parse_args()
|
|
if args.multiple[0] <= 0:
|
|
quit()
|
|
for x in range(0,args.multiple[0]):
|
|
if (args.story == ''):
|
|
story = fetchVign()
|
|
else:
|
|
story = []
|
|
for x in args.story:
|
|
story.append(x)
|
|
print(story)
|
|
finalStrip = writeStrip(story,22)
|
|
if args.multiple[0] == 1:
|
|
if args.xsize == 0:
|
|
finalStrip.show()
|
|
else:
|
|
finalStrip.resize((args.xsize[0],int(args.xsize[0]/2400*500))).show()
|