From ceb812565fb014d64cb2b715d93dfbfa107d329d Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 7 Jun 2019 00:13:43 -0300 Subject: [PATCH 1/3] add unfinished impl for quicksave --- scritcher/executer.py | 34 ++++++++++++++++++++++++++++++++-- scritcher/utils.py | 1 + 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/scritcher/executer.py b/scritcher/executer.py index a0b2ba4..ab405d1 100644 --- a/scritcher/executer.py +++ b/scritcher/executer.py @@ -1,5 +1,8 @@ -import sys +import os import shlex +import tempfile +from pathlib import Path + from .utils import load_file_path from .error import InterpreterError @@ -7,13 +10,40 @@ from .error import InterpreterError class Interpreter: """Interpreter for scritcher instructions/statements.""" def __init__(self): + self.orig_path = None self.loaded_path = None def _cmd_noop(self): pass def _cmd_load(self, loadpath: str): - path = load_file_path(loadpath) + source_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.loaded_path = Path(bmp_path) + + def _cmd_quicksave(self): + suffix = self.orig_path.suffix + name = self.orig_path.name.replace(suffix, '') + parent_folder = self.orig_path.parents[0] + + # we need to search all files that match the pattern NAME_g* + index = 0 + for glitched_out in parent_folder.glob(f'{name}*'): + try: + idx = int(glitched_out.name.strip(name + '_g')[0]) + index = idx + except (IndexError, ValueError): + continue + + # create our next glitched iteration + glitched_path = parent_folder / f'{name}_g{index + 1}.raw' + print(glitched_path) + def run(self, line: str): """Run a single line.""" diff --git a/scritcher/utils.py b/scritcher/utils.py index 64554bb..fe5d775 100644 --- a/scritcher/utils.py +++ b/scritcher/utils.py @@ -1,5 +1,6 @@ import sys from pathlib import Path + from .error import InterpreterError def load_file_path(arg: str) -> Path: From 0acf870f6cff93a5942a4ef9355cfe23d96107a6 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 7 Jun 2019 00:21:27 -0300 Subject: [PATCH 2/3] finish impl for quicksave --- scritcher/executer.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scritcher/executer.py b/scritcher/executer.py index ab405d1..f9f63c8 100644 --- a/scritcher/executer.py +++ b/scritcher/executer.py @@ -1,6 +1,8 @@ import os import shlex import tempfile +import shutil + from pathlib import Path from .utils import load_file_path @@ -40,9 +42,10 @@ class Interpreter: except (IndexError, ValueError): continue - # create our next glitched iteration - glitched_path = parent_folder / f'{name}_g{index + 1}.raw' - print(glitched_path) + # create our next glitched path + out_path = shutil.copyfile( + self.loaded_path, parent_folder / f'{name}_g{index + 1}.raw') + print('saved to', out_path) def run(self, line: str): From 3b822e4b9a79ca399c653b3773ffd33ec26d7f55 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 7 Jun 2019 00:35:46 -0300 Subject: [PATCH 3/3] add GlitchImage class, converting to BMP --- scritcher/executer.py | 28 ++++++++++++++++++++-------- scritcher/image.py | 17 +++++++++++++++++ scritcher/main.py | 3 +++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 scritcher/image.py diff --git a/scritcher/executer.py b/scritcher/executer.py index f9f63c8..2b3a8a1 100644 --- a/scritcher/executer.py +++ b/scritcher/executer.py @@ -4,16 +4,18 @@ import tempfile import shutil from pathlib import Path +from PIL import Image from .utils import load_file_path from .error import InterpreterError +from .image import GlitchImage class Interpreter: """Interpreter for scritcher instructions/statements.""" def __init__(self): self.orig_path = None - self.loaded_path = None + self.img = None def _cmd_noop(self): pass @@ -26,25 +28,35 @@ class Interpreter: handle, bmp_path = tempfile.mkstemp('.bmp') os.close(handle) - self.loaded_path = Path(bmp_path) + self.img = GlitchImage(source_path, Path(bmp_path)) + self.img.load() def _cmd_quicksave(self): - suffix = self.orig_path.suffix - name = self.orig_path.name.replace(suffix, '') + 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}*'): + for glitched_out in parent_folder.glob(f'{name}_g*'): try: idx = int(glitched_out.name.strip(name + '_g')[0]) - index = idx + + # 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.loaded_path, parent_folder / f'{name}_g{index + 1}.raw') + self.img.path, parent_folder / f'{name}_g{index + 1}.raw') print('saved to', out_path) @@ -55,7 +67,7 @@ class Interpreter: args = shlex.split(line) command = args[0] - if command != 'load' and self.loaded_path is None: + if command != 'load' and self.img is None: print('warn: no file loaded.') try: diff --git a/scritcher/image.py b/scritcher/image.py new file mode 100644 index 0000000..8a16764 --- /dev/null +++ b/scritcher/image.py @@ -0,0 +1,17 @@ +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') diff --git a/scritcher/main.py b/scritcher/main.py index 33a082a..572c068 100644 --- a/scritcher/main.py +++ b/scritcher/main.py @@ -1,9 +1,12 @@ import sys +import logging from pathlib import Path from .executer import Interpreter from .error import InterpreterError +logging.basicConfig(level=logging.DEBUG) + def main(): try: scri_path = Path(sys.argv[1]).resolve()