Compare commits
No commits in common. "210e36fd9adfc6506b3c3f3220a1bc6699256b6c" and "d15eddd2bcac2b860bcb5874544924b37dc956cf" have entirely different histories.
210e36fd9a
...
d15eddd2bc
150
waves.py
150
waves.py
@ -2,79 +2,97 @@
|
|||||||
|
|
||||||
import cairo
|
import cairo
|
||||||
import math
|
import math
|
||||||
from datetime import datetime
|
import datetime
|
||||||
import calendar
|
import calendar
|
||||||
import random
|
import random
|
||||||
import colorsys
|
from utils import random_color
|
||||||
import argparse
|
|
||||||
|
|
||||||
|
|
||||||
|
# dimensions of the output image
|
||||||
|
WIDTH, HEIGHT = 1920, 1080
|
||||||
|
# how much should the phases be offset?
|
||||||
|
WAVE_OFFSET = math.pi / random.choice([1, 2, 4]) #-1.9
|
||||||
|
# amplitude of the sine wave
|
||||||
|
#AMPLITUDE = 9
|
||||||
|
# only 1 color with shades?
|
||||||
|
MONOCHROME = True
|
||||||
|
# works only if monochrome is set - uses todays date as base for the color
|
||||||
|
DATE_BASED_COLOR = True
|
||||||
|
DATE_BASED_AMPLITUDE = True
|
||||||
|
DATE_BASED_COUNT = True
|
||||||
|
# background black? White otherwise:
|
||||||
|
DARK_BG = True
|
||||||
# precision of the calculation
|
# precision of the calculation
|
||||||
PRECISION = 10000
|
PRECISION = 10000
|
||||||
|
|
||||||
|
def days_color(date):
|
||||||
def get_color_from_date(date: datetime) -> tuple[float, float, float]:
|
import colorsys
|
||||||
"""Return a color based on the progress through the year."""
|
|
||||||
# a day between 1 and 365 (inclusive)
|
# a day between 1 and 365 (inclusive)
|
||||||
today = date.timetuple().tm_yday
|
today = date.timetuple().tm_yday
|
||||||
days_in_year = 365 + calendar.isleap(date.year)
|
year = date.year
|
||||||
|
days_in_year = 365 + calendar.isleap(year)
|
||||||
# between 0 and 1, how far through the year are we?
|
# between 0 and 1, how far through the year are we?
|
||||||
progress = today / days_in_year
|
progress = today/days_in_year
|
||||||
return colorsys.hsv_to_rgb(progress, 1, 0.9)
|
return colorsys.hsv_to_rgb(progress, 1, 0.9)
|
||||||
|
|
||||||
|
def days_amp(date, waves):
|
||||||
def get_amplitude_from_date(date: datetime, waves) -> float:
|
day = date.day
|
||||||
"""Return the amplitude of waves, based on progress through the month."""
|
|
||||||
days_in_month = calendar.monthrange(date.year, date.month)[1]
|
days_in_month = calendar.monthrange(date.year, date.month)[1]
|
||||||
max_amp = 1 / waves / 2
|
max_amp = 1/waves/2
|
||||||
return date.day / days_in_month * max_amp
|
return day/days_in_month * max_amp
|
||||||
|
|
||||||
|
def days_count(date):
|
||||||
|
return date.month
|
||||||
|
|
||||||
def create_wpotd(
|
def create_wpotd(output):
|
||||||
width: int,
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
|
||||||
height: int,
|
|
||||||
frequency: int,
|
|
||||||
wave_offset: float,
|
|
||||||
date: datetime = datetime.now(),
|
|
||||||
dark: bool = True,
|
|
||||||
) -> cairo.ImageSurface:
|
|
||||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
|
|
||||||
ctx = cairo.Context(surface)
|
ctx = cairo.Context(surface)
|
||||||
|
|
||||||
ctx.scale(width, height) # Normalizing the canvas
|
ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas
|
||||||
# ctx.set_antialias(cairo.Antialias.BEST)
|
#ctx.set_antialias(cairo.Antialias.BEST)
|
||||||
|
|
||||||
lastpoints = [(x / PRECISION, 0) for x in range(PRECISION + 1)]
|
step_size = 1/PRECISION
|
||||||
|
lastpoints = [(x/PRECISION, 0) for x in range(PRECISION+1)]
|
||||||
|
|
||||||
waves = date.month
|
#date = datetime.datetime.strptime('2021-06-30', '%Y-%m-%d')
|
||||||
amplitude = get_amplitude_from_date(date, waves)
|
date = datetime.datetime.today()
|
||||||
|
frequency = random.randint(10, 40)
|
||||||
r, g, b = get_color_from_date(date)
|
if DATE_BASED_COUNT:
|
||||||
alpha_step = 1 / waves
|
waves = days_count(date)
|
||||||
|
|
||||||
# background color
|
|
||||||
if dark:
|
|
||||||
ctx.set_source_rgb(0, 0, 0)
|
|
||||||
else:
|
else:
|
||||||
ctx.set_source_rgb(255, 255, 255)
|
waves = 12
|
||||||
|
if DATE_BASED_AMPLITUDE:
|
||||||
|
amplitude = days_amp(date, waves)
|
||||||
|
else:
|
||||||
|
amplitude = 25
|
||||||
|
|
||||||
|
if MONOCHROME:
|
||||||
|
if DATE_BASED_COLOR:
|
||||||
|
r, g, b = days_color(date) #datetime.datetime.now())
|
||||||
|
else:
|
||||||
|
r, g, b = random_color()
|
||||||
|
alpha_step = 1/waves
|
||||||
|
if DARK_BG:
|
||||||
|
# make bg black
|
||||||
ctx.rectangle(0, 0, 1, 1)
|
ctx.rectangle(0, 0, 1, 1)
|
||||||
|
ctx.set_source_rgb(0, 0, 0)
|
||||||
ctx.fill()
|
ctx.fill()
|
||||||
|
|
||||||
wave_height = 1 / waves
|
wave_height = 1/waves
|
||||||
step_size = 1 / PRECISION
|
for num in range(waves+1):
|
||||||
|
if not MONOCHROME:
|
||||||
for wave_index in range(waves + 1):
|
r, g, b = random_color()
|
||||||
points = []
|
points = []
|
||||||
x = 0
|
x = 0
|
||||||
while x < 1:
|
while x < 1:
|
||||||
# step along, create points along the wave
|
y = amplitude * math.sin(frequency * x + (num * WAVE_OFFSET) )
|
||||||
y = amplitude * math.sin(frequency * x + (wave_index * wave_offset))
|
points.append((x, ( y + (0.5+num)*wave_height)))
|
||||||
points.append((x, (y + (0.5 + wave_index) * wave_height)))
|
|
||||||
x += step_size
|
x += step_size
|
||||||
# print(f'Draw {len(points)} points for curve {num}')
|
#print(f'Draw {len(points)} points for curve {num}')
|
||||||
|
if not MONOCHROME:
|
||||||
|
ctx.set_source_rgb(r, g, b)
|
||||||
else:
|
else:
|
||||||
# make more transparent toward bottom
|
ctx.set_source_rgba(r, g, b, 1 - (alpha_step * num)) # make more transparent toward bottom
|
||||||
ctx.set_source_rgba(r, g, b, 1 - (alpha_step * wave_index))
|
|
||||||
# draw waves
|
# draw waves
|
||||||
ctx.move_to(*points[0])
|
ctx.move_to(*points[0])
|
||||||
for p in points[1:]:
|
for p in points[1:]:
|
||||||
@ -86,45 +104,11 @@ def create_wpotd(
|
|||||||
ctx.line_to(*points[0])
|
ctx.line_to(*points[0])
|
||||||
ctx.fill()
|
ctx.fill()
|
||||||
lastpoints = points
|
lastpoints = points
|
||||||
return surface
|
#return surface
|
||||||
|
surface.write_to_png(output) # Output to PNG
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
create_wpotd('out/waves.png')
|
||||||
parser.add_argument("width", type=int, help="Width of the image")
|
|
||||||
parser.add_argument("height", type=int, help="Height of the image")
|
|
||||||
parser
|
|
||||||
parser.add_argument("--dark", help="Draw on dark background", action="store_true")
|
|
||||||
parser.add_argument(
|
|
||||||
"-o",
|
|
||||||
"--offset",
|
|
||||||
type=float,
|
|
||||||
help="How much the waves should be offset to each other",
|
|
||||||
)
|
|
||||||
parser.add_argument("-f", "--frequency", type=int, help="Frequency of the waves")
|
|
||||||
parser.add_argument(
|
|
||||||
"--stdout", help="Write output image to stdout", action="store_true"
|
|
||||||
)
|
|
||||||
args = parser.parse_args()
|
|
||||||
print(args)
|
|
||||||
if not args.offset:
|
|
||||||
args.offset = math.pi / random.choice([1, 2, 4])
|
|
||||||
if not args.frequency:
|
|
||||||
args.frequency = random.randint(10, 40)
|
|
||||||
output = create_wpotd(
|
|
||||||
args.width,
|
|
||||||
args.height,
|
|
||||||
dark=args.dark,
|
|
||||||
wave_offset=args.offset,
|
|
||||||
frequency=args.frequency,
|
|
||||||
)
|
|
||||||
if args.stdout:
|
|
||||||
import sys
|
|
||||||
|
|
||||||
output.write_to_png(sys.stdout.buffer)
|
if __name__ == '__main__':
|
||||||
else:
|
|
||||||
output.write_to_png("out/waves.png")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user