Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active February 16, 2025 20:12
Show Gist options
  • Save obfusk/941fc09179e55095d8941b7eb92a54d4 to your computer and use it in GitHub Desktop.
Save obfusk/941fc09179e55095d8941b7eb92a54d4 to your computer and use it in GitHub Desktop.
test apkrepotool icon extraction
import dataclasses
import sys
import zipfile
from pathlib import Path
import PIL
import PIL.Image
import apkrepotool
import apkrepotool.xml_icons as xml_icons
import repro_apk.binres as binres
OUTDIR = "tmp2"
for apkfile in sys.argv[1:]:
print(f"apk={apkfile!r}")
try:
man = apkrepotool.get_manifest(Path(apkfile))
for field in dataclasses.fields(man):
print(f" {field.name}={getattr(man, field.name)!r}")
if man.png_icons:
with zipfile.ZipFile(apkfile) as zf:
data = zf.read(man.png_icons[0])
with open(f"{OUTDIR}/{man.appid}_{man.version_code}-png.png", "wb") as fh:
fh.write(data)
if man.webp_icons:
with (zipfile.ZipFile(apkfile) as zf, zf.open(man.webp_icons[0]) as zfh,
PIL.Image.open(zfh, formats=["WEBP"]) as im):
im.save(f"{OUTDIR}/{man.appid}_{man.version_code}-webp.png", "PNG")
if man.xml_icons:
with zipfile.ZipFile(apkfile) as zf:
data = xml_icons.extract_icon(zf, man.xml_icons[0])
with open(f"{OUTDIR}/{man.appid}_{man.version_code}-xml.png", "wb") as fh:
fh.write(data)
except (apkrepotool.Error, xml_icons.Error, binres.Error, PIL.UnidentifiedImageError) as e:
print(f"Error: {e}")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment