outputs as jpg not png

This commit is contained in:
RealEvoranz
2026-08-25 22:09:50 -04:00
parent cafc5097bd
commit 255c8b05fc
2 changed files with 28 additions and 21 deletions
+28 -21
View File
@@ -14,7 +14,7 @@ from PIL import Image
API_URL = "https://commons.wikimedia.org/w/api.php" API_URL = "https://commons.wikimedia.org/w/api.php"
OUTPUT_FILE = Path("random_image.png") OUTPUT_FILE = Path("visual_search.jpg")
METADATA_FILE = Path("random_image.json") METADATA_FILE = Path("random_image.json")
MAX_ATTEMPTS = 10 MAX_ATTEMPTS = 10
@@ -154,27 +154,34 @@ def download_image(url):
# ============================================================ # ============================================================
# PNG CONVERSION # JPEG CONVERSION
# ============================================================ # ============================================================
def convert_to_png(image_data): def convert_to_jpeg(image_data):
"""Convert downloaded image bytes to PNG.""" """Convert downloaded image bytes to JPEG."""
try: try:
with Image.open( with Image.open(
io.BytesIO(image_data) io.BytesIO(image_data)
) as image: ) as image:
# Handle JPEG, WebP, PNG, etc. # JPEG does not support alpha (transparency).
# # If the image has transparency (RGBA or LA), paste it over a white background.
# RGBA preserves transparency where possible. if image.mode in ("RGBA", "LA") or (image.mode == "P" and "transparency" in image.info):
png_image = image.convert("RGBA") background = Image.new("RGB", image.size, (255, 255, 255))
if image.mode == "P":
image = image.convert("RGBA")
background.paste(image, mask=image.split()[-1])
jpeg_image = background
else:
jpeg_image = image.convert("RGB")
output = io.BytesIO() output = io.BytesIO()
png_image.save( jpeg_image.save(
output, output,
format="PNG", format="JPEG",
quality=90,
optimize=True, optimize=True,
) )
@@ -182,7 +189,7 @@ def convert_to_png(image_data):
except Exception as e: except Exception as e:
print( print(
f"PNG conversion failed: {e}" f"JPEG conversion failed: {e}"
) )
return None return None
@@ -393,25 +400,25 @@ def get_random_image():
continue continue
# ---------------------------------------------------- # ----------------------------------------------------
# CONVERT TO PNG # CONVERT TO JPEG
# ---------------------------------------------------- # ----------------------------------------------------
print("Converting to PNG...") print("Converting to JPEG...")
png_data = convert_to_png( jpeg_data = convert_to_jpeg(
image_data image_data
) )
if png_data is None: if jpeg_data is None:
continue continue
# ---------------------------------------------------- # ----------------------------------------------------
# SAVE PNG # SAVE JPEG
# ---------------------------------------------------- # ----------------------------------------------------
try: try:
OUTPUT_FILE.write_bytes( OUTPUT_FILE.write_bytes(
png_data jpeg_data
) )
except OSError as e: except OSError as e:
@@ -427,7 +434,7 @@ def get_random_image():
metadata = { metadata = {
"title": title, "title": title,
"source": "Wikimedia Commons", "source": "Wikimedia Commons",
"output_format": "PNG", "output_format": "JPEG",
"width": width, "width": width,
"height": height, "height": height,
@@ -436,8 +443,8 @@ def get_random_image():
"original_size": size, "original_size": size,
"png_size": len( "jpeg_size": len(
png_data jpeg_data
), ),
"original_url": ( "original_url": (
@@ -483,7 +490,7 @@ def get_random_image():
) )
print( print(
f"Size: " f"Size: "
f"{len(png_data) / 1024:.1f} KB" f"{len(jpeg_data) / 1024:.1f} KB"
) )
print( print(
f"Source: {title}" f"Source: {title}"
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB