Files
Yuzu-GCA/fix_png2.py

67 lines
2.1 KiB
Python

import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
with open(r"D:\Yuzu-GCA\screen_fixed.png", 'rb') as f:
data = f.read()
print(f"Original size: {len(data)}")
# The file has been corrupted by text-mode LF->CRLF conversion.
# Every 0x0A byte in the original PNG has been changed to 0x0D 0x0A.
# We need to revert this: change 0x0D 0x0A back to 0x0A.
# But the PNG signature contains a legitimate 0x0D 0x0A (at offset 4-5),
# which should remain as-is.
# Strategy: replace 0D 0A -> 0A, EXCEPT for the known legitimate ones
# Legitimate: offset 4-5 (PNG signature "\r\n")
# Actually, let's just do the replacement everywhere and then fix the signature
fixed = bytearray()
i = 0
while i < len(data):
if i < len(data) - 1 and data[i] == 0x0D and data[i+1] == 0x0A:
# CR LF found - replace with just LF
fixed.append(0x0A)
i += 2
else:
fixed.append(data[i])
i += 1
fixed = bytes(fixed)
print(f"After CRLF->LF fix: {len(fixed)}")
# Now fix the PNG signature (offset 4-5 should be 0D 0A, but we changed it to 0A)
# The correct PNG signature: 89 50 4E 47 0D 0A 1A 0A
# We need to fix offset 4 to be 0D again
if fixed[4] == 0x0A: # Should be 0x0D
fixed = fixed[:4] + b'\x0D' + fixed[5:]
print("Fixed PNG signature byte 4 (0D)")
print(f"First 16 bytes: {' '.join(f'{b:02X}' for b in fixed[:16])}")
# Check IHDR
offset = 8
length = int.from_bytes(fixed[offset:offset+4], 'big')
chunk_type = fixed[offset+4:offset+8].decode('ascii', errors='replace')
print(f"IHDR: type={chunk_type}, length={length}")
w = int.from_bytes(fixed[offset+8:offset+12], 'big')
h = int.from_bytes(fixed[offset+12:offset+16], 'big')
bit_depth = fixed[offset+16]
color_type = fixed[offset+17]
print(f" Width={w}, Height={h}, BitDepth={bit_depth}, ColorType={color_type}")
# Save
with open(r"D:\Yuzu-GCA\screen_fixed2.png", 'wb') as f:
f.write(fixed)
print("Saved screen_fixed2.png")
# Try to open with PIL
from PIL import Image
try:
img = Image.open(r"D:\Yuzu-GCA\screen_fixed2.png")
print(f"Success! Format={img.format}, Size={img.size}, Mode={img.mode}")
except Exception as e:
print(f"PIL error: {e}")