-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicpixelsort.py
More file actions
274 lines (208 loc) · 9.1 KB
/
basicpixelsort.py
File metadata and controls
274 lines (208 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from functools import reduce
from PIL import Image
import multiprocessing
from itertools import product
import numpy as np
import random
import time
import sys
start = time.time()
# Sort pixels in a rectangle section of the image with given size.
# im = input image.
# size = pixel dimensions of area to be sorted.
def kernel_sort(im, size):
# Loop through pixels one rectangle at a time.
for x in range(int(im.size[0]/size[0])):
for y in range(int(im.size[1]/size[1])):
# Extract pixels in rectangle.
block = im.crop((x*size[0], y*size[1],
(x+1)*size[0], (y+1)*size[1]))
pixels = list(block.getdata())
# Sort pixels by rgb, order by more red, then more green, then more
# blue.
pixels = sorted(pixels, key=lambda rgb:
255*255*rgb[0]+rgb[1]*255+rgb[2])
# Convert pixels to image for pasting.
block = Image.new(block.mode, block.size)
block.putdata(pixels)
# Paste sorted rectangle to current block location.
im.paste(t, (x*size[0], y*size[1],
(x+1)*size[0], (y+1)*size[1]))
return im
# Horizontally sorts image pixels for each row.
def horizontal_sort(im):
# Loop through each row.
for y in range(im.size[1]):
# Extract pixels in row.
row = im.crop((0, y, im.size[0], y+1))
pixels = list(row.getdata())
# Sort pixels by rgb, order by more red, then more green, then more blue.
pixels = sorted(c, key=lambda rgb:
255*255*rgb[0]+rgb[1]*255+rgb[2])
# Convert pixels to image for pasting.
row = Image.new(row.mode, row.size)
row.putdata(pixels)
# Paste sorted row to current row.
im.paste(row, (0, y, im.size[0], y+1))
return im
# Sort image pixels for each column.
def vertical_sort(im):
# Loop through each column.
for x in range(im.size[0]):
# Extract pixels in column.
column = im.crop((x, 0, x+1, im.size[1]))
pixels = list(column.getdata())
# Sort pixels by rgb, order by more red, then more green, then more blue.
pixels = sorted(pixels, key=lambda rgb:
255*255*rgb[0]+rgb[1]*255+rgb[2])
# Convert pixels to image for pasting.
column = Image.new(column.mode, column.size)
column.putdata(pixels)
im.paste(column, (x, 0, x+1, im.size[1]))
return im
# Color in each horizontally connected non-filtered,
# color=rgb(255,255,255)), pixels as same color randomly.
def color_filtered(im, filtered):
# Extract pixels from image and the pixels of the image filter.
pixels = list(im.getdata())
filtered_pixels = list(filtered.getdata())
# Loop through each row of image.
for y in range(im.size[1]):
# Generate random rgb color.
color = (random.randrange(255),
random.randrange(255),
random.randrange(255))
for x in range(im.size[0]):
# Extract the pixel rgb value.
pixel = filtered_pixels[x+im.size[0]*y]
# Generate new color if position is on a filtered pixel else fill in
# the same color.
if pixel == (0,0,0):
color = (random.randrange(255),
random.randrange(255),
random.randrange(255))
else:
pixels[x+im.size[0]*y] = color
# Convert pixels back into image.
im = Image.new(im.mode, im.size)
im.putdata(pixels)
return im
# Sort pixels horizontally connected to each other by their rgb value.
def sort_filtered(im, filtered):
# Extract pixels from image and the pixels of the image filter.
pixels = list(im.getdata())
filtered_pixels = list(filtered.getdata())
connected = []
# Loop through each row of image.
for y in range(im.size[1]):
# Sort horizontally connected pixels by their rgb value, order by
# more red, then more green, then more blue.
if len(connected) > 0:
connected = sorted(connected, key=lambda rgb:
255*255*rgb[0]+rgb[1]*255+rgb[2])
# Replace unsorted pixels in image with sorted pixels.
for i in range(len(connected)):
pixels[x+y*im.size[0]-len(connected)-1+i] = connected[i]
# Empty list of horizontally connected pixels.
connected = []
# Loop through each pixel in row.
for x in range(im.size[0]):
# Extract the rgb pixel value of the filtered image pixel.
pixel = filtered_pixels[x+im.size[0]*y]
# Sort and place the horizontally connected pixels when the
# current pixel is filtered.
if pixel == (0,0,0):
if len(connected) > 0:
# Sort pixels by rgb value, order by more red, then more
# green, then more blue.
connected = sorted(connected, key=lambda rgb: 255*255*rgb[0] + rgb[1]*255 + rgb[2])
# Replace the unsorted pixels with sorted pixels.
for i in range(len(connected)):
pixels[x+y*im.size[0]-len(connected)-1+i] = connected[i]
# Empty list of horizontally connected pixels.
connected = []
else:
# The current pixel is unfiltered, rgb color (255,255,255),
# so append it to the list of horizontally connected pixels.
connected.append(pixels[x+im.size[0]*y])
# Convert pixels back into image.
img = Image.new(im.mode, im.size)
img.putdata(pixels)
return img
# Filter pixels into either filtered, rgb value (0,0,0), or unfiltered pixels,
# rgb value (255,255,255), depending on if they are inside or outside the
# specified light intensity range.
def filter_intensity(im, max_intensity):
# Extract pixels in image.
pixels = list(im.getdata())
# Loop through all pixels.
for i in range(len(pixels)):
# Measure intensity as the sum of all the rgb values.
intensity = pixels[i][0] + pixels[i][1] + pixels[i][2]
# Filter out bright and dark intensity pixels.
if (intensity < max_intensity or intensity >= (255*3 - max_intensity)):
# RGB value for filtered pixels.
pixels[i] = (0, 0, 0)
else:
# RGB value for unfiltered pixels.
pixels[i] = (255, 255, 255)
# Convert pixels back into image.
im = Image.new(im.mode, im.size)
im.putdata(pixels)
return im
# Perform pixelsorting on the image with the given intensity for the filter.
# im - image to sort.
# intensity - what brightness pixels will be filtered out.
# i - thread index.
# returns - the resulting pixelsorted area by this thread.
def pixelsort(im, intensity, i, returns, sort=True):
im_filter = filter_intensity(im, intensity)
if sort:
im = sort_filtered(im, im_filter)
else:
im = color_filtered(im, im_filter)
returns[i] = im
return im
# Performs multithreaded pixelsorting on given image.
# im - image to sort.
# intensity - what brightness pixels will be filtered out.
# splits - # of processes/how many sections the image should be split up in.
# sort - sort pixels, if false the filtered areas will simply be colored in
# instead.
def multiprocessed_pixelsort(im, intensity, splits, sort=True):
# Split image into parts, pixelsort each part,
# pixelsort borders, combine to one image.
w,h = im.size
img = []
# Determine area to sort for each thread.
for i in range(splits):
img.append(im.crop((0, int(h/splits)*i, w, int(h/splits)*(i+1))))
manager = multiprocessing.Manager()
returns = manager.dict()
jobs = []
for i in range(len(img)):
p = multiprocessing.Process(target=pixelsort, args=(img[i], intensity, i, returns, sort))
p.start()
jobs.append(p)
for i in jobs:
i.join()
image = Image.new("RGB", (w, h))
for i in range(splits):
image.paste(returns[i], (0, int(h/splits)*i, w, int(h/splits)*(i+1)))
return image
# Prints command usage.
def print_help():
print("Usage: python basicpixelsort.py image.[jpg, png, etc] intensity " +
"rotation [coloring, pixelsorting] no_of_threads")
if __name__ == '__main__':
# Determine if the correct number of parameters were given.
if (len(sys.argv) != 6):
print_help()
sys.exit()
im = Image.open(sys.argv[1])
im = im.rotate(int(sys.argv[3]), expand=True)
image = multiprocessed_pixelsort(im, int(sys.argv[2]), int(sys.argv[5], False))
image = image.rotate(-int(sys.argv[3]))
# Print elapsed time.
print(time.time()-start)
image.show()