diff --git a/python/grass/imaging/images2swf.py b/python/grass/imaging/images2swf.py index 7650ce2b2c9..d743a56f886 100644 --- a/python/grass/imaging/images2swf.py +++ b/python/grass/imaging/images2swf.py @@ -69,6 +69,7 @@ import os import zlib +from pathlib import Path try: import numpy as np @@ -921,62 +922,60 @@ def readSwf(filename, asNumpy=True): images = [] # Open file and read all - with open(filename, "rb") as fp: - bb = fp.read() - - # Check opening tag - tmp = bb[0:3].decode("ascii", "ignore") - if tmp.upper() == "FWS": - pass # ok - elif tmp.upper() == "CWS": - # Decompress movie - bb = bb[:8] + zlib.decompress(bb[8:]) + bb = Path(filename).read_bytes() + # Check opening tag + tmp = bb[0:3].decode("ascii", "ignore") + if tmp.upper() == "FWS": + pass # ok + elif tmp.upper() == "CWS": + # Decompress movie + bb = bb[:8] + zlib.decompress(bb[8:]) + else: + raise OSError("Not a valid SWF file: " + str(filename)) + + # Set filepointer at first tag (skipping framesize RECT and two uin16's + i = 8 + nbits = bitsToInt(bb[i : i + 1], 5) # skip FrameSize + nbits = 5 + nbits * 4 + Lrect = nbits / 8.0 + if Lrect % 1: + Lrect += 1 + Lrect = int(Lrect) + i += Lrect + 4 + + # Iterate over the tags + counter = 0 + while True: + counter += 1 + + # Get tag header + head = bb[i : i + 6] + if not head: + break # Done (we missed end tag) + + # Determine type and length + T, L1, L2 = getTypeAndLen(head) + if not L2: + print("Invalid tag length, could not proceed") + break + # print(T, L2) + + # Read image if we can + if T in {20, 36}: + im = _readPixels(bb, i + 6, T, L1) + if im is not None: + images.append(im) + elif T in {6, 21, 35, 90}: + print("Ignoring JPEG image: cannot read JPEG.") else: - raise OSError("Not a valid SWF file: " + str(filename)) - - # Set filepointer at first tag (skipping framesize RECT and two uin16's - i = 8 - nbits = bitsToInt(bb[i : i + 1], 5) # skip FrameSize - nbits = 5 + nbits * 4 - Lrect = nbits / 8.0 - if Lrect % 1: - Lrect += 1 - Lrect = int(Lrect) - i += Lrect + 4 - - # Iterate over the tags - counter = 0 - while True: - counter += 1 - - # Get tag header - head = bb[i : i + 6] - if not head: - break # Done (we missed end tag) - - # Determine type and length - T, L1, L2 = getTypeAndLen(head) - if not L2: - print("Invalid tag length, could not proceed") - break - # print(T, L2) - - # Read image if we can - if T in [20, 36]: - im = _readPixels(bb, i + 6, T, L1) - if im is not None: - images.append(im) - elif T in [6, 21, 35, 90]: - print("Ignoring JPEG image: cannot read JPEG.") - else: - pass # Not an image tag + pass # Not an image tag - # Detect end tag - if T == 0: - break + # Detect end tag + if T == 0: + break - # Next tag! - i += L2 + # Next tag! + i += L2 # Convert to normal PIL images if needed if not asNumpy: images2 = images