Skip to content

Commit

Permalink
Update folder_diff.py
Browse files Browse the repository at this point in the history
  • Loading branch information
villares committed Oct 13, 2023
1 parent c891ee2 commit 8c609ab
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions admin_scripts/folder_diff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/home/villares/thonny-python-env/bin/python3

"""
Experiment on diffing sketches
TODO: Improve image files & code files accepted
"""

import PIL.Image
import py5
import difflib
Expand Down Expand Up @@ -59,15 +66,19 @@ def walk_images(i):

def get_img_and_code(path, resize=None):
if resize is None:
resize = (py5.height, py5.height)
resize = (py5.height - 100, py5.height - 100)
img, code = None, ''
try:
if path.is_file():
img, code = load_image_and_data(path, resize)
elif path.is_dir():
# TODO: search any valid image format
image_path = path / (path.name + '.png')
if not image_path.is_file():
image_path = path / (path.name + '.gif')
img, code = load_image_and_data(image_path, resize)
if len(code) == 0:
# TODO: search any code/text file
code_path = path / (path.name + '.py')
if not code_path.is_file():
code_path = path / (path.name + '.pyde')
Expand All @@ -78,12 +89,8 @@ def get_img_and_code(path, resize=None):

def load_image_and_data(image_file, resize=None):
img = PIL.Image.open(image_file)
cur_width, cur_height = img.size
if resize:
new_width, new_height = resize
sf = min(new_height / cur_height, new_width / cur_width)
img = img.resize((int(cur_width * sf), int(cur_height * sf)),
PIL.Image.Resampling.LANCZOS)
img.thumbnail(resize)
code = img.info.get('code', '').replace(' \n', '\n').replace('\n\n\n', '')
return py5.convert_image(img), code

Expand Down

0 comments on commit 8c609ab

Please sign in to comment.