song_slides.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. Copyright © 2022 Noah Vogt <noah@noahvogt.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. """
  14. from os import path
  15. from wand.exceptions import BlobError
  16. from utils import (
  17. log,
  18. error_msg,
  19. )
  20. import config as const
  21. def generate_song_slides(
  22. slidegen,
  23. slide_count,
  24. template_img,
  25. zfill_length,
  26. ) -> None:
  27. log("generating song slides...")
  28. # unique_structures: list = list(set(chosen_structure))
  29. current_slide_index: int = 0
  30. for index, structure in enumerate(slidegen.chosen_structure):
  31. structure_element_splitted: list = slidegen.songtext[
  32. structure
  33. ].splitlines()
  34. line_count = len(structure_element_splitted)
  35. use_line_ranges_per_index = []
  36. use_lines_per_index = []
  37. if line_count <= const.STRUCTURE_ELEMENT_MAX_LINES:
  38. inner_slide_count = 1
  39. else:
  40. inner_slide_count: int = (
  41. line_count // const.STRUCTURE_ELEMENT_MAX_LINES + 1
  42. )
  43. use_lines_per_index = [
  44. line_count // inner_slide_count
  45. ] * inner_slide_count
  46. for inner_slide in range(inner_slide_count):
  47. if sum(use_lines_per_index) == line_count:
  48. break
  49. use_lines_per_index[inner_slide] = (
  50. use_lines_per_index[inner_slide] + 1
  51. )
  52. for inner_slide in range(inner_slide_count):
  53. use_line_ranges_per_index.append(
  54. sum(use_lines_per_index[:inner_slide])
  55. )
  56. for inner_slide in range(inner_slide_count):
  57. current_slide_index += 1
  58. log(
  59. "generating song slide [{} / {}]...".format(
  60. current_slide_index, slide_count
  61. )
  62. )
  63. if inner_slide_count == 1:
  64. structure_element_value: str = slidegen.songtext[structure]
  65. else:
  66. splitted_wanted_range: list = structure_element_splitted[
  67. use_line_ranges_per_index[
  68. inner_slide
  69. ] : use_line_ranges_per_index[inner_slide]
  70. + use_lines_per_index[inner_slide]
  71. ]
  72. structure_element_value: str = ""
  73. for element in splitted_wanted_range:
  74. structure_element_value += element + "\n"
  75. structure_element_value = structure_element_value[:-1]
  76. song_slide = slidegen.song_slide_form()
  77. song_slide_img = song_slide.get_slide(
  78. template_img,
  79. structure_element_value,
  80. slidegen.chosen_structure,
  81. index,
  82. bool(
  83. inner_slide_count != 1
  84. and inner_slide != inner_slide_count - 1
  85. ),
  86. )
  87. song_slide_img.format = const.IMAGE_FORMAT
  88. try:
  89. song_slide_img.save(
  90. filename=path.join(
  91. slidegen.output_dir,
  92. const.FILE_NAMEING
  93. + str(current_slide_index + 1).zfill(zfill_length)
  94. + "."
  95. + const.FILE_EXTENSION,
  96. )
  97. )
  98. except BlobError:
  99. error_msg("could not write slide to target directory")