Compare commits
No commits in common. "3b822e4b9a79ca399c653b3773ffd33ec26d7f55" and "38e0a1a961c697143e2bea18f0a93e9e9e2c0c2a" have entirely different histories.
3b822e4b9a
...
38e0a1a961
4 changed files with 4 additions and 70 deletions
|
@ -1,64 +1,19 @@
|
||||||
import os
|
import sys
|
||||||
import shlex
|
import shlex
|
||||||
import tempfile
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
from .utils import load_file_path
|
from .utils import load_file_path
|
||||||
from .error import InterpreterError
|
from .error import InterpreterError
|
||||||
from .image import GlitchImage
|
|
||||||
|
|
||||||
|
|
||||||
class Interpreter:
|
class Interpreter:
|
||||||
"""Interpreter for scritcher instructions/statements."""
|
"""Interpreter for scritcher instructions/statements."""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.orig_path = None
|
self.loaded_path = None
|
||||||
self.img = None
|
|
||||||
|
|
||||||
def _cmd_noop(self):
|
def _cmd_noop(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _cmd_load(self, loadpath: str):
|
def _cmd_load(self, loadpath: str):
|
||||||
source_path = load_file_path(loadpath)
|
path = load_file_path(loadpath)
|
||||||
self.orig_path = source_path
|
|
||||||
|
|
||||||
# create a temporary file to hold bmp data
|
|
||||||
handle, bmp_path = tempfile.mkstemp('.bmp')
|
|
||||||
os.close(handle)
|
|
||||||
|
|
||||||
self.img = GlitchImage(source_path, Path(bmp_path))
|
|
||||||
self.img.load()
|
|
||||||
|
|
||||||
def _cmd_quicksave(self):
|
|
||||||
suffix = self.img.original.suffix
|
|
||||||
name = self.img.original.name.replace(suffix, '')
|
|
||||||
parent_folder = self.orig_path.parents[0]
|
|
||||||
|
|
||||||
# we need to search all files that match the pattern NAME_g*
|
|
||||||
# then we save on the one _g suffix above it.
|
|
||||||
|
|
||||||
# e.g if the original path is "miya.png", we want to search for
|
|
||||||
# files named "miya_g1.raw", "miya_g2.raw", and then we want to write
|
|
||||||
# on "miya_g3.raw".
|
|
||||||
index = 0
|
|
||||||
for glitched_out in parent_folder.glob(f'{name}_g*'):
|
|
||||||
try:
|
|
||||||
idx = int(glitched_out.name.strip(name + '_g')[0])
|
|
||||||
|
|
||||||
# glob() doesnt seem to show a stable order. anyways, we can
|
|
||||||
# just only update our index when its the maximum found
|
|
||||||
if idx > index:
|
|
||||||
index = idx
|
|
||||||
except (IndexError, ValueError):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# create our next glitched path
|
|
||||||
out_path = shutil.copyfile(
|
|
||||||
self.img.path, parent_folder / f'{name}_g{index + 1}.raw')
|
|
||||||
print('saved to', out_path)
|
|
||||||
|
|
||||||
|
|
||||||
def run(self, line: str):
|
def run(self, line: str):
|
||||||
"""Run a single line."""
|
"""Run a single line."""
|
||||||
|
@ -67,7 +22,7 @@ class Interpreter:
|
||||||
args = shlex.split(line)
|
args = shlex.split(line)
|
||||||
command = args[0]
|
command = args[0]
|
||||||
|
|
||||||
if command != 'load' and self.img is None:
|
if command != 'load' and self.loaded_path is None:
|
||||||
print('warn: no file loaded.')
|
print('warn: no file loaded.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
import logging
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
class GlitchImage:
|
|
||||||
"""A wrapper class around PIL.Image"""
|
|
||||||
def __init__(self, orig_path, target_path):
|
|
||||||
self.original = orig_path
|
|
||||||
self.path = target_path
|
|
||||||
|
|
||||||
def load(self):
|
|
||||||
"""Load the given image, convert it to BMP, and write it on the
|
|
||||||
given target path."""
|
|
||||||
log.info('opening %r into %r', self.original, self.path)
|
|
||||||
img = Image.open(self.original)
|
|
||||||
img.save(self.path, 'BMP')
|
|
|
@ -1,12 +1,9 @@
|
||||||
import sys
|
import sys
|
||||||
import logging
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .executer import Interpreter
|
from .executer import Interpreter
|
||||||
from .error import InterpreterError
|
from .error import InterpreterError
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
scri_path = Path(sys.argv[1]).resolve()
|
scri_path = Path(sys.argv[1]).resolve()
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .error import InterpreterError
|
from .error import InterpreterError
|
||||||
|
|
||||||
def load_file_path(arg: str) -> Path:
|
def load_file_path(arg: str) -> Path:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue