Created
May 5, 2025 12:26
-
-
Save rena2019/be88173f8f88688a2d186e744c7711af to your computer and use it in GitHub Desktop.
Extract PNGs from EML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from email import policy | |
from email.parser import BytesParser | |
# Pfad zur .eml-Datei | |
eml_file = "beispiel.eml" | |
output_dir = "bilder" | |
# Sicherstellen, dass der Ausgabeordner existiert | |
os.makedirs(output_dir, exist_ok=True) | |
# EML-Datei einlesen | |
with open(eml_file, 'rb') as f: | |
msg = BytesParser(policy=policy.default).parse(f) | |
# Durch alle Teile der Nachricht iterieren | |
for part in msg.walk(): | |
content_type = part.get_content_type() | |
content_disposition = part.get("Content-Disposition", "") | |
# Nur Bilder (meist image/jpeg, image/png) | |
if content_type.startswith("image/"): | |
filename = part.get_filename() | |
if not filename: | |
ext = content_type.split("/")[-1] | |
filename = f"bild_{hash(part)}.{ext}" | |
filepath = os.path.join(output_dir, filename) | |
# Bild speichern | |
with open(filepath, 'wb') as img: | |
img.write(part.get_payload(decode=True)) | |
print(f"Bild gespeichert: {filepath}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment