-
Notifications
You must be signed in to change notification settings - Fork 2
/
gradientgen.py
44 lines (33 loc) · 2.69 KB
/
gradientgen.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# script by HYOUG
from PIL import Image
from sys import argv
from os import getcwd
from random import randint
def gen_gradient(xy_size: tuple = (1000, 1000), rgb_start: tuple = (randint(0, 255) for i in range(3)), rgb_end: tuple = (randint(0, 255) for i in range(3)), fp: str = f"{getcwd()}/gradient.png") -> None:
"""Generate a gradient from the given format and the two RGB given"""
xy_size = list(map(int, xy_size)) # convert the function's arguments
rgb_start = list(map(int, rgb_start)) # //
rgb_end = list(map(int, rgb_end)) # //
r_gap = (rgb_end[0] - rgb_start[0]) / xy_size[0] # calculate the gap of the "R" value for every column
g_gap = (rgb_end[1] - rgb_start[1]) / xy_size[0] # calculate the gap of the "G" value for every column
b_gap = (rgb_end[2] - rgb_start[2]) / xy_size[0] # calculate the gap of the "B" value for every column
im = Image.new("RGB", xy_size) # create a new picture (RGB format, with the given XY size)
for x in range(xy_size[0]): # iterate the x value of the picture
column_color = (int(rgb_start[0] + r_gap * x), # calculate the RGB value of the column
int(rgb_start[1] + g_gap * x),
int(rgb_start[2] + b_gap * x))
for y in range(xy_size[1]): # iterate the y value of the image
im.putpixel((x, y), column_color) # apply the RGB value on the whole column
im.save(fp) # save the generated picture
im.close() # close the picture's editing
if __name__ == "__main__": # check if the script is the main program
xy_size = tuple(argv[1:3]) # fatch command line arguments
rgb_start = tuple(argv[3:6]) # //
rgb_end = tuple(argv[6:]) # //
gen_gradient(xy_size, rgb_start, rgb_end) # call the gen_gradient function with the command line arguments
"""
TODO :
command line arguments parser
"""