format with black
This commit is contained in:
@@ -16,7 +16,6 @@ from epd.image_processor import getErr, getNear, addVal, procImg
|
||||
from epd.epd_uploader import upload_image
|
||||
|
||||
|
||||
|
||||
# get truly random post
|
||||
try:
|
||||
szuru_url = os.environ["SZURU_URL"]
|
||||
@@ -32,11 +31,13 @@ post = None
|
||||
while post is None:
|
||||
random_id = random.randint(0, highest_post.id_)
|
||||
temp_post = booru.getPost(random_id)
|
||||
if temp_post and temp_post.mime.startswith('image/') and temp_post.content:
|
||||
if temp_post and temp_post.mime.startswith("image/") and temp_post.content:
|
||||
post = temp_post
|
||||
print(f"[DEBUG] Found image post with ID: {post.id_}, MIME: {post.mime}")
|
||||
else:
|
||||
print(f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image.")
|
||||
print(
|
||||
f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image."
|
||||
)
|
||||
|
||||
# download image
|
||||
image_downloaded = False
|
||||
@@ -45,19 +46,21 @@ while not image_downloaded:
|
||||
r = requests.get(post.content, stream=True)
|
||||
print(f"[DEBUG] Downloading image from {post.content}")
|
||||
r.raise_for_status()
|
||||
with open("image.jpg", 'wb') as f:
|
||||
with open("image.jpg", "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
print("[DEBUG] Image downloaded and saved as image.jpg")
|
||||
|
||||
# Attempt to open the image to verify
|
||||
img = Image.open("image.jpg")
|
||||
img.verify() # Verify that it is an image
|
||||
img.verify() # Verify that it is an image
|
||||
img.close()
|
||||
image_downloaded = True
|
||||
print("[DEBUG] Image successfully verified.")
|
||||
except (PIL.UnidentifiedImageError, requests.exceptions.RequestException) as e:
|
||||
print(f"[ERROR] Failed to process downloaded file (Error: {e}). Retrying with a new post...")
|
||||
print(
|
||||
f"[ERROR] Failed to process downloaded file (Error: {e}). Retrying with a new post..."
|
||||
)
|
||||
# Clean up the invalid file
|
||||
if os.path.exists("image.jpg"):
|
||||
os.remove("image.jpg")
|
||||
@@ -66,13 +69,17 @@ while not image_downloaded:
|
||||
while post is None:
|
||||
random_id = random.randint(0, highest_post.id_)
|
||||
temp_post = booru.getPost(random_id)
|
||||
if temp_post and temp_post.mime.startswith('image/') and temp_post.content:
|
||||
if temp_post and temp_post.mime.startswith("image/") and temp_post.content:
|
||||
post = temp_post
|
||||
print(f"[DEBUG] Found new image post with ID: {post.id_}, MIME: {post.mime}")
|
||||
print(
|
||||
f"[DEBUG] Found new image post with ID: {post.id_}, MIME: {post.mime}"
|
||||
)
|
||||
else:
|
||||
print(f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image.")
|
||||
print(
|
||||
f"[DEBUG] Skipping post ID: {random_id} (MIME: {temp_post.mime if temp_post else 'None'}, Content: {bool(temp_post.content) if temp_post else 'None'}) - not a valid image."
|
||||
)
|
||||
|
||||
# Process and upload
|
||||
# Process and upload
|
||||
img = Image.open("image.jpg")
|
||||
|
||||
# Get configuration from environment variables
|
||||
@@ -84,7 +91,9 @@ except KeyError as e:
|
||||
raise ValueError(f"Missing required environment variable: {e}")
|
||||
|
||||
if epd_type_str not in EPD_TYPES:
|
||||
raise ValueError(f"Invalid EPD_TYPE: {epd_type_str}. Must be one of {list(EPD_TYPES.keys())}")
|
||||
raise ValueError(
|
||||
f"Invalid EPD_TYPE: {epd_type_str}. Must be one of {list(EPD_TYPES.keys())}"
|
||||
)
|
||||
epd_ind = EPD_TYPES[epd_type_str]
|
||||
|
||||
is_red = False
|
||||
@@ -93,12 +102,16 @@ if dithering_mode == "color":
|
||||
# EPDs that are explicitly monochrome in scriptD.js despite palArr:
|
||||
monochrome_only_epds = [0, 3, 6, 7, 9, 12, 16, 19, 22, 26, 27, 28, 39, 40, 43]
|
||||
if epd_ind in monochrome_only_epds:
|
||||
print(f"[WARNING] Dithering mode is set to 'color', but EPD type '{epd_type_str}' (Index {epd_ind}) is treated as monochrome by the device's protocol. Color will not appear.")
|
||||
is_red = False # Force monochrome for this EPD type
|
||||
print(
|
||||
f"[WARNING] Dithering mode is set to 'color', but EPD type '{epd_type_str}' (Index {epd_ind}) is treated as monochrome by the device's protocol. Color will not appear."
|
||||
)
|
||||
is_red = False # Force monochrome for this EPD type
|
||||
else:
|
||||
is_red = True
|
||||
elif dithering_mode != "mono":
|
||||
raise ValueError(f"Invalid DITHERING_MODE: {dithering_mode}. Must be 'mono' or 'color'.")
|
||||
raise ValueError(
|
||||
f"Invalid DITHERING_MODE: {dithering_mode}. Must be 'mono' or 'color'."
|
||||
)
|
||||
|
||||
# Get display dimensions and palette from epdArr
|
||||
dW, dH, pal_ind = epdArr[epd_ind]
|
||||
@@ -122,9 +135,11 @@ print(f"[DEBUG] EPD Type String: {epd_type_str}")
|
||||
print(f"[DEBUG] EPD Index: {epd_ind}")
|
||||
print(f"[DEBUG] Palette Index: {pal_ind}")
|
||||
# Process the final image with dithering
|
||||
processed_img = procImg(img, dW, dH, 0, 0, pal_ind, isLvl=False, isRed=is_red, palArr=palArr)
|
||||
processed_img = procImg(
|
||||
img, dW, dH, 0, 0, pal_ind, isLvl=False, isRed=is_red, palArr=palArr
|
||||
)
|
||||
|
||||
processed_img.save("processed.png") # Save for debugging
|
||||
processed_img.save("processed.png") # Save for debugging
|
||||
print("[DEBUG] Imaged processed and saved as processed.png")
|
||||
|
||||
upload_image(processed_img, ip_addr, epd_ind, epdArr, palArr, getNear)
|
||||
|
Reference in New Issue
Block a user