Created
April 25, 2020 08:21
-
-
Save redjack001/169e388d031925743b83f525552565e8 to your computer and use it in GitHub Desktop.
Create a gradient picture from python
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
from PIL import Image | |
#set the size of the new image | |
w = 500 | |
h = 200 | |
#start creating gradient | |
pixel_list = [] | |
pixel_w = [] | |
#colour at 0,0 | |
start1 = (255,255,255) | |
s = list(start1) | |
pixel_list.append(start1) | |
print('Position zero:', pixel_list[0]) | |
#colour at end | |
end1 = (174,15,15) | |
e = list(end1) | |
#in case you want to change the final colour you could use f to adjust otherwise just keep it at 1 | |
f = 1 | |
#transition per line | |
r = (s[0] - e[0])/w*f | |
g = (s[1] - e[1])/w*f | |
b = (s[2] - e[2])/w*f | |
t = () | |
for j in range (0,w): | |
t = pixel_list[j] | |
#convert pixel tuple to a list and recalculate | |
li = list(t) | |
li[0] = int(max((li[0] - r*j),0)) | |
li[1] = int(max((li[1] - g*j),0)) | |
li[2] = int(max((li[2] - b*j),0)) | |
z = (li[0],li[1],li[2]) | |
final_t = tuple(z) | |
#print('final_t:', final_t) if you want to show the list of pixel values | |
pixel_list[j] = final_t | |
for i in range (0,h): | |
pixel_w = [] | |
pixel_w.append(final_t) | |
pixel_list.extend(pixel_w) | |
l = len(pixel_list) | |
print('Pixel_list:', pixel_list, l) | |
#remove the last item | |
del pixel_list[l-1:] | |
print('Reprint length:', len(pixel_list)) | |
im = Image.new('RGB', (w,h)) | |
im.putdata(pixel_list) | |
im.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment