Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Updated README.md and code
  • Loading branch information
Tanya-18 committed Oct 14, 2021
commit a47df15f3974a9325fe892932fce8c22d1b2778b
50 changes: 36 additions & 14 deletions ImageProcessingScripts/Automated Pixel Sorting/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
## Automated Pixel Sorting :
# Automated Pixel Sorting

## Aim :

- To randomly generate pixel-sorted results from image files.
- To be able to randomize it as much as possible and to do everything from the terminal.

## Purpose :

The purpose of the script is to generate custom number of pixel-sorted images from input file.

## Short description of package/script :

### Description :
- Pixel sorting images using the [Pixelsort](https://github.com/satyarth/pixelsort) library.
- Running the code randomly generates 'N' number of images.
- Everything, from count and choice of paramters to the values they hold, is randomly generated.
- The results are stored in a seperate folder which is emptied on each fresh run.

## Workflow of the Project :

- When the script is run, it expects few parameters to be passed : number of expected results ```-n``` and input file ```-i```
- Once the number of results and filename is defined, the script deletes any existing file in the "generated" folder.
- Then a loop is run 'N' times where 'N' is the count that is passed to the script from the terminal.
- In each run of the loop, the count and choice of paramters are decided and the same is printed out.
- These randomized parameters are then used to call the ```perform_sorting()``` function which does the sorting on the image.
- Each run of the function generates a new image file which is then saved to the "generated" folder with a different name.

## Setup instructions :

- Install required libraries : ```pip install -r requirements.txt```
- Place your file in the "images" folder for the script to work on it. [jpg/png/etc.]
- Run and pass parameters : ```python automated_pixel_sorting.py -i image.jpg -n 5```
- You can view the results from the "generated" folder and run the script again to get new randomized results.

## Output :

![Sample Results](./images/markdown.png)

## Author(s) :

### Try it out yourself :
- Install the Pixelsort library using : ```pip install pixelsort```
- The few paramters that are to be passed through the terminal are :
* Image Name with extension : ```-i image.ext``` [png or jpg]
* Number of outputs to be generated : ```-n 10``` [any number]
- To try it out, place your image in the "images" folder.
- Run the script from the terminal : ```python automated_pixel_sorting.py -i image.jpg -n 5```
- The parameters and their values for each instance on each run are printed out in your terminal.
- The results can be found in the "generated" folder which gets empty at each fresh run.

### Sample Results :
![Sample Results](./images/markdown.png)
- [Tanya Sabarwal](https://github.com/Tanya-18)
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,7 @@
from PIL import Image
from pixelsort import pixelsort

# --- DEFINE ARGS AND SET DEFAULTS ---
count = 0
in_path = 'images/'
out_path = 'generated/'
argumentList = sys.argv[1:]
options = 'hi:n:'

# --- DEFINE TERMINAL ARG OPERATIONS ---
try:
args, values = getopt.getopt(argumentList, options)
for currentArgument, currentValue in args:
if currentArgument in ('-h'):
print('-'*30)
print('-h : args description')
print('-i : pass location of input img-file')
print('-n : number of outputs required')
print('-'*30)
if currentArgument in ('-i'):
print('-'*30)
in_path += currentValue
print(f'[+] Input-file: {in_path}')
if currentArgument in ('-n'):
count = int(currentValue)
print(f'[+] Output-Count: {currentValue}')
print('-'*30)

except getopt.error as e:
print(str(e))

# --- DELETE PREVIOUS RESULTS ---
dir = out_path
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))

for index in range(count):

def randomize_params():
angle = r.randint(90, 359)

# --- DEFINE ALL PARAMETERS ---
Expand Down Expand Up @@ -74,30 +39,23 @@
if param == 'interval_function':
interval_fns = ['random', 'threshold', 'waves']
args['interval_function'] = r.choice(interval_fns)
if param == 'randomness':
elif param == 'randomness':
args['randomness'] = r.uniform(0.5, 1)
if param == 'sorting_function':
elif param == 'sorting_function':
sorting_fns = ['lightness', 'hue', 'saturation', 'intensity', 'minimum']
args['sorting_function'] = r.choice(sorting_fns)
if param == 'lower_threshold':
elif param == 'lower_threshold':
args['lower_threshold'] = r.uniform(0.5, 1)
if param == 'upper_threshold':
elif param == 'upper_threshold':
up_thresh = r.uniform(0.6, 1)
if up_thresh <= args['lower_threshold']:
up_thresh += r.uniform(0.1, 1 - args['lower_threshold'])
args['upper_threshold'] = up_thresh
if args['upper_threshold'] - args['lower_threshold'] < 0.25:
elif args['upper_threshold'] - args['lower_threshold'] < 0.25:
args['lower_threshold'] -= 0.25
return args

# --- PRINT THE RANDOMIZED CHOICES ---
for arg in args:
print(arg, ':', args[arg])

# --- DEFINE LOCATIONS FOR LOAD AND SAVE ---
in_file = in_path
out_file = out_path + f'result-0{index + 1}.png'
img = Image.open(in_file)

def perform_sorting(args, img):
# --- PERFORM PIXELSORT WITH RANDOMIZED PARAMS ---
new_img = pixelsort(
image = img,
Expand All @@ -108,7 +66,64 @@
randomness = args['randomness'],
sorting_function = args['sorting_function']
)
return new_img

def Main():
# --- DEFINE ARGS AND SET DEFAULTS ---
count = 0
in_path = 'images/'
out_path = 'generated/'
argument_list = sys.argv[1:]
options = 'hi:n:'

# --- DEFINE TERMINAL ARG OPERATIONS ---
try:
args, _ = getopt.getopt(argument_list, options)
for current_argument, current_value in args:
if current_argument in ('-h'):
print('-'*30)
print('-h : args description')
print('-i : pass location of input img-file')
print('-n : number of outputs required')
print('-'*30)
if current_argument in ('-i'):
print('-'*30)
in_path += current_value
print(f'[+] Input-file: {in_path}')
if current_argument in ('-n'):
count = int(current_value)
print(f'[+] Output-Count: {current_value}')
print('-'*30)

except getopt.error as error:
print(str(error))

# --- DELETE PREVIOUS RESULTS ---
for file in os.listdir(out_path):
os.remove(os.path.join(out_path, file))

# --- GENERATE 'N=COUNT' INSTANCES ---
for index in range(count):

# --- CALL PARAMETER FUNCTION ---
args = randomize_params()

# --- PRINT RANDOMIZED CHOICES ---
for arg in args.items():
print(arg[0], ':', arg[1])

# --- DEFINE LOCATIONS FOR LOAD AND SAVE ---
in_file = in_path
out_file = out_path + f'result-0{index + 1}.png'
img = Image.open(in_file)

# --- CALL SORT FUNCTION ---
new_img = perform_sorting(args, img)

# --- SAVE NEW FILE ---
new_img.save(out_file)
print('-'*30)

# --- SAVE NEW FILE ---
new_img.save(out_file)
print('-'*30)
if __name__ == "__main__":
Main()