import os import re import base64 INPUT_CSS = 'SD/css/imgs.css' OUTPUT_DIR = 'Imgs/' os.makedirs(OUTPUT_DIR, exist_ok=True) with open(INPUT_CSS, 'r') as f: css = f.read() print('First 500 chars of CSS:') print(css[:500]) # Improved regex to match the actual CSS structure in imgs.css # Handles: img.className { ... content: url(data:image/TYPE;base64,BASE64DATA) ... } # Regex for CSS image blocks with single braces and correct escaping pattern = re.compile( r'img\.([a-zA-Z0-9_]+)\s*\{[^}]*?content\s*:\s*url\(data:image/(png|bmp|jpeg|jpg);base64,([A-Za-z0-9+/=\s]+)\)[^}]*?\}', re.DOTALL) matches = list(pattern.finditer(css)) print(f"Found {len(matches)} images in CSS.") if matches: print('First match:') print(matches[0].group(0)) for match in matches: class_name, img_type, b64data = match.groups() ext = 'jpg' if img_type == 'jpeg' else img_type out_path = os.path.join(OUTPUT_DIR, f"{class_name}.{ext}") try: clean_b64 = ''.join(b64data.split()) with open(out_path, 'wb') as imgf: imgf.write(base64.b64decode(clean_b64)) print(f"Extracted {out_path} (class: {class_name}, type: {img_type})") except Exception as e: print(f"Failed to extract {out_path}: {e}") print("Done.")