Created
April 25, 2026 23:09
-
-
Save darealshinji/88182ecb793cde2ccb098984aa8b2482 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python3 | |
| from argparse import ArgumentParser | |
| from lief import ELF | |
| def generate_thumbnail(size, infile, outfile): | |
| prog = ELF.parse(infile) | |
| if prog is None: | |
| raise ValueError("failed to parse input file: " + infile) | |
| section = prog.get_section(".icon") | |
| if section is None: | |
| raise ValueError("cannot find section `.icon' in file: " + infile) | |
| data = section.content | |
| if data[0:1] == b'<': # naive but sufficient guess | |
| from cairosvg import svg2png | |
| svg2png(bytestring=data, output_width=size, output_height=size, write_to=outfile) | |
| else: | |
| from PIL import Image | |
| from io import BytesIO | |
| with Image.open(BytesIO(data)) as img: | |
| img = img.resize((size, size)) | |
| img.save(outfile, "PNG") | |
| def main(): | |
| parser = ArgumentParser(description=__doc__) | |
| parser.add_argument("size", help="size of desired thumbnail") | |
| parser.add_argument("infile", help="input file name (ELF)") | |
| parser.add_argument("outfile", help="output file name (PNG)") | |
| args = parser.parse_args() | |
| try: | |
| generate_thumbnail(args.size, args.infile, args.outfile) | |
| except Exception as error: | |
| print("error:", error) | |
| exit(1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment