Convert Image To Xml Zip File < 2025 >
# Cleanup Path(xml_path).unlink() print(f"Saved: output_zip_path") image_to_xml_zip("sample.jpg", "image_package.zip")
# Build XML structure root = ET.Element("imagePackage") meta = ET.SubElement(root, "metadata") ET.SubElement(meta, "originalName").text = Path(image_path).name convert image to xml zip file
import base64 import zipfile import xml.etree.ElementTree as ET from pathlib import Path def image_to_xml_zip(image_path, output_zip_path): # Read and encode image with open(image_path, 'rb') as img_file: img_data = base64.b64encode(img_file.read()).decode('utf-8') # Cleanup Path(xml_path)
# Create ZIP containing the XML with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zf: zf.write(xml_path, arcname="image_data.xml") zipfile.ZIP_DEFLATED) as zf: zf.write(xml_path