本文主要是介绍随机涂鸦 pil,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随机涂鸦 pil
import math
import time
import cv2img1=cv2.imread("/home/lixiang/下载/mp4/111.jpg")from PIL import Image, ImageDraw
import randomdef tuya(img):draw = ImageDraw.Draw(img)# 涂鸦数量num_scribbles = 8 # 不规则形状数量# 随机不规则形状生成for _ in range(num_scribbles):# 随机生成不规则形状的中心点center_x = random.randint(0, img.width)center_y = random.randint(0, img.height)# 随机生成形状的顶点数量和半径# num_vertices = random.randint(20, 40) # 顶点数量更多,变化更细腻num_vertices = random.randint(10, 40) # 顶点数量更多,变化更细腻base_radius = random.randint(50, 300) # 基础半径# 随机生成顶点,围绕中心点生成不规则形状vertices = []for i in range(num_vertices):# 随机扰动角度和半径angle = i * (2 * math.pi / num_vertices) + random.uniform(-0.3, 0.3)noise_factor = random.uniform(0.7, 1.3) # 半径的随机扰动因子r = base_radius * noise_factor# 噪声加入到顶点位置的随机抖动noise_x = random.uniform(-10, 10)noise_y = random.uniform(-10, 10)# 计算顶点的实际坐标x = center_x + int(r * math.cos(angle)) + int(noise_x)y = center_y + int(r * math.sin(angle)) + int(noise_y)vertices.append((x, y))# 绘制不规则多边形draw.polygon(vertices, fill="black", outline="black")for i in range(20):img = Image.open("/home/lixiang/下载/mp4/111.jpg")start=time.time()tuya(img)print('time',time.time()-start)# img.save("scribbled_image.jpg")img.show()# print(img1.shape)
这篇关于随机涂鸦 pil的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!