hivecorp commited on
Commit
fe7e13c
·
verified ·
1 Parent(s): 4cfd52e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -74,26 +74,22 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
74
  with Drawing() as draw:
75
  draw.font = font_path
76
  draw.font_size = text_size
77
- draw.fill_color = Color(text_color)
78
  draw.text_alignment = 'center'
79
  draw.text_interline_spacing = 10
80
-
81
  with Image(width=video_width, height=video_height, background=Color(bg_color)) as img:
82
  lines = page.split("\n")
83
- # Centering text vertically
84
- total_text_height = len(lines) * (text_size + 10) # Height of text area
85
- start_y = (video_height - total_text_height) // 2 # Start position to center vertically
86
-
87
  for j, line in enumerate(lines):
88
- draw.text(int(video_width / 2), start_y + (j * (text_size + 10)), line)
89
-
90
- draw(img) # Apply the drawing to the image
 
91
  img.format = 'png'
92
  img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
93
  img.save(filename=img_path)
94
  text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
95
  video_clips.append(text_clip)
96
-
97
  # 合并所有视频片段
98
  final_video = concatenate_videoclips(video_clips)
99
  final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
@@ -120,7 +116,7 @@ async def create_demo():
120
  gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
121
  gr.ColorPicker(value="#000000", label="Background Color"),
122
  gr.ColorPicker(value="#FFFFFF", label="Text Color"),
123
- gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
124
  gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
125
  ],
126
  outputs=[
@@ -139,4 +135,4 @@ async def create_demo():
139
  # 运行应用
140
  if __name__ == "__main__":
141
  demo = asyncio.run(create_demo())
142
- demo.launch()
 
74
  with Drawing() as draw:
75
  draw.font = font_path
76
  draw.font_size = text_size
77
+ draw.fill_color = Color(text_color) # Ensure text color is contrasting
78
  draw.text_alignment = 'center'
79
  draw.text_interline_spacing = 10
 
80
  with Image(width=video_width, height=video_height, background=Color(bg_color)) as img:
81
  lines = page.split("\n")
 
 
 
 
82
  for j, line in enumerate(lines):
83
+ # Calculate y position based on the line index
84
+ y_position = (video_height / 2) - ((len(lines) * (text_size + 10)) / 2) + j * (text_size + 10)
85
+ draw.text(int(video_width / 2), y_position, line)
86
+ draw(img) # Correctly draw the text onto the image
87
  img.format = 'png'
88
  img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
89
  img.save(filename=img_path)
90
  text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
91
  video_clips.append(text_clip)
92
+
93
  # 合并所有视频片段
94
  final_video = concatenate_videoclips(video_clips)
95
  final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
 
116
  gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
117
  gr.ColorPicker(value="#000000", label="Background Color"),
118
  gr.ColorPicker(value="#FFFFFF", label="Text Color"),
119
+ gr.Textbox(label="Text Font", value="msyh.ttf"), # Ensure this path is correct
120
  gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
121
  ],
122
  outputs=[
 
135
  # 运行应用
136
  if __name__ == "__main__":
137
  demo = asyncio.run(create_demo())
138
+ demo.launch(share=True) # Change to share=True if you want a public link