From 8fe915ed11930b39468fa057b318cc928041da45 Mon Sep 17 00:00:00 2001 From: laggykiller Date: Tue, 5 Sep 2023 23:10:03 +0800 Subject: [PATCH] v1.0.4 - Support using APNGAsmBinder with statement (context manager) - Update examples - FIx permissions of example scripts --- CMakeLists.txt | 2 +- README.md | 12 +++++++----- example/example_binder.py | 13 +++++++------ example/example_direct.py | 0 src-python/apngasm_python/__init__.py | 2 +- src-python/apngasm_python/apngasm.py | 8 +++++--- 6 files changed, 21 insertions(+), 16 deletions(-) mode change 100644 => 100755 example/example_binder.py mode change 100644 => 100755 example/example_direct.py diff --git a/CMakeLists.txt b/CMakeLists.txt index b4cc9a3..288b857 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,7 +109,7 @@ else() endif() endif() -project(apngasm-python VERSION 1.0.3) +project(apngasm-python VERSION 1.0.4) set(PY_VERSION_SUFFIX "") set(PY_FULL_VERSION ${PROJECT_VERSION}${PY_VERSION_SUFFIX}) diff --git a/README.md b/README.md index 59f77e7..c4b1a6e 100644 --- a/README.md +++ b/README.md @@ -37,19 +37,21 @@ for file_name in sorted(os.listdir('frames')): # This sets the APNG animation to loop for 3 times before stopping apngasm.set_loops(3) apng.assemble('result-from-file.apng') +apngasm.reset() # From Pillow -apngasm.reset() for file_name in sorted(os.listdir('frames')): image = Image.open(os.path.join('frames', file_name)).convert('RGBA') frame = apngasm.add_frame_from_pillow(image, delay_num=50, delay_den=1000) apngasm.assemble('result-from-pillow.apng') +apngasm.reset() # Disassemble and get pillow image of one frame -apngasm.reset() -frames = apngasm.disassemble_as_pillow('input/ball.apng') -frame = frames[0] -frame.save('output/ball0.png') +# You can use with statement to avoid calling reset() +with APNGAsmBinder() as apng: + frames = apng.disassemble_as_pillow('input/ball.apng') + frame = frames[0] + frame.save('output/ball0.png') # Disassemble all APNG into PNGs apngasm.save_pngs('output') diff --git a/example/example_binder.py b/example/example_binder.py old mode 100644 new mode 100755 index 3a9c214..9834732 --- a/example/example_binder.py +++ b/example/example_binder.py @@ -62,16 +62,17 @@ success = apngasm.assemble('output/elephant-spinning-pillow.apng') print(f'{success = }') +apngasm.reset() # Assemble palette and grey PNGs -apngasm.reset() -apngasm.add_frame_from_file('input/palette.png') -apngasm.add_frame_from_file('input/grey.png') -success = apngasm.assemble('output/birds.apng') -print(f'{success = }') +# You can use with statement to avoid calling reset() +with APNGAsmBinder() as apng: + apng.add_frame_from_file('input/palette.png') + apng.add_frame_from_file('input/grey.png') + success = apng.assemble('output/birds.apng') + print(f'{success = }') # Assemble palette and grey PNGs, but with Pillow -apngasm.reset() image0 = Image.open('input/grey.png') frame0 = apngasm.add_frame_from_pillow(image0) image1 = Image.open('input/palette.png') diff --git a/example/example_direct.py b/example/example_direct.py old mode 100644 new mode 100755 diff --git a/src-python/apngasm_python/__init__.py b/src-python/apngasm_python/__init__.py index 17b6f68..42009ea 100644 --- a/src-python/apngasm_python/__init__.py +++ b/src-python/apngasm_python/__init__.py @@ -1,4 +1,4 @@ '''apngasm-python''' -__version__ = '1.0.3' +__version__ = '1.0.4' from .apngasm import APNGAsmBinder \ No newline at end of file diff --git a/src-python/apngasm_python/apngasm.py b/src-python/apngasm_python/apngasm.py index 6eb7512..8e9b833 100644 --- a/src-python/apngasm_python/apngasm.py +++ b/src-python/apngasm_python/apngasm.py @@ -15,10 +15,12 @@ class APNGAsmBinder: def __init__(self): self.apngasm = APNGAsm() - - def __del__(self): + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): self.apngasm.reset() - del self.apngasm def frame_pixels_as_pillow(self, frame, new_value=None): '''