Skip to content

Commit 362fd6d

Browse files
authored
Merge pull request #745 from Tanya-18/main
Automated Pixel-Sorting
2 parents b87caaa + 1ce9f30 commit 362fd6d

File tree

10 files changed

+160
-0
lines changed

10 files changed

+160
-0
lines changed

auto_pixelsort/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Automated Pixel-Sorting :
2+
3+
- This tool performs pixelsorting using the [Pixelsort](https://github.com/satyarth/pixelsort) library.
4+
- Running the code randomly generates 'N' number of images.
5+
- Everything, from count and choice of paramters to the values they hold, is randomly generated.
6+
7+
8+
## Setup and paramter instructions :
9+
10+
- Install the Pixelsort library using : ```pip install pixelsort```
11+
- The few paramters that are to be passed through the terminal are :
12+
* Image Name with extension : ```-i image.ext``` [png or jpg]
13+
* Number of outputs to be generated : ```-n 10``` [any number]
14+
- To try it out, place your image in the "images" folder.
15+
- Run the script from the terminal : ```python autosort.py -i image.jpg -n 5```
16+
- The parameters and their values for each instance on each run are printed out in your terminal.
17+
- The results can be found in the "generated" folder which gets empty at each fresh run.
18+
19+
- Examples :
20+
- ```python autosort.py -i image.jpg -n 6```
21+
- ```python autosort.py -i image.png -n 10```
22+
23+
## Output :
24+
25+
![Sample Results](https://i.imgur.com/Bhxz1pX.png)
26+
27+
## Author(s) :
28+
- [Tanya Sabarwal](https://github.com/Tanya-18)

auto_pixelsort/autosort.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import os
2+
import sys
3+
import getopt
4+
import random as r
5+
from PIL import Image
6+
from pixelsort import pixelsort
7+
8+
9+
def randomize_params():
10+
angle = r.randint(90, 359)
11+
12+
# --- DEFINE ALL PARAMETERS ---
13+
params = {
14+
1: 'interval_function',
15+
2: 'randomness',
16+
3: 'lower_threshold',
17+
4: 'upper_threshold',
18+
5: 'sorting_function',
19+
}
20+
21+
# --- RANDOMIZE COUNT AND CHOICE OF PARAMS ---
22+
param_count = r.randint(1, 5)
23+
selected_params = []
24+
for _ in range(param_count):
25+
param_choice = r.randint(1, 5)
26+
if params[param_choice] not in selected_params:
27+
selected_params.append(params[param_choice])
28+
29+
# --- SET DEFAULTS FOR PARAMS ---
30+
args = {}
31+
args['angle'] = angle
32+
args['interval_function'] = 'threshold'
33+
args['lower_threshold'] = 0.5
34+
args['upper_threshold'] = 0.8
35+
args['randomness'] = 0.5
36+
args['sorting_function'] = 'lightness'
37+
38+
# --- UPDATE WITH RANDOMIZED VALUES ---
39+
for param in selected_params:
40+
if param == 'interval_function':
41+
interval_fns = ['random', 'threshold', 'waves']
42+
args['interval_function'] = r.choice(interval_fns)
43+
elif param == 'randomness':
44+
args['randomness'] = r.uniform(0.5, 1)
45+
elif param == 'sorting_function':
46+
sorting_fns = ['lightness', 'hue', 'saturation', 'intensity', 'minimum']
47+
args['sorting_function'] = r.choice(sorting_fns)
48+
elif param == 'lower_threshold':
49+
args['lower_threshold'] = r.uniform(0.5, 1)
50+
elif param == 'upper_threshold':
51+
up_thresh = r.uniform(0.6, 1)
52+
if up_thresh <= args['lower_threshold']:
53+
up_thresh += r.uniform(0.1, 1 - args['lower_threshold'])
54+
args['upper_threshold'] = up_thresh
55+
elif args['upper_threshold'] - args['lower_threshold'] < 0.25:
56+
args['lower_threshold'] -= 0.25
57+
return args
58+
59+
60+
def perform_sorting(args, img):
61+
# --- PERFORM PIXELSORT WITH RANDOMIZED PARAMS ---
62+
new_img = pixelsort(
63+
image=img,
64+
angle=args['angle'],
65+
interval_function=args['interval_function'],
66+
lower_threshold=args['lower_threshold'],
67+
upper_threshold=args['upper_threshold'],
68+
randomness=args['randomness'],
69+
sorting_function=args['sorting_function']
70+
)
71+
return new_img
72+
73+
74+
def Main():
75+
# --- DEFINE ARGS AND SET DEFAULTS ---
76+
count = 0
77+
in_path = 'images/'
78+
out_path = 'generated/'
79+
argument_list = sys.argv[1:]
80+
options = 'hi:n:'
81+
82+
# --- DEFINE TERMINAL ARG OPERATIONS ---
83+
try:
84+
args, _ = getopt.getopt(argument_list, options)
85+
for current_argument, current_value in args:
86+
if current_argument in ('-h'):
87+
print('-' * 30)
88+
print('-h : args description')
89+
print('-i : pass location of input img-file')
90+
print('-n : number of outputs required')
91+
print('-' * 30)
92+
if current_argument in ('-i'):
93+
print('-' * 30)
94+
in_path += current_value
95+
print(f'[+] Input-file: {in_path}')
96+
if current_argument in ('-n'):
97+
count = int(current_value)
98+
print(f'[+] Output-Count: {current_value}')
99+
print('-' * 30)
100+
101+
except getopt.error as error:
102+
print(str(error))
103+
104+
# --- DELETE PREVIOUS RESULTS ---
105+
for file in os.listdir(out_path):
106+
os.remove(os.path.join(out_path, file))
107+
108+
# --- GENERATE 'N=COUNT' INSTANCES ---
109+
for index in range(count):
110+
111+
# --- CALL PARAMETER FUNCTION ---
112+
args = randomize_params()
113+
114+
# --- PRINT RANDOMIZED CHOICES ---
115+
for arg in args.items():
116+
print(arg[0], ':', arg[1])
117+
118+
# --- DEFINE LOCATIONS FOR LOAD AND SAVE ---
119+
in_file = in_path
120+
out_file = out_path + f'result-0{index + 1}.png'
121+
img = Image.open(in_file)
122+
123+
# --- CALL SORT FUNCTION ---
124+
new_img = perform_sorting(args, img)
125+
# --- SAVE NEW FILE ---
126+
new_img.save(out_file)
127+
print('-' * 30)
128+
129+
130+
if __name__ == "__main__":
131+
Main()
806 KB
Loading
791 KB
Loading
893 KB
Loading
886 KB
Loading
762 KB
Loading

auto_pixelsort/images/markdown.png

1.24 MB
Loading

auto_pixelsort/images/planet.jpg

99 KB
Loading

auto_pixelsort/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pixelsort==1.0.1

0 commit comments

Comments
 (0)