|
1 | 1 | # Create posts from downloaded posts |
2 | 2 | import os |
3 | | -import psutil |
4 | 3 | import pytesseract |
5 | 4 | from openai import OpenAI |
6 | 5 | from config import DEVELOPER_PROMPT |
7 | 6 | from PIL import Image, ImageDraw, ImageFont |
8 | 7 |
|
| 8 | + |
| 9 | +def is_mostly_black(image_path, threshold=30, ratio=0.9): |
| 10 | + img = Image.open(image_path).convert("L") # Convert to grayscale |
| 11 | + pixels = list(img.getdata()) |
| 12 | + |
| 13 | + dark_pixels = sum(p < threshold for p in pixels) |
| 14 | + total_pixels = len(pixels) |
| 15 | + |
| 16 | + return dark_pixels / total_pixels >= ratio |
| 17 | + |
| 18 | + |
9 | 19 | def wrap_text(text, font, max_width, draw): |
10 | 20 | """ |
11 | 21 | Wraps text to fit within the max_width. |
@@ -112,40 +122,12 @@ def create_posts(downloaded_posts): |
112 | 122 | # Draw multiline text. |
113 | 123 | draw.multiline_text((x, y), description, font=font, fill="white", spacing=4) |
114 | 124 |
|
115 | | - # Show the image for validation |
116 | | - img.show() |
117 | | - |
118 | | - # Validation prompt |
119 | | - print(f"\n--- Post Validation for {post['shortcode']} ---") |
120 | | - print("Original text:", post_text) |
121 | | - |
122 | | - while True: |
123 | | - user_choice = input("\nDo you want to save this post? (y/n/q to quit): ").lower().strip() |
124 | | - |
125 | | - if user_choice in ['y', 'yes']: |
126 | | - # Save the post |
127 | | - img.save("new_posts/" + post['shortcode'] + '.png') |
128 | | - |
129 | | - # Save caption |
130 | | - with open("new_posts/" + post['shortcode'] + '.txt', 'w') as f: |
131 | | - f.write(translated_caption_text) |
132 | | - |
133 | | - print(f"✅ Post saved: {post['shortcode']}.png") |
134 | | - break |
135 | | - elif user_choice in ['n', 'no']: |
136 | | - # Skip post |
137 | | - print(f"❌ Post skipped: {post['shortcode']}") |
138 | | - break |
139 | | - elif user_choice in ['q', 'quit']: |
140 | | - print("Exiting post creation...") |
141 | | - return |
142 | | - else: |
143 | | - print("Please enter 'y' for yes, 'n' for no, or 'q' to quit.") |
144 | | - |
145 | | - # Close images by PID |
146 | | - for proc in psutil.process_iter(): |
147 | | - if proc.name() == r"display-im6.q16": |
148 | | - proc.kill() |
149 | | - break |
| 125 | + if is_mostly_black(image_path): |
| 126 | + # Save the post |
| 127 | + img.save("new_posts/" + post['shortcode'] + '.png') |
| 128 | + # Save caption |
| 129 | + with open("new_posts/" + post['shortcode'] + '.txt', 'w') as f: |
| 130 | + f.write(translated_caption_text) |
150 | 131 |
|
| 132 | + print(f"Created post for {post['shortcode']}") |
151 | 133 | print("-" * 50) |
0 commit comments