<commit_before>#!/usr/bin/env python

import os
import random
import Image

def getBytes(image):
	with open(image, 'rb') as im:
		data = im.read()
		bytes = bytearray(data)
	
	return bytes


def getJpegHeaderLength(image_bytes):
	for i in (range(len(image_bytes)-1)):
		if (image_bytes[i] == 255) & (image_bytes[i+1] == 218):
			result = i 			
			break

	return result

def glitchJpegBytes(image, amount=None, seed=None, iterations=None):
	if amount == None:
		amount = random.randint(0,99)
	elif amount > 99:
		amount = 99

	if seed == None:
		seed = random.randint(1,99)
	elif seed > 99:
		seed = 99
	elif seed < 1:
		seed = 1

	if iterations == None:
		iterations = random.randint(1, 110)

	parameters = {
		'amount' : amount,
		'seed': seed,
		'iterations': iterations
	}

	print parameters

	amount = float(amount)/100
	seed = float(seed)/100

	bytes = getBytes(image)
	headerlength = getJpegHeaderLength(bytes)

	for i in (range(iterations)):
		max_index = len(bytes) - headerlength - 4
		px_min = int((max_index / iterations) * i)
		px_max = int((max_index / iterations) * (i +1))
		delta = (px_max - px_min) * 0.8
		px_i = int(px_min + (delta * seed))

		if (px_i > max_index):
			px_i = max_index

		byte_index = headerlength + px_i
		bytes[byte_index] = int(amount * 256.0)

	new_name = 'new_' + image
	new_name = image.rsplit('.')[0] + '_glitched.jpg'

	with open(new_name, 'wb') as output:
		output.write(bytes)

	return (new_name, image, parameters)

def savetoPNG(glitched_image):
	image = glitched_image[0]
	old_image= glitched_image[1]
	parameters = glitched_image[2]

	png_name = image.rsplit('.')[0] + '.png'

	while True:
		try: 
			im = Image.open(image)
			im.save(png_name)
			os.remove(image)
			print 'succes'
			break
		except IOError:
			print 'oops'
			parameters['iterations'] -= 1
			second_attempt = (
				glitchJpegBytes(old_image, 
				amount=parameters['amount'], 
				seed=parameters['seed'],
				iterations=(parameters['iterations'])))
			image = second_attempt[0]

def glitchJpeg(image, amount=None, seed=None, iterations=None):
	glitched_image = glitchJpegBytes(image, amount, seed, iterations)
	# savetoPNG(glitched_image)

if __name__ == "__main__":
	import argparse
	
	parser = argparse.ArgumentParser()
	parser.add_argument("jpg", help="you need to provide a jpg image")
	parser.add_argument('-a', metavar="amount", type=int,
		help="a number used to replace data in the image file. must be between 0 and 99")
	parser.add_argument('-s', metavar="seed", type=int,
		help="a number used to determine which data gets replaced. Must be between 0 and 99")
	parser.add_argument('-i', metavar="iterations", type=int,
		help="how many times over do you want to glitch the image. must be between 0 and 110")
	args = parser.parse_args()

	glitchJpeg(args.jpg, amount=args.a, seed=args.s, iterations=args.i)
	<commit_msg>uncommented savetopng line<commit_after>#!/usr/bin/env python

import os
import random
import Image

def getBytes(image):
	with open(image, 'rb') as im:
		data = im.read()
		bytes = bytearray(data)
	
	return bytes


def getJpegHeaderLength(image_bytes):
	for i in (range(len(image_bytes)-1)):
		if (image_bytes[i] == 255) & (image_bytes[i+1] == 218):
			result = i 			
			break

	return result

def glitchJpegBytes(image, amount=None, seed=None, iterations=None):
	if amount == None:
		amount = random.randint(0,99)
	elif amount > 99:
		amount = 99

	if seed == None:
		seed = random.randint(1,99)
	elif seed > 99:
		seed = 99
	elif seed < 1:
		seed = 1

	if iterations == None:
		iterations = random.randint(1, 110)

	parameters = {
		'amount' : amount,
		'seed': seed,
		'iterations': iterations
	}

	print parameters

	amount = float(amount)/100
	seed = float(seed)/100

	bytes = getBytes(image)
	headerlength = getJpegHeaderLength(bytes)

	for i in (range(iterations)):
		max_index = len(bytes) - headerlength - 4
		px_min = int((max_index / iterations) * i)
		px_max = int((max_index / iterations) * (i +1))
		delta = (px_max - px_min) * 0.8
		px_i = int(px_min + (delta * seed))

		if (px_i > max_index):
			px_i = max_index

		byte_index = headerlength + px_i
		bytes[byte_index] = int(amount * 256.0)

	new_name = 'new_' + image
	new_name = image.rsplit('.')[0] + '_glitched.jpg'

	with open(new_name, 'wb') as output:
		output.write(bytes)

	return (new_name, image, parameters)

def savetoPNG(glitched_image):
	image = glitched_image[0]
	old_image= glitched_image[1]
	parameters = glitched_image[2]

	png_name = image.rsplit('.')[0] + '.png'

	while True:
		try: 
			im = Image.open(image)
			im.save(png_name)
			os.remove(image)
			print 'succes'
			break
		except IOError:
			print 'oops'
			parameters['iterations'] -= 1
			second_attempt = (
				glitchJpegBytes(old_image, 
				amount=parameters['amount'], 
				seed=parameters['seed'],
				iterations=(parameters['iterations'])))
			image = second_attempt[0]

def glitchJpeg(image, amount=None, seed=None, iterations=None):
	glitched_image = glitchJpegBytes(image, amount, seed, iterations)
	savetoPNG(glitched_image)

if __name__ == "__main__":
	import argparse
	
	parser = argparse.ArgumentParser()
	parser.add_argument("jpg", help="you need to provide a jpg image")
	parser.add_argument('-a', metavar="amount", type=int,
		help="a number used to replace data in the image file. must be between 0 and 99")
	parser.add_argument('-s', metavar="seed", type=int,
		help="a number used to determine which data gets replaced. Must be between 0 and 99")
	parser.add_argument('-i', metavar="iterations", type=int,
		help="how many times over do you want to glitch the image. must be between 0 and 110")
	args = parser.parse_args()

	glitchJpeg(args.jpg, amount=args.a, seed=args.s, iterations=args.i)
	
{'fraction_non_alphanumeric': 0.13425520742166988, 'fraction_numerical': 0.02065464729564152, 'mean_word_length': 4.175724637681159, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2913645', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2045, 'n_words': 532}