images.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. def get_verse_image(verse):
  2. """создает картинку, сохраняет в файл и возвращает имя файла"""
  3. verse_lines = verse['text'].split('\n')
  4. shift = 0
  5. one_stoke = False
  6. space_mult = 1
  7. if len(verse_lines) == 4:
  8. shift = 90
  9. elif len(verse_lines) == 1:
  10. one_stoke = True
  11. words = verse_lines[0].split(" ")
  12. lines = len(words) // 5
  13. if len(words) % 5 != 0:
  14. lines += 1
  15. verse_lines = []
  16. for l in range(lines):
  17. if l == lines - 1:
  18. verse_lines.append(" ".join(words[l*5:]))
  19. else:
  20. verse_lines.append(" ".join(words[l*5:l*5+5]))
  21. print(verse_lines)
  22. elif len(verse_lines) == 3:
  23. shift = 100
  24. elif len(verse_lines) == 6:
  25. shift = -10
  26. space_mult = 0.8
  27. if len(verse['text']) > 160:
  28. shift = 10
  29. space_mult = 0.9
  30. y_pos = 120
  31. lines = ""
  32. for line in verse_lines:
  33. splited_line = split_line(line)
  34. l = f'<tspan x="50%" y="{y_pos + shift}">{splited_line[0]}</tspan>'
  35. l1 = ""
  36. if len(splited_line) == 2:
  37. anchor = "middle" if one_stoke else "end"
  38. x_cord = 'x="1500"' if not one_stoke else 'x="50%"'
  39. l1 = f'<tspan {x_cord} y="{y_pos + 90 + shift}" \
  40. text-anchor="{anchor}">{splited_line[1]}</tspan>'
  41. y_pos += 40
  42. lines += f"{l}\n{l1}\n"
  43. y_pos += int(150 * space_mult)
  44. template = f"""
  45. <svg
  46. width="1600"
  47. height="900"
  48. viewBox="0 0 1600 900"
  49. version="1.1"
  50. >
  51. <rect width="100%" height="100%" fill="white" />
  52. <text
  53. style="font-style:italic;font-weight:normal;font-size:75px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#00004f;fill-opacity:1"
  54. dominant-baseline="middle"
  55. text-anchor="middle">
  56. {lines}
  57. <tspan text-anchor="end" style="font-weight:bold;font-size:50px" x="1570" y="880">{verse['title']}</tspan>
  58. </text>
  59. </svg>
  60. """
  61. file_name = f"img/{verse['alias'].replace(' ', '_')}.svg"
  62. with open(file_name, "w") as file:
  63. file.write(template)
  64. return file_name
  65. def split_line(text):
  66. """попытка сделать алгоритм, который делит длинные строки"""
  67. max_line_length = 38
  68. if len(text) > max_line_length:
  69. lines = [" ".join(text.split(" ")[:-1]), text.split(" ")[-1]]
  70. if len(lines[0]) > max_line_length:
  71. lines = [" ".join(text.split(" ")[:-2]), " ".join(text.split(" ")[-2:])]
  72. if len(lines[0]) > max_line_length:
  73. lines = [" ".join(text.split(" ")[:-3]), " ".join(text.split(" ")[-3:])]
  74. if len(lines[0]) > max_line_length:
  75. lines = ["-".join(text.split("-")[:-1]) + "-", text.split("-")[-1]]
  76. if len(lines[0]) > max_line_length:
  77. lines = ["-".join(text.split("-")[:-2]) + "-", "-".join(text.split("-")[-2:])]
  78. if len(lines[0]) > max_line_length:
  79. lines = ["-".join(text.split("-")[:-3]) + "-", "-".join(text.split("-")[-3:])]
  80. if len(lines[0]) > max_line_length:
  81. lines = ["-".join(text.split("-")[:-4]) + "-", "-".join(text.split("-")[-4:])]
  82. return lines
  83. return [text]