Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List layers without loading image?
#19
I found an issue with the previous code and got more help on IRC to fix it.
The issue was that the "name" variable sometimes included a terminating zero which messed with text editors when exporting the output to a file (and isn't part of the layer's name anyway).
We also made a few minor changes for better clarity :


Code:
#!/usr/bin/env python3
import sys

PROP_ACTIVE_LAYER = 2
PROP_VISIBLE      = 8
if __name__ == "__main__":
    filename = sys.argv[1]
    # open the file in readonly binary mode
    with open(filename, 'rb') as f:
        # go to the 30th bytes
        f.seek(30, 0)
        # read properties
        while True:
            prop_type = int.from_bytes(f.read(4), "big")
            prop_size = int.from_bytes(f.read(4), "big")
            f.read(prop_size)
            if prop_type == 0: #PROP_END
                break
        # read layers
        while True:
            next_layer_offset = int.from_bytes(f.read(8), "big")
            if not next_layer_offset: #end of layers offsets
                break;
            saved_pos = f.tell()
            f.seek(next_layer_offset + 12, 0)
            name_len = int.from_bytes(f.read(4), "big")
            name0 = f.read(name_len).decode("utf-8")
            name = name0.replace('\0', '')
            print()
            print(name)
            while True:
                prop_type = int.from_bytes(f.read(4), "big")
                prop_size = int(int.from_bytes(f.read(4), "big") / 4)
                #print(prop_type, "size", prop_size)
                for i in range(prop_size):
                    lastint = int.from_bytes(f.read(4), "big")
                if prop_type == PROP_VISIBLE:
                    print("Visibility: %x" % lastint)
                    break
                elif prop_type == PROP_ACTIVE_LAYER:
                    print("Active")
            f.seek(saved_pos, 0)
Reply


Messages In This Thread
RE: List layers without loading image? - by ChameleonScales - 09-21-2021, 08:52 PM

Forum Jump: