Skip to content

Instantly share code, notes, and snippets.

@zmic
Last active May 28, 2020 14:14
Show Gist options
  • Save zmic/adb3e1ef48b544528e10dc375aec43a6 to your computer and use it in GitHub Desktop.
Save zmic/adb3e1ef48b544528e10dc375aec43a6 to your computer and use it in GitHub Desktop.
graphic1.py
import numpy as np
from PIL import Image
#-------------------------------------------------------------------
#
# a few helper functions
#
def create_XY(X, Y, x0, x1, y0, y1, endpoint=False, dtype=np.float64):
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
I[0], I[1] = x0 + fx*I[1], y0 + fy*I[0]
return I.astype(dtype)
def colorize(R, bins, rgb0, rgb1):
bin_widths = bins[1:] - bins[:-1]
D = np.digitize(R, bins[1:-1])
C0 = np.take(rgb0, D, axis=0)
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)
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)
#-------------------------------------------------------------------
#
# the calculation
#
def smod(a, b):
return np.fmod(a.astype(np.int64), 1 + np.abs(b.astype(np.int64)))
W, H = 800, 800
range = 10.24
x, y = create_XY(W, H, -range, range, -range, range)
r1 = np.tan(x)
r2 = np.tan(y)
r3 = smod(r1, r2)
r4 = smod(y, r1)
r5 = np.sin(r3 + np.sin(r4 * np.exp(r2)) + r4)
r6 = normalize(r5)
#-----------------------------------------------
color_stops = np.array([-0.001, 1.001])
rgb_from = np.array([[0, 0, 0]])
rgb_to = np.array([[190, 178, 236]])
r7 = colorize(r6, color_stops, rgb_from, rgb_to)
#-----------------------------------------------
save_rgb(r7, 'result.jpg' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment