Skip to content

Instantly share code, notes, and snippets.

@ice2heart
Last active December 3, 2019 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ice2heart/7206ce3771ac3dcf95f6c4d071c0e4a0 to your computer and use it in GitHub Desktop.
Save ice2heart/7206ce3771ac3dcf95f6c4d071c0e4a0 to your computer and use it in GitHub Desktop.
Noiser
#!/usr/bin/env python3
'''
sudo apt-get install libjpeg-dev zlib1g-dev
pip install pillow-simd
'''
from PIL import Image
import sys
import random
def add_noise(source, output, size):
im = Image.open(source)
im = im.resize(size)
im.save(output, "JPEG", quality=random.randint(40,80), optimize=False, progressive=False)
def main(file_name, steps):
im = Image.open(file_name)
width, height = im.size
file_name = file_name.split('.')
file_type = file_name[-1:][0]
file_name = '.'.join(file_name[:-1])
suffix = ''
for i in range(steps):
if i % 2 :
size = int(width * 1.2), int(height * 1.2)
else:
size = int(width / 1.2), int(height / 1.2)
new_suffix=f'_{i}'
add_noise(f'{file_name}{suffix}.{file_type}', f'{file_name}{new_suffix}.{file_type}', size)
suffix = new_suffix
if __name__ == '__main__':
main(sys.argv[1], int(sys.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment