Files
Yuzu-GCA/recover_png.py

74 lines
2.6 KiB
Python

"""
Try to recover the original image from screen.png.
Strategy 1: Decode as UTF-16 LE, then encode back to bytes via UTF-16 LE.
This should give us the raw bytes without the BOM.
Strategy 2: Try to interpret as raw pixel data (e.g. RGBA raw dump).
Strategy 3: Check if the decoded text contains base64 encoded image data.
"""
import sys
import re
path = r"D:\Yuzu-GCA\screen.png"
with open(path, 'rb') as f:
raw = f.read()
# Decode as UTF-16 LE (with BOM automatically handled by Python)
text = raw.decode('utf-16-le')
# Strategy 1: Re-encode as UTF-16 LE to get bytes
reencoded = text.encode('utf-16-le')
print(f"Re-encoded size: {len(reencoded)} bytes")
# Check if it's a valid PNG after removing BOM
# Original raw starts with ff fe, so raw[2:] should equal reencoded
print(f"Original raw[2:] == reencoded: {raw[2:] == reencoded}")
# Now, what if the original binary PNG was processed wrong?
# Let's look at the first few bytes of reencoded
print(f"First 20 bytes of reencoded: {reencoded[:20].hex(' ')}")
print(f"First 20 bytes of raw (no BOM): {raw[2:22].hex(' ')}")
# The reencoded bytes should NOT be a valid PNG (since they start with RXNG etc.)
# But maybe the original PNG bytes were: each char's ord() is two PNG bytes?
# For ASCII chars like 'I', ord('I')=0x49, but 'I' in UTF-16LE is 49 00
# So each char gives us: low_byte = ord(c) & 0xFF, high_byte = (ord(c) >> 8) & 0xFF
# If the original PNG had 49 00 at some point, that would become 'I' in UTF-16LE text
# Let's try: take each char, extract its two bytes
# For chars in BMP: char -> code point -> 2 bytes
bytes_from_chars = bytearray()
for ch in text:
cp = ord(ch)
if cp <= 0xFFFF:
bytes_from_chars.append(cp & 0xFF)
bytes_from_chars.append((cp >> 8) & 0xFF)
else:
# Surrogate pair - skip for now
pass
print(f"\nBytes from chars (first 20): {bytes(bytes_from_chars[:20]).hex(' ')}")
# Check if this is a valid PNG
if bytes_from_chars[:8] == b'\x89PNG\r\n\x1a\n':
print("SUCCESS: This is a valid PNG!")
# Write the recovered PNG
with open(r"D:\Yuzu-GCA\screen_recovered.png", 'wb') as f:
f.write(bytes_from_chars)
print("Recovered PNG written to screen_recovered.png")
else:
print(f"Not a PNG. First 8 bytes: {bytes(bytes_from_chars[:8]).hex(' ')}")
# Check what it starts with
print(f"As text: {bytes(bytes_from_chars[:8])}")
# Strategy 2: Maybe the text contains base64?
# Search for base64 patterns
b64_pattern = r'[A-Za-z0-9+/=]{50,}'
b64_matches = re.findall(b64_pattern, text)
print(f"\nBase64-like strings found: {len(b64_matches)}")
for m in b64_matches[:3]:
print(f" {m[:80]}...")