Skip to content

Commit

Permalink
Version 2.3.a0
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaccarthy committed Feb 8, 2023
1 parent ecd304b commit 6261bc3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
4 changes: 2 additions & 2 deletions sc8pr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2022 D.G. MacCarthy <https://dmaccarthy.github.io/sc8pr>
# Copyright 2015-2023 D.G. MacCarthy <https://dmaccarthy.github.io/sc8pr>
#
# This file is part of "sc8pr".
#
Expand All @@ -16,7 +16,7 @@
# along with "sc8pr". If not, see <http://www.gnu.org/licenses/>.


version = 2, 2, 2
version = 2, 3, "a0"
print("sc8pr {}.{}.{}: https://dmaccarthy.github.io/sc8pr".format(*version))

import sys, struct, zlib, io
Expand Down
11 changes: 7 additions & 4 deletions sc8pr/misc/media.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2021 D.G. MacCarthy <https://dmaccarthy.github.io/sc8pr>
# Copyright 2015-2023 D.G. MacCarthy <https://dmaccarthy.github.io/sc8pr>
#
# This file is part of "sc8pr".
#
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sc8pr/misc/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 16 additions & 22 deletions sc8pr/plot/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@
# You should have received a copy of the GNU General Public License
# along with "sc8pr". If not, see <http://www.gnu.org/licenses/>.

"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
from sc8pr.shape import Circle, Ellipse, Line, Arrow, Polygon
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))
Expand All @@ -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":
Expand All @@ -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())
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = sc8pr
version = 2.2.2
version = 2.3.a0
author = D.G. MacCarthy
author_email = [email protected]
url = https://dmaccarthy.github.io/sc8pr
Expand Down

0 comments on commit 6261bc3

Please sign in to comment.