藝術來源生活,美可能是無意中生成的,那么我想在每次生成一個隨機顏色分布的圖片該怎么做呢》》》》》
通過代碼生成了一批成果圖,找出自己喜歡的就好了,哈哈哈



import numpy as np
import random
import cv2 as cv
h = 600
w = 600
img = 255 * np.ones((h ,w , 3), dtype=np.uint8) #先生成一張600*600像素的白圖
for j in range(h): # 回圈遍歷所有像素點,替換像素點
for k in range(w):
img[j,k,0] = random.randint(0,255) #隨機生成一個0到255之間的整數 可以通過挑引數設定不同的顏色范圍
img[j,k,1] = random.randint(0,255)
img[j, k, 2] = random.randint(0, 255)
cv.imshow("result4", img)
cv.waitKey(0)
這樣生成的圖片肯定是沒有什么美觀的,這時候我們從漸變入手,看看能否有點突破,

from PIL import Image,ImageDraw
def gradient(list_of_colors):
width = 600
height = 480
img = Image.new("RGB", (width, height))
draw = ImageDraw.Draw(img)
for i in range(len(list_of_colors)):
r1,g1,b1 = list_of_colors[i]
for x in range(width//len(list_of_colors)):
colour = (r1,g1,b1)
draw.line((x+(width/len(list_of_colors)*i), 0, x+(width/len(list_of_colors)*i), height), fill=colour)
img.show()
gradient([(30, 198, 244), (99, 200, 72),(120, 50, 80),(200, 90, 140)])
看看還有什么其他的漸變方法呢,

import math
from PIL import Image
im = Image.new('RGB', (600, 600))
ld = im.load()
def gaussian(x, a, b, c, d=0):
return a * math.exp(-(x - b)**2 / (2 * c**2)) + d
for x in range(im.size[0]):
r = int(gaussian(x, 158.8242, 201, 87.0739) + gaussian(x, 158.8242, 402, 87.0739))
g = int(gaussian(x, 129.9851, 157.7571, 108.0298) + gaussian(x, 200.6831, 399.4535, 143.6828))
b = int(gaussian(x, 231.3135, 206.4774, 201.5447) + gaussian(x, 17.1017, 395.8819, 39.3148))
for y in range(im.size[1]):
ld[x, y] = (r, g, b)
im.show()

import numpy as np
import random
import cv2 as cv
from PIL import Image,ImageDraw
h = 600
w = 600
img = 255 * np.ones((h ,w , 3), dtype=np.uint8) #先生成一張600*600像素的白圖
x = 0 #用于平移
y = 0
rate = 30 # 將圖片分成rate*rate區域
length = h//rate #每次移動距離
length2 = w//rate #每次移動距離
#回圈遍歷代碼劃分rate*rate區域來填充不同的顏色
for i in range(rate):
for m in range(rate):
color = random.randint(10,245)#每個區域的顏色隨機生成
for j in range(x,x+length): # 回圈遍歷所有像素點,替換像素點
for k in range(y,y+length2):
img[j,k,0] = random.randint(color-10,color+10) #隨機生成一個0到255之間的整數 可以通過挑引數設定不同的顏色范圍
img[j,k,1] = random.randint(color-10,color+10)
img[j, k, 2] = random.randint(color-10,color+10)
y += length2
x += length
y = 0
cv.imshow("result", img)
cv.waitKey(0)
我感覺這個有點像 那個啥 ,哈哈哈,在嘗試一下

import numpy as np
import random
import cv2 as cv
from PIL import Image,ImageDraw
h = 600
w = 600
img = 255 * np.ones((h ,w , 3), dtype=np.uint8) #先生成一張600*600像素的白圖
x = 0 #用于平移
y = 0
rate = 30 # 將圖片分成rate*rate區域
length = h//rate #每次移動距離
length2 = w//rate #每次移動距離
#回圈遍歷代碼劃分rate*rate區域來填充不同的顏色
for i in range(rate):
for m in range(rate):
color1 = random.randint(150, 255)#每個區域的顏色隨機生成
color2 = random.randint(100, 200)
color3 = random.randint(0, 100)
for j in range(x,x+length): # 回圈遍歷所有像素點,替換像素點
for k in range(y,y+length2):
img[j,k,0] = color3 #隨機生成一個0到255之間的整數 可以通過挑引數設定不同的顏色范圍
img[j,k,1] = color2
img[j, k, 2] = color1
y += length2
x += length
y = 0
cv.imshow("result", img)
cv.waitKey(0)
阿西吧就這樣把
有需要寫代碼的可以加扣334542894
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292445.html
標籤:其他
