Last active
May 29, 2020 14:48
-
-
Save zmic/df416198018bcb6f89c287136adadef2 to your computer and use it in GitHub Desktop.
Graphic 3
This file contains hidden or 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 os | |
import numpy as np | |
from PIL import Image | |
#------------------------------------------------------------------- | |
# | |
# helper functions | |
# | |
def create_complex_plane(X, Y, x0, x1, y0, y1, endpoint=False): | |
I = np.indices((Y,X)).astype(np.float64) | |
fy = (y1 - y0) / (Y - 1) if endpoint else (y1 - y0) / Y | |
fx = (x1 - x0) / (X - 1) if endpoint else (x1 - x0) / X | |
return (x0 + fx*I[1]) + 1j*(y0 + fy*I[0]) | |
def colorize(R, bins, rgb0, rgb1 = None): | |
bin_widths = bins[1:] - bins[:-1] | |
D = np.digitize(R, bins) - 1 | |
C0 = np.take(rgb0, D, axis=0) | |
if rgb1 is None: | |
return C0 | |
# create gradients between rgb0 and rgb1 | |
C1 = np.take(rgb1, D, axis=0) | |
F = (R - np.take(bins, D))/np.take(bin_widths, D) | |
F = np.repeat(F, 3) | |
F.shape = C0.shape | |
C = (1-F)*C0 + F*C1 | |
return C | |
def normalize(x, floor = -1e+300, ceil = +1e+300, nan_replace = 0.0): | |
x = np.where(np.isnan(x), nan_replace, x) | |
x = np.maximum(x, floor) | |
x = np.minimum(x, ceil) | |
xmin, xmax = np.min(x), np.max(x) | |
print(xmin, xmax) | |
return (x - xmin) / (xmax - xmin) | |
def save_rgb(R, path): | |
R = np.round(R).astype(np.uint8) | |
image = Image.fromarray(R, mode='RGB') | |
image.save(path, quality=85) | |
#------------------------------------------------------------------- | |
def mod1s(a, b): | |
return np.mod(a, 1 + np.floor(np.abs(b))) | |
W, H = 800, 800 | |
s = 80 | |
b0_3 = create_complex_plane(W, H, -s, s, -s, s) | |
b0_4 = np.log(b0_3) - np.sqrt(b0_3) | |
x1 = np.abs(b0_4) | |
b0_6 = np.real(b0_4) | |
b7 = np.sqrt(x1) + np.cos(b0_6) | |
b0_5 = np.imag(b0_4) | |
x3 = np.conj(b0_4) | |
b8 = mod1s(np.abs(b0_5),x3*b0_6) | |
x0 = np.cos(b7-b8)-(2*b7-2*b8-b7*b8) | |
x9 = 56.763*(np.sin(b8)-(b7-0.111)) | |
r = np.cos(x0+0.1295)-np.cos(np.pi/2-x9) | |
#------------------------------------------------------------------- | |
color_stops = np.array([-0.0001, 0.1516, 0.4102, 0.7137, 0.8215, 1.0268]) | |
rgb_from = [(239, 239, 239), (120, 120, 120), (20, 20, 20), (20, 20, 20), (31, 31, 31), (101, 101, 101)] | |
rgb_to = rgb_from[1:] | |
r = normalize(r) | |
r = colorize(r, color_stops, rgb_from, rgb_to) | |
#------------------------------------------------------------------- | |
save_rgb(r, 'out.jpg') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment