Created
November 14, 2018 22:32
-
-
Save badjano/db3ed6bbb9b85ecd5c8d69c1d2eab1b5 to your computer and use it in GitHub Desktop.
Pixel Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from PIL import ImageFilter, Image | |
import os | |
def randomize(data, width, height, chunk_count): | |
new_data = [] | |
for i in range(height): | |
chunk = int(width / chunk_count) | |
for j in range(chunk_count): | |
start = i * width + j * chunk | |
end = start + chunk | |
subset = data[start:end] | |
random.shuffle(subset) | |
new_data += subset | |
mod = width - chunk_count * chunk | |
if mod > 0: | |
start = i * width + width - mod | |
end = start + mod | |
subset = data[start:end] | |
random.shuffle(subset) | |
new_data += subset | |
return new_data | |
def rotate(data, width, height): | |
new_data = [] | |
for j in range(height): | |
for i in range(width): | |
index = (i * width + j) % (width * height) | |
new_data += [data[index]] | |
return new_data | |
def random_array(count): | |
r = [] | |
for i in range(count): | |
r += [random.random()] | |
return r | |
def sort(data, width, height, chunk_count, weights, random_direction=True): | |
new_data = [] | |
def process(arr): | |
return sum([weights[a] * arr[a] for a in range(len(arr))]) | |
for i in range(height): | |
chunk = int(width / chunk_count) | |
for j in range(chunk_count): | |
if random_direction: | |
direction = random.randint(0, 1) * 2 - 1 | |
else: | |
direction = 1 | |
start = i * width + j * chunk | |
end = start + chunk | |
subset = list(sorted(data[start:end], key=lambda k: direction * process(k))) | |
new_data += subset | |
mod = width - chunk_count * chunk | |
if mod > 0: | |
if random_direction: | |
direction = random.randint(0, 1) * 2 - 1 | |
else: | |
direction = 1 | |
start = i * width + width - mod | |
end = start + mod | |
subset = list(sorted(data[start:end], key=lambda k: direction * process(k))) | |
new_data += subset | |
return new_data | |
def sort_random(data, width, height, chunk_count): | |
new_data = [] | |
for i in range(height): | |
arr = random_array(chunk_count) | |
total = sum(arr) | |
arr = [int(a * width / total) for a in arr] | |
arr[-1] += width - sum(arr) | |
for j in range(chunk_count): | |
direction = random.randint(0, 1) * 2 - 1 | |
start = i * width + sum(arr[0:j]) | |
end = start + arr[j] | |
subset = list(sorted(data[start:end], key=lambda k: direction * sum(k))) | |
new_data += subset | |
return new_data | |
def sort_random_linear(data, width, height, chunk_count): | |
new_data = [] | |
while len(new_data) < len(data): | |
direction = random.randint(0, 1) * 2 - 1 | |
start = len(new_data) | |
end = min(len(data), start + random.randint(1, chunk_count)) | |
subset = list(sorted(data[start:end], key=lambda k: direction * sum(k))) | |
new_data += subset | |
return new_data | |
def shift_data(data, width, height, length): | |
def shift(seq, n): | |
return seq[n:] + seq[:n] | |
new_data = [] | |
while len(new_data) < len(data): | |
start = len(new_data) | |
end = min(len(data), start + random.randint(1, length)) | |
subset = shift(data[start:end], random.randint(0, length)) | |
new_data += subset | |
return new_data | |
def render(name, extension): | |
for i in range(300): | |
img = Image.open("img/%s.%s" % (name, extension)) | |
width, height = img.size | |
data = list(img.getdata()) | |
# data = shift_data(data, width, height, 200) | |
# data = sort_random_linear(data, width, height, i+1) | |
# data = sort_random(data, width, height, 20) | |
data = sort(data, width, height, 3, [i*0.05, 1, 1 - i*0.075], False) | |
# data = randomize(data, width, height, 40) | |
# data = sort(data, width, height, 60) | |
# data = rotate(data, width, height) | |
# data = randomize(data, width, height, 20) | |
# data = rotate(data, width, height) | |
img.putdata(data) | |
directory = "render/%s/" % name | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
filename = "render/%s/%s_%d.png" % (name, name, i) | |
img.save(filename) | |
print(filename) | |
def random_pixels(): | |
imgs = ["art_%02d" % (i + 1) for i in range(1,2)] | |
for i in imgs: | |
render(i, "jpg") | |
random_pixels() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment