Skip to content

Commit

Permalink
Merge pull request #28 from TotallyNotChase/preview-release
Browse files Browse the repository at this point in the history
Update to 1.0.0
  • Loading branch information
TotallyNotChase committed Jul 26, 2020
2 parents 532f560 + 449b897 commit fcb023b
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 160 deletions.
18 changes: 12 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# Ignore package setup generated files
*.egg-info/
*.mypy_cache
*/.mypy_cache
*.__pycache__
build/
dist/
# Ignore pycache
*/__pycache__
# Ignore script generated temporary directory
*/Glitched GIF
# Ignore venv
glitchenv/
# Ignore vscode specific files
.vscode/
# Ignore script generated outputs
Collections/
build/
dist/
glitch_env/
# Ignore some file formats (usually present for the user)
*.png
*.gif
*.jpg
*.info
# Except the test.png and test.gif files
!test.png
!test.gif
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,12 @@
The version is only checked once every few days and saved into a local file. This file is checked afterwards. Eliminates the need to send request to pypi

* Add `--version` argument to the commandline script

## Version 1.0.0 - **MAJOR**
* NEW `glitch_image` and `glitch_gif` in `glitch_this.py`:-
* `seed`: Set a custom seed to be used by `random`, for generating similar images across runs
* NEW parameters for `commandline.py`:-
* `-sd, --seed`: Set a custom seed to be used by `random`, for generating similar images across runs
* Cleanup the codebase using fstrings
* Add FULL **typing support** for providing a better experience to library users
* Fix undefined variable in `glitch_gif`
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Checkout a web demo right [here](https://github.com/pahefu/web-glitch-this), cou

* Customize the **number of frames** in a GIF as well as their **duration** - all from the comfort of your terminal!
* Set how many times the GIF should **loop**!
* Set your own custom **seed** for a predictable RNG!

## Changelog
View the changelog [here](https://github.com/TotallyNotChase/glitch-this/blob/master/CHANGELOG.md)
Expand Down
98 changes: 57 additions & 41 deletions glitch_this/commandline.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
#!/usr/bin/env python3
import os, argparse
import argparse
import os
from datetime import datetime
from pathlib import Path
from time import time
from typing import Dict

from glitch_this import ImageGlitcher

def read_version():
with open(version_filepath, 'r') as file:
content = file.read()

def read_version() -> str:
with open(version_filepath, 'r') as version_file:
content = version_file.read()
return content.strip()

def write_version(version):
with open(version_filepath, 'w') as file:
file.write(version + '\n')

def is_expired(filepath):
def write_version(version: str):
with open(version_filepath, 'w') as version_file:
version_file.write(version + '\n')


def is_expired(filepath: str) -> bool:
# Check if the file has been created 2 weeks prior
from datetime import datetime
file_creation = datetime.fromtimestamp(os.stat(filepath).st_mtime)
now = datetime.now()
return (now - file_creation).days > 14

def is_latest(version):

def is_latest(version: str) -> bool:
# Check pypi for the latest version number
from urllib import request
import json
Expand All @@ -30,7 +37,8 @@ def is_latest(version):
else:
# Either version log does not exist or is outdated
try:
contents = request.urlopen('https://pypi.org/pypi/glitch-this/json').read()
contents = request.urlopen(
'https://pypi.org/pypi/glitch-this/json').read()
except:
# Connection issue
# Silenty return True, update check failed
Expand All @@ -39,22 +47,22 @@ def is_latest(version):
latest_version = data['info']['version']
write_version(latest_version)

print('Current version: {} | Latest version: {}'.format(version, latest_version))
print(f'Current version: {version} | Latest version: {latest_version}')
return version == latest_version

def get_help(glitch_min, glitch_max):

def get_help(glitch_min: float, glitch_max: float) -> Dict:
help_text = dict()
help_text['path'] = 'Relative or Absolute string path to source image'
help_text['level'] = 'Number between {} and {}, inclusive, representing amount of glitchiness'.format(glitch_min,
glitch_max)
help_text['level'] = f'Number between {glitch_min} and {glitch_max}, inclusive, representing amount of glitchiness'
help_text['color'] = 'Include if you want to add color offset'
help_text['scan'] = 'Include if you want to add scan lines effect\nDefaults to False'
help_text['seed'] = 'Set a random seed for generating similar images across runs'
help_text['gif'] = 'Include if you want output to be a GIF'
help_text['frames'] = 'Number of frames to include in output GIF, default - 23'
help_text['step'] = 'Glitch every step\'th frame of output GIF, default - 1 (every frame)'
help_text['increment'] = 'Increment glitch_amount by given value after glitching every frame of output GIF'
help_text['cycle'] = 'Include if glitch_amount should be cycled back to {} or {} if it over/underflows'.format(glitch_min,
glitch_max)
help_text['cycle'] = f'Include if glitch_amount should be cycled back to {glitch_min} or {glitch_max} if it over/underflows'
help_text['duration'] = 'How long to display each frame (in centiseconds), default - 200'
help_text['relative_duration'] = 'Multiply given value to input GIF\'s original duration and use that as duration'
help_text['loop'] = 'How many times the glitched GIF should loop, default - 0 (infinite loop)'
Expand All @@ -63,18 +71,19 @@ def get_help(glitch_min, glitch_max):
help_text['out'] = 'Explcitly supply full/relative path to output file'
return help_text


def main():
glitch_min, glitch_max = 0.1, 10.0
version = ImageGlitcher.__version__
current_version = ImageGlitcher.__version__
help_text = get_help(glitch_min, glitch_max)
# Add commandline arguments parser
argparser = argparse.ArgumentParser(description=
'glitch_this: Glitchify images and GIFs, with highly customizable options!\n\n'
argparser = argparse.ArgumentParser(description='glitch_this: Glitchify images and GIFs, with highly customizable options!\n\n'
'* Website: https://github.com/TotallyNotChase/glitch-this \n'
'* Version: ' + version + '\n'
f'* Version: {current_version}\n'
'* Changelog: https://github.com/TotallyNotChase/glitch-this/blob/master/CHANGELOG.md',
formatter_class=argparse.RawTextHelpFormatter)
argparser.add_argument('--version', action='version', version='glitch_this {}'.format(version))
argparser.add_argument('--version', action='version',
version=f'glitch_this {current_version}')
argparser.add_argument('src_img_path', metavar='Image_Path', type=str,
help=help_text['path'])
argparser.add_argument('glitch_level', metavar='Glitch_Level', type=float,
Expand All @@ -85,6 +94,12 @@ def main():
help=help_text['scan'])
argparser.add_argument('-g', '--gif', dest='gif', action='store_true',
help=help_text['gif'])
argparser.add_argument('-ig', '--inputgif', dest='input_gif', action='store_true',
help=help_text['inputgif'])
argparser.add_argument('-f', '--force', dest='force', action='store_true',
help=help_text['force'])
argparser.add_argument('-sd', '--seed', dest='seed', metavar='Seed', type=float, default=None,
help=help_text['seed'])
argparser.add_argument('-fr', '--frames', dest='frames', metavar='Frames', type=int, default=23,
help=help_text['frames'])
argparser.add_argument('-st', '--step', dest='step', metavar='Step', type=int, default=1,
Expand All @@ -99,10 +114,6 @@ def main():
help=help_text['relative_duration'])
argparser.add_argument('-l', '--loop', dest='loop', metavar='Loop_Count', type=int, default=0,
help=help_text['loop'])
argparser.add_argument('-ig', '--inputgif', dest='input_gif', action='store_true',
help=help_text['inputgif'])
argparser.add_argument('-f', '--force', dest='force', action='store_true',
help=help_text['force'])
argparser.add_argument('-o', '--outfile', dest='outfile', metavar='Outfile_path', type=str,
help=help_text['out'])
args = argparser.parse_args()
Expand All @@ -128,11 +139,12 @@ def main():
# Overwrite the previous values
out_path, out_file = os.path.split(Path(args.outfile))
if out_path != '' and not os.path.exists(out_path):
raise Exception('Given outfile path, ' + out_path + ', does not exist')
raise Exception('Given outfile path, ' +
out_path + ', does not exist')
# The extension in user provided outfile path is ignored
out_filename = out_file.rsplit('.', 1)[0]
# Now create the full path
full_path = os.path.join(out_path, '{}.{}'.format(out_filename, out_fileex))
full_path = os.path.join(out_path, f'{out_filename}.{out_fileex}')
if os.path.exists(full_path) and not args.force:
raise Exception(full_path + ' already exists\nCannot overwrite '
'existing file unless -f or --force is included\nProgram Aborted')
Expand All @@ -149,6 +161,7 @@ def main():
cycle=args.cycle,
scan_lines=args.scan_lines,
color_offset=args.color,
seed=args.seed,
gif=args.gif,
frames=args.frames,
step=args.step)
Expand All @@ -159,11 +172,13 @@ def main():
cycle=args.cycle,
scan_lines=args.scan_lines,
color_offset=args.color,
seed=args.seed,
step=args.step)
# Set args.gif to true if it isn't already in this case
args.gif = True
# Set args.duration to src_duration * relative duration, if one was given
args.duration = args.duration if not args.rel_duration else int(args.rel_duration * src_duration)
args.duration = args.duration if not args.rel_duration else int(
args.rel_duration * src_duration)
t1 = time()
# End of glitching
t2 = time()
Expand All @@ -174,21 +189,22 @@ def main():
print('Glitched Image saved in "{}"'.format(full_path))
else:
glitch_img[0].save(full_path,
format='GIF',
append_images=glitch_img[1:],
save_all=True,
duration=args.duration,
loop=args.loop,
compress_level=3)
format='GIF',
append_images=glitch_img[1:],
save_all=True,
duration=args.duration,
loop=args.loop,
compress_level=3)
t3 = time()
print('Glitched GIF saved in "{}"\nFrames = {}, Duration = {}, Loop = {}'.format(full_path, args.frames, args.duration, args.loop))
print('Time taken to glitch: ' + str(t1 - t0))
print('Time taken to save: ' + str(t3 - t2))
print('Total Time taken: ' + str(t3 - t0))
print(
f'Glitched GIF saved in "{full_path}"\nFrames = {args.frames}, Duration = {args.duration}, Loop = {args.loop}')
print(f'Time taken to glitch: {t1 - t0}')
print(f'Time taken to save: {t3 - t2}')
print(f'Total Time taken: {t3 - t0}')

# Let the user know if new version is available
if not is_latest(version):
if not is_latest(current_version):
print('A new version of "glitch-this" is available. Please consider upgrading via `pip3 install --upgrade glitch-this`')

if __name__=='__main__':
if __name__ == '__main__':
main()
Loading

0 comments on commit fcb023b

Please sign in to comment.