prithivMLmods commited on
Commit
c255cb5
1 Parent(s): f6c2653

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -39
app.py CHANGED
@@ -18,42 +18,34 @@ def create_typing_video(code_text, format_choice, line_spacing, width_choice, he
18
 
19
  # Convert font_size to integer
20
  font_size = int(font_size)
 
21
 
22
  video_frames = []
23
 
24
  # Setup initial parameters
25
  image_width, image_height = int(width_choice), int(height_choice)
26
  max_width = image_width - 40 # Margin of 20 pixels on each side
27
- max_height = image_height - 40 # Margin of 20 pixels on top and bottom
28
  current_text = ""
29
 
30
  # Create the background
31
  background = Image.new("RGB", (image_width, image_height), color=background_color)
32
 
33
- # Load and adjust the font
34
- font = ImageFont.truetype(font_path, font_size)
35
-
36
- # Adjust font size if necessary to fit text within image height
37
- def adjust_font_size(text, font, max_width, max_height):
38
- while True:
39
- wrapped_lines = textwrap.wrap(text, width=max_width // font.getbbox(' ')[2])
40
- text_height = sum(font.getbbox(line)[3] - font.getbbox(line)[1] for line in wrapped_lines)
41
-
42
- if text_height <= max_height:
43
- return font
44
-
45
- # Reduce font size
46
- font_size -= 1
47
- font = ImageFont.truetype(font_path, font_size)
48
-
49
- font = adjust_font_size(code_text, font, max_width, max_height)
50
 
51
  # Generate frames for the typing effect
52
  for char in code_text:
53
  current_text += char
54
 
55
  if format_choice == "Paragraph":
56
- wrapped_lines = textwrap.wrap(current_text, width=max_width // font.getbbox(' ')[2])
57
  else: # Programming
58
  wrapped_lines = current_text.splitlines()
59
 
@@ -160,7 +152,7 @@ font_choice = gr.Dropdown(
160
  "COOPBL.TTF",
161
  "Rockwell-Bold.ttf",
162
  "Candara Light.TTF",
163
- "Carlito-Regular.ttf",
164
  "Castellar.ttf",
165
  "Courier New.ttf",
166
  "LSANS.TTF",
@@ -219,23 +211,23 @@ sound_choice = gr.Dropdown(
219
  label="Typing Sound"
220
  )
221
  custom_audio = gr.File(
222
- label="Upload Custom Audio SFX (Optional)"
 
223
  )
224
 
225
  background_color = gr.Dropdown(
226
- choices=["black", "white", "gray", "blue", "green", "red"],
227
  value="black",
228
  label="Background Color"
229
  )
230
 
231
  text_color = gr.Dropdown(
232
- choices=["white", "black", "blue", "orange", "red"],
233
  value="white",
234
  label="Text Color"
235
  )
236
 
237
  enhance_quality = gr.Checkbox(
238
- value=False,
239
  label="Enhance Video Quality"
240
  )
241
 
@@ -245,21 +237,23 @@ video_speed = gr.Dropdown(
245
  label="Video Speed"
246
  )
247
 
248
- interface = gr.Interface(
249
  fn=generate_video,
250
- inputs=[gr.Textbox(lines=10, placeholder="Enter code here...", label="Code Text"),
251
- format_choice,
252
- line_spacing,
253
- width_choice,
254
- height_choice,
255
- font_choice,
256
- font_size,
257
- sound_choice,
258
- custom_audio,
259
- background_color,
260
- text_color,
261
- enhance_quality,
262
- video_speed],
 
 
263
 
264
  outputs=gr.Video(label="Typing Video"),
265
  title="Type Bytes🐧",
@@ -268,4 +262,4 @@ interface = gr.Interface(
268
  )
269
 
270
  if __name__ == "__main__":
271
- interface.launch(share=True)
 
18
 
19
  # Convert font_size to integer
20
  font_size = int(font_size)
21
+ font = ImageFont.truetype(font_path, font_size)
22
 
23
  video_frames = []
24
 
25
  # Setup initial parameters
26
  image_width, image_height = int(width_choice), int(height_choice)
27
  max_width = image_width - 40 # Margin of 20 pixels on each side
 
28
  current_text = ""
29
 
30
  # Create the background
31
  background = Image.new("RGB", (image_width, image_height), color=background_color)
32
 
33
+ # Calculate the maximum width and adjust font size if necessary
34
+ while True:
35
+ wrapped_lines = textwrap.wrap(code_text, width=max_width // font.getlength(' '))
36
+ text_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in wrapped_lines])
37
+
38
+ if text_height <= image_height - 40:
39
+ break
40
+ font_size -= 1
41
+ font = ImageFont.truetype(font_path, font_size)
 
 
 
 
 
 
 
 
42
 
43
  # Generate frames for the typing effect
44
  for char in code_text:
45
  current_text += char
46
 
47
  if format_choice == "Paragraph":
48
+ wrapped_lines = textwrap.wrap(current_text, width=max_width // font.getlength(' '))
49
  else: # Programming
50
  wrapped_lines = current_text.splitlines()
51
 
 
152
  "COOPBL.TTF",
153
  "Rockwell-Bold.ttf",
154
  "Candara Light.TTF",
155
+ "Carlito-Regular.ttf Carlito-Regular.ttf",
156
  "Castellar.ttf",
157
  "Courier New.ttf",
158
  "LSANS.TTF",
 
211
  label="Typing Sound"
212
  )
213
  custom_audio = gr.File(
214
+ label="Upload Custom Audio SFX🔊",
215
+ type="filepath"
216
  )
217
 
218
  background_color = gr.Dropdown(
219
+ choices=["black", "white", "darkblue", "orange", "green"],
220
  value="black",
221
  label="Background Color"
222
  )
223
 
224
  text_color = gr.Dropdown(
225
+ choices=["black", "white", "darkblue", "orange", "green"],
226
  value="white",
227
  label="Text Color"
228
  )
229
 
230
  enhance_quality = gr.Checkbox(
 
231
  label="Enhance Video Quality"
232
  )
233
 
 
237
  label="Video Speed"
238
  )
239
 
240
+ iface = gr.Interface(
241
  fn=generate_video,
242
+ inputs=[
243
+ gr.Textbox(label="Enter Content", lines=10, placeholder="Enter the text to be displayed in the video..."),
244
+ format_choice,
245
+ line_spacing,
246
+ width_choice,
247
+ height_choice,
248
+ font_choice,
249
+ font_size,
250
+ sound_choice,
251
+ custom_audio,
252
+ background_color,
253
+ text_color,
254
+ enhance_quality,
255
+ video_speed
256
+ ],
257
 
258
  outputs=gr.Video(label="Typing Video"),
259
  title="Type Bytes🐧",
 
262
  )
263
 
264
  if __name__ == "__main__":
265
+ iface.launch(share=True)