diff --git a/README.md b/README.md index 02c47bf..7b91abc 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Latest release (2.2): pip3 install sc8pr ``` -Development version (2.2.x): +Development version (2.3.a): ``` pip3 install https://github.com/dmaccarthy/sc8pr/archive/master.zip ``` diff --git a/sc8pr/__init__.py b/sc8pr/__init__.py index d5b3ecc..db805a5 100644 --- a/sc8pr/__init__.py +++ b/sc8pr/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2015-2022 D.G. MacCarthy +# Copyright 2015-2023 D.G. MacCarthy # # This file is part of "sc8pr". # @@ -16,7 +16,7 @@ # along with "sc8pr". If not, see . -version = 2, 2, 2 +version = 2, 3, "a0" print("sc8pr {}.{}.{}: https://dmaccarthy.github.io/sc8pr".format(*version)) import sys, struct, zlib, io diff --git a/sc8pr/misc/media.py b/sc8pr/misc/media.py index 3390e19..6ed17c0 100644 --- a/sc8pr/misc/media.py +++ b/sc8pr/misc/media.py @@ -1,4 +1,4 @@ -# Copyright 2015-2021 D.G. MacCarthy +# Copyright 2015-2023 D.G. MacCarthy # # This file is part of "sc8pr". # @@ -77,13 +77,16 @@ def __init__(self, src, **kwargs): def __next__(self): "Return the next frame as an uncompressed PixelData instance" - return PixelData((next(self._iter), self._info)) + return PixelData((bytes(next(self._iter)), self._info)) - def read(self, n=None): + def read(self, n=None, compress=None): "Return a Video instance from the next n frames" vid = Video() + if compress is None: + compress = n is None or n > 20 while n or n is None: - try: vid += next(self._iter), self._info + try: + vid += (next(self._iter), self._info) if compress else next(self) except: n = 0 if n: n -= 1 return vid diff --git a/sc8pr/misc/video.py b/sc8pr/misc/video.py index c49ba6e..860d831 100644 --- a/sc8pr/misc/video.py +++ b/sc8pr/misc/video.py @@ -86,6 +86,8 @@ def __iadd__(self, img): "Append a frame to the video" if isinstance(img, Video): return self.extend(img._costumes) if not isinstance(img, PixelData): + if type(img[0]) is not bytes: + img = bytes(img[0]), img[1] img = PixelData(img, True) self._costumes.append(img) if not hasattr(self, "_size"): self._size = img.size diff --git a/sc8pr/plot/json.py b/sc8pr/plot/json.py index 2db217b..5c81d35 100644 --- a/sc8pr/plot/json.py +++ b/sc8pr/plot/json.py @@ -15,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with "sc8pr". If not, see . -"EXPERIMENTAL!!! Create a Canvas from a description stored in a JSON file" +"EXPERIMENTAL!!! Create a Canvas from a description stored in a dictionary / JSON file" import json from sc8pr import Sketch, Canvas @@ -23,14 +23,18 @@ from sc8pr.plot import plot, gridlines from sc8pr.text import Font from sc8pr.util import ondrag +import sc8pr.plot.mpl as mpl -data = None def canvas(data): - cv = Canvas(data["size"]) + "Create a Canvas instance from data in a dictionary or JSON file" + if type(data) is str: + with open(data) as f: data = json.load(f) + bg = data.get("bg") dr = data.get("drag") cs = data.get("coords") - bg = data.get("bg") + + cv = Canvas(data["size"]) if bg: cv.config(bg=bg) if cs: cv.attachCS(cs, data.get("margin", 1)) @@ -42,13 +46,14 @@ def canvas(data): if len(g) > 2: a.update(g[2]) gridlines(cv, *g[:2], **a) if data.get("flatten_grid", True): cv.flatten() - data = data.get("items", []) - for item, key, args, cfg in data: + + for item, key, args, cfg in data.get("items", []): if item == "circ": item = Circle(args) if type(args) in (int, float) else Ellipse(args) elif item == "line": item = Line(*args) elif item == "poly": item = Polygon(args) elif item == "arrow": item = Arrow(**args) + elif item == "mpl": item = mpl.text(args[0], **args[1]) else: key = None if key: cv[key] = item.config(**cfg) elif item == "plot": @@ -61,19 +66,8 @@ def canvas(data): for gr in cv: gr.bind(ondrag) return cv -def render(fn, mode=0): - # Mode... 0 --> Canvas, 1 --> Image, 2 --> BytesIO, 3 --> bytes - global data - with open(fn) as f: - data = json.load(f) - cv = canvas(data) - if mode: - cv = cv.snapshot() - if mode > 1: - cv = cv.png - if mode == 3: cv = cv.read() - return cv - -def save_png(json, png): - with open(png, "wb") as png: - png.write(render(json, 3)) +def save_png(json_fn, png_fn): + "Convert JSON file data to PNG image" + with open(png_fn, "wb") as f: + data = canvas(json_fn).snapshot().png + f.write(data.read()) diff --git a/setup.cfg b/setup.cfg index d88c059..52da0a8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = sc8pr -version = 2.2.2 +version = 2.3.a0 author = D.G. MacCarthy author_email = sc8pr.py@gmail.com url = https://dmaccarthy.github.io/sc8pr