randstrip.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. from PIL import Image
  3. from PIL import ImageFont
  4. from PIL import ImageDraw
  5. import csv
  6. import random
  7. import os
  8. fileDir = os.path.dirname(os.path.abspath(__file__))
  9. fileDir = fileDir +"/"
  10. def replaceText(text):
  11. """This function replace $WILDCARD with a word found in subs.csv
  12. subs.csv definition is 1st colum $WILDCARD, subsequent columns, possible values (chosen at random), delimiter is ;"""
  13. with open(fileDir+"subs.csv") as rtext:
  14. csvReader = csv.reader(rtext,delimiter=";")
  15. for row in csvReader:
  16. if text.find(row[0]) != -1:
  17. text = text.replace(row[0],row[random.randint(1,len(row)-1)])
  18. return text
  19. def fetchText(indText):
  20. """This function fetch the text for the image with just only one character
  21. rtext.csv definition is: 1st column the name of the file (i.e. A001.png), 2nd x-coord, 3rd y-coord, 4th and subsequent, the possible outcomes
  22. Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text)
  23. It returns a tuple (x,y,text)"""
  24. with open(fileDir+"rtext.csv") as rtext:
  25. csvReader = csv.reader(rtext,delimiter=';')
  26. for row in csvReader:
  27. if row[0]==indText:
  28. if len(row)>2:
  29. return row[1],row[2],row[random.randint(3,len(row)-1)].replace('@','\n')
  30. else:
  31. return 0
  32. def fetch2Text(indText):
  33. """This function fetch the text for the image with two characters
  34. rtext.csv definition is: 1st column the name of the file (i.e. B001.png), 2nd x-coord, 3rd y-coord of the first string
  35. 4th x-coord, 5th y-coord of the second string, 6th and subsequent are the outcomes, alternated as the odd one is an
  36. answer to the even one
  37. Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text)
  38. It returns a tuple(x1,y1,x2,y2,text1,text2)"""
  39. with open(fileDir+"r2text.csv") as rtext:
  40. csvReader = csv.reader(rtext,delimiter=';')
  41. for row in csvReader:
  42. if row[0]==indText:
  43. if len(row)>2:
  44. rand1 = random.randint(5,len(row)-1)
  45. if rand1 %2 == 0:
  46. rand1 -=1
  47. rand2 = rand1+1
  48. return row[1],row[2],row[3],row[4],row[rand1].replace('@','\n'),row[rand2].replace('@','\n')
  49. else:
  50. return 0
  51. def fetchVign():
  52. """This functions fetch an image, randomly, chosen from a markov tree defined in ram.csv
  53. ram.csv definition is: 1st column the name of the image (without extension), subsequent columns, possible outcomes chosen randomly
  54. It returns an array with the file names"""
  55. starts = []
  56. startdest = []
  57. nvign = 0
  58. currVign = "000"
  59. story = []
  60. with open(fileDir+"ram.csv") as ram:
  61. csvReader = csv.reader(ram)
  62. for row in csvReader:
  63. starts.append(row[0])
  64. startdest.append(row)
  65. while nvign <= 3:
  66. story.append(startdest[starts.index(currVign)][random.randint(1,len(startdest[starts.index(currVign)])-1)])
  67. currVign = story[nvign]
  68. if story[nvign] == "B00":
  69. story[nvign] += "."
  70. story[nvign] += str(random.randint(0,2))
  71. story[nvign]+=".png"
  72. nvign +=1
  73. return story
  74. def addThing(indVign):
  75. """This function adds a small image (object) to a larger image
  76. obj.csv definition is: name of the image (i.e. A001.png), x-coord, y-coord, subsequent columns possible outcomes
  77. It returns a tuple (object file name, x, y)"""
  78. with open(fileDir+"obj.csv") as obj:
  79. csvReader = csv.reader(obj)
  80. for row in csvReader:
  81. if row[0] == indVign:
  82. return row[random.randint(3,len(row)-1)],row[1],row[2]
  83. return 0
  84. def writeStrip(story):
  85. """This function creates the strip returning an image object that could be saved or viewed. It takes an array with filenames as parameter
  86. The first image is always 000, then appends to strip the files, then decorates it fetching text and adding objects"""
  87. strip = []
  88. for indVign in story:
  89. if indVign!="000":
  90. vign = Image.open(fileDir+indVign).convert('RGBA')
  91. addtext = ImageDraw.Draw(vign)
  92. fnt = ImageFont.truetype(fileDir+"ubuntu.ttf",16)
  93. if indVign[0] == 'A':
  94. textVign = fetchText(indVign)
  95. if textVign !=0:
  96. text1 = textVign[2]
  97. if text1.find('$') != -1:
  98. text1 = replaceText(text1)
  99. addtext.multiline_text((int(textVign[0]),int(textVign[1])),text1,fill="#000000",font=fnt,align="center")
  100. else:
  101. textVign = fetch2Text(indVign)
  102. if textVign!=0:
  103. text1 = textVign[4]
  104. text2 = textVign[5]
  105. if text1.find('$') != -1:
  106. text1 = replaceText(text1)
  107. if text2.find('$') != -1:
  108. text2 = replaceText(text2)
  109. addtext.multiline_text((int(textVign[0]),int(textVign[1])),text1,fill="#000000",font=fnt,align="center")
  110. addtext.multiline_text((int(textVign[2]),int(textVign[3])),text2,fill="#000000",font=fnt,align="center")
  111. obj = addThing(indVign)
  112. if obj!=0:
  113. objImg = Image.open(fileDir+obj[0])
  114. vign.paste(objImg,(int(obj[1]),int(obj[2])))
  115. strip.append(vign)
  116. image = Image.new('RGBA',(2400,500))
  117. xshift=0
  118. for vign in strip:
  119. image.paste(vign,(xshift,0))
  120. xshift += 600
  121. return image
  122. def createStrip(name):
  123. """Create strip and save it
  124. createStrip(str path/filename)"""
  125. try:
  126. story = fetchVign()
  127. finalStrip = writeStrip(story)
  128. finalStrip.save(name)
  129. return 0
  130. except Exception as err:
  131. return err
  132. if __name__ == "__main__":
  133. story = fetchVign()
  134. finalStrip = writeStrip(story)
  135. finalStrip.show()