RozzaCreat commited on
Commit
1adc3da
·
verified ·
1 Parent(s): ce24dcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -67
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import sys
3
  import time
4
- import subprocess
5
  from PIL import Image, ImageDraw, ImageFont
6
 
7
  # Create directories
@@ -161,12 +160,10 @@ def generate_passover_coloring_pages(age):
161
  main_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}.png")
162
  img.save(main_path)
163
 
164
- # Try different methods to create a PDF
165
  pdf_filename = f"passover_coloring_pages_age_{age}_{timestamp}.pdf"
166
  pdf_path = os.path.join(PDF_DIR, pdf_filename)
167
- pdf_created = False
168
 
169
- # Method 1: Try using PIL/Pillow (built-in)
170
  try:
171
  # Open the first image to get the size
172
  first_img = Image.open(page_paths[0]).convert('RGB')
@@ -184,25 +181,67 @@ def generate_passover_coloring_pages(age):
184
  save_all=True,
185
  append_images=images
186
  )
187
- pdf_created = True
188
- print(f"\nPDF created with Pillow: {pdf_path}")
189
- except Exception as e:
190
- print(f"Pillow PDF creation failed: {str(e)}")
191
-
192
- # Method 2: Try using ImageMagick if available
193
- if not pdf_created:
194
- try:
195
- convert_cmd = ["convert"]
196
- convert_cmd.extend(page_paths)
197
- convert_cmd.append(pdf_path)
198
- subprocess.run(convert_cmd, check=True)
199
- pdf_created = True
200
- print(f"\nPDF created with ImageMagick: {pdf_path}")
201
- except (subprocess.SubprocessError, FileNotFoundError):
202
- print("ImageMagick not available, trying alternative method...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
- # Create an alternative HTML file that can be printed to PDF by the user
205
- if not pdf_created:
 
 
206
  html_path = os.path.join(IMAGES_DIR, f"passover_coloring_pages_{age}_{timestamp}.html")
207
 
208
  with open(html_path, 'w') as f:
@@ -238,12 +277,12 @@ def generate_passover_coloring_pages(age):
238
  """)
239
 
240
  # Add relative paths to the saved images in the main directory
241
- for i, _ in enumerate(selected_pages):
242
  img_filename = f"passover_{age}_{i}_{timestamp}.png"
243
  f.write(f"""
244
  <div class="page">
245
  <img src="{img_filename}" alt="Coloring Page {i+1}">
246
- <div class="page-title">{selected_pages[i]}</div>
247
  </div>
248
  """)
249
 
@@ -254,54 +293,31 @@ def generate_passover_coloring_pages(age):
254
 
255
  print(f"\nPDF creation failed, but HTML created: {html_path}")
256
  print("You can open this HTML file in your browser and use 'Print' to save as PDF.")
257
-
258
- # Print summary
259
- print(f"\nIndividual coloring pages saved in: {IMAGES_DIR}")
260
-
261
- if pdf_created:
262
- return pdf_path
263
- else:
264
  return html_path
265
 
266
- def main():
267
- """Main function for CLI interface."""
268
  print("===== Jewish Passover Coloring Pages Generator =====")
269
- print("This program creates PDF coloring books for Passover")
270
 
 
 
 
 
271
  if len(sys.argv) > 1:
272
  try:
273
- age = int(sys.argv[1])
274
- if 1 <= age <= 12:
275
- generate_passover_coloring_pages(age)
276
- else:
277
- print("Please provide a valid age between 1 and 12.")
278
  except ValueError:
279
- print("Please provide a valid number for age.")
 
 
 
 
 
 
 
280
  else:
281
- while True:
282
- print("\nEnter a child's age (1-12) or 'q' to quit:")
283
- user_input = input("> ").strip().lower()
284
-
285
- if user_input == 'q':
286
- print("\nThank you for using the Passover Coloring Pages Generator!")
287
- break
288
-
289
- try:
290
- age = int(user_input)
291
- if not (1 <= age <= 12):
292
- print("Please enter a valid age between 1 and 12.")
293
- continue
294
-
295
- print(f"\nGenerating coloring pages for age {age}...")
296
- output_path = generate_passover_coloring_pages(age)
297
-
298
- if output_path:
299
- print("\nGeneration complete!")
300
-
301
- except ValueError:
302
- print("Please enter a valid number for the age.")
303
- except Exception as e:
304
- print(f"Error: {str(e)}")
305
-
306
- if __name__ == "__main__":
307
- main()
 
1
  import os
2
  import sys
3
  import time
 
4
  from PIL import Image, ImageDraw, ImageFont
5
 
6
  # Create directories
 
160
  main_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}.png")
161
  img.save(main_path)
162
 
163
+ # Try to create a PDF using Pillow
164
  pdf_filename = f"passover_coloring_pages_age_{age}_{timestamp}.pdf"
165
  pdf_path = os.path.join(PDF_DIR, pdf_filename)
 
166
 
 
167
  try:
168
  # Open the first image to get the size
169
  first_img = Image.open(page_paths[0]).convert('RGB')
 
181
  save_all=True,
182
  append_images=images
183
  )
184
+ print(f"\nPDF created successfully: {pdf_path}")
185
+
186
+ # Create HTML as backup
187
+ html_path = os.path.join(IMAGES_DIR, f"passover_coloring_pages_{age}_{timestamp}.html")
188
+
189
+ with open(html_path, 'w') as f:
190
+ f.write(f"""<!DOCTYPE html>
191
+ <html>
192
+ <head>
193
+ <title>Jewish Passover Coloring Pages - Age {age}</title>
194
+ <style>
195
+ body {{ font-family: Arial, sans-serif; max-width: 900px; margin: 0 auto; padding: 20px; }}
196
+ h1, h2 {{ color: #2c3e50; text-align: center; }}
197
+ .controls {{ background-color: #f8f9fa; padding: 15px; margin-bottom: 20px; text-align: center; border-radius: 5px; }}
198
+ .page {{ page-break-after: always; margin-bottom: 30px; }}
199
+ img {{ max-width: 100%; display: block; margin: 0 auto; border: 1px solid #ddd; }}
200
+ .page-title {{ text-align: center; margin-top: 10px; font-weight: bold; }}
201
+ .print-button {{ background-color: #3498db; color: white; border: none; padding: 10px 20px;
202
+ border-radius: 4px; cursor: pointer; font-size: 16px; }}
203
+ .print-button:hover {{ background-color: #2980b9; }}
204
+ @media print {{
205
+ .controls {{ display: none; }}
206
+ .page {{ margin-bottom: 0; }}
207
+ body {{ padding: 0; margin: 0; }}
208
+ }}
209
+ </style>
210
+ </head>
211
+ <body>
212
+ <div class="controls">
213
+ <h1>Jewish Passover (Pesach) Coloring Pages</h1>
214
+ <h2>For Ages {age} Years</h2>
215
+ <p>Printable coloring pages are available below. You can also open the PDF directly.</p>
216
+ <p>PDF path: {pdf_path}</p>
217
+ <button class="print-button" onclick="window.print()">Print All Pages</button>
218
+ </div>
219
+ """)
220
+
221
+ # Add relative paths to the saved images in the main directory
222
+ for i, desc in enumerate(selected_pages):
223
+ img_filename = f"passover_{age}_{i}_{timestamp}.png"
224
+ f.write(f"""
225
+ <div class="page">
226
+ <img src="{img_filename}" alt="Coloring Page {i+1}">
227
+ <div class="page-title">{desc}</div>
228
+ </div>
229
+ """)
230
+
231
+ f.write("""
232
+ </body>
233
+ </html>
234
+ """)
235
+
236
+ print(f"HTML backup created: {html_path}")
237
+ print(f"\nIndividual coloring pages saved in: {IMAGES_DIR}")
238
+
239
+ return pdf_path
240
 
241
+ except Exception as e:
242
+ print(f"Error creating PDF: {str(e)}")
243
+
244
+ # Create HTML as fallback
245
  html_path = os.path.join(IMAGES_DIR, f"passover_coloring_pages_{age}_{timestamp}.html")
246
 
247
  with open(html_path, 'w') as f:
 
277
  """)
278
 
279
  # Add relative paths to the saved images in the main directory
280
+ for i, desc in enumerate(selected_pages):
281
  img_filename = f"passover_{age}_{i}_{timestamp}.png"
282
  f.write(f"""
283
  <div class="page">
284
  <img src="{img_filename}" alt="Coloring Page {i+1}">
285
+ <div class="page-title">{desc}</div>
286
  </div>
287
  """)
288
 
 
293
 
294
  print(f"\nPDF creation failed, but HTML created: {html_path}")
295
  print("You can open this HTML file in your browser and use 'Print' to save as PDF.")
296
+ print(f"\nIndividual coloring pages saved in: {IMAGES_DIR}")
297
+
 
 
 
 
 
298
  return html_path
299
 
300
+ if __name__ == "__main__":
 
301
  print("===== Jewish Passover Coloring Pages Generator =====")
 
302
 
303
+ # Default age
304
+ default_age = 7
305
+
306
+ # Look for age in command-line arguments
307
  if len(sys.argv) > 1:
308
  try:
309
+ default_age = int(sys.argv[1])
310
+ if not (1 <= default_age <= 12):
311
+ print("Invalid age provided. Using default age 7.")
312
+ default_age = 7
 
313
  except ValueError:
314
+ print("Invalid age format. Using default age 7.")
315
+
316
+ print(f"Generating coloring pages for age {default_age}...")
317
+ output_path = generate_passover_coloring_pages(default_age)
318
+
319
+ if output_path:
320
+ print("\nGeneration complete!")
321
+ print(f"Files saved to: {output_path}")
322
  else:
323
+ print("\nGeneration failed.")