当前位置:   article > 正文

python画坤坤(高还原绘图) 附源码_用python画蔡徐坤代码

用python画蔡徐坤代码

目录

一、前言

 二、源码


一、前言

作为一个真ikun,这第一篇文章当然要写我们最帅的坤坤啦,哈哈哈~~~

先来看看效果图:

左边网上找的原图,右边是python turtle绘制的图形

 程序中的数据都是我在ppt中用绘图标尺量出来的,各块颜色也是用取色器取的,基本是高度还原图像了

 二、源码

然后下面是源码,将近180行:

  1. import turtle as t
  2. import math
  3. t.setup(1000, 750)
  4. t.setworldcoordinates(-800,-600,800,600)
  5. t.title('I am ikun!!')
  6. t.width(8)
  7. t.speed(0)
  8. t.pencolor('black')
  9. #以圆心和半径画圆
  10. def my_circle(rad,c_x,c_y,color=None):
  11. if color is not None:
  12. t.fillcolor(color)
  13. t.begin_fill()
  14. t.penup()
  15. t.setheading(0)
  16. t.goto(c_x,c_y-rad)
  17. t.pendown()
  18. t.circle(rad)
  19. if color is not None:
  20. t.end_fill()
  21. #求θ角度方向上椭圆的坐标
  22. def get_ellipse_xy(a,b,theta):
  23. if theta < 0:theta=theta+math.pi*2
  24. x = a * b / math.sqrt(b * b + a * a * math.tan(theta) * math.tan(theta))
  25. if theta < math.pi/2:
  26. return {'x':x,'y':x*math.tan(theta)}
  27. elif theta < math.pi:
  28. return {'x':x*(-1),'y':x*(-1)*math.tan(theta)}
  29. elif theta < math.pi*3/2:
  30. return {'x':x*(-1),'y':x*(-1)*math.tan(theta)}
  31. else:
  32. return {'x': x, 'y': x*math.tan(theta)}
  33. # 画一个椭圆,shape为椭圆形状参数,start_ang、end_ang为起始角度
  34. # shape = {"X0": 0,"Y0": 0,"a": 200,"b": 100,"angle": math.pi/3}
  35. def ellipse(shape,start_ang,end_ang,color=None):
  36. if color is not None:
  37. t.fillcolor(color)
  38. t.begin_fill()
  39. a = shape['a']
  40. b = shape['b']
  41. shape_ang = shape['angle']
  42. theta = start_ang - shape_ang
  43. x1, y1 = get_ellipse_xy(a, b, theta).values()
  44. x = shape['X0'] + x1 * math.cos(shape_ang) - y1 * math.sin(shape_ang)
  45. y = shape['Y0'] + x1 * math.sin(shape_ang) + y1 * math.cos(shape_ang)
  46. t.penup()
  47. t.goto(x,y)
  48. t.pendown()
  49. step = math.pi/180*2 # 以方位角2°一步
  50. num_steps = math.ceil((end_ang-start_ang)/step) # 总步数
  51. for i in range(num_steps):
  52. theta = theta + step
  53. x1, y1 = get_ellipse_xy(a, b, theta).values()
  54. t.goto(shape['X0'] + x1 * math.cos(shape_ang) - y1 * math.sin(shape_ang),
  55. shape['Y0'] + x1 * math.sin(shape_ang) + y1 * math.cos(shape_ang))
  56. if color is not None:
  57. t.end_fill()
  58. #画篮球
  59. my_circle(150, -206, -212, '#BA7148')
  60. baskt_line1 = {"X0": -120,"Y0": -34,"a": 186,"b": 162,"angle": 0}
  61. ellipse(baskt_line1, math.pi/180*198, math.pi/180*290)
  62. baskt_line2 = {"X0": -294,"Y0": -402,"a": 186,"b": 162,"angle": 0}
  63. ellipse(baskt_line2, math.pi/180*21, math.pi/180*110)
  64. t.penup()
  65. t.goto(-346,-160)
  66. t.pendown()
  67. t.goto(-66,-274)
  68. # 画脸蛋
  69. face = {"X0": 80,"Y0": -22,"a": 256,"b": 198,"angle": 0}
  70. ellipse(face, 0, math.pi*2,'#F5D477')
  71. #眼睛
  72. my_circle(77, 63, 41, 'white')
  73. my_circle(68, 217, 41, 'white')
  74. my_circle(24, 100, 34, 'black')
  75. my_circle(24, 244, 34, 'black')
  76. #嘴巴
  77. t.width(5)
  78. mouse = {"X0": 145,"Y0": -73,"a": 75,"b": 53,"angle": 0}
  79. ellipse(mouse, 0, math.pi*2,'#F4A644')
  80. mouse_line = {"X0": 138,"Y0": -40,"a": 92,"b": 53,"angle": 0}
  81. ellipse(mouse_line, math.pi/180*208, math.pi/180*342,'#F4A644')
  82. #腮红
  83. t.width(1)
  84. t.pencolor('#F5D477')
  85. my_circle(62, -82, -62, 'red') # 左边
  86. face_cheek = {"X0": 294,"Y0": -66,"a": 37,"b": 60,"angle": -math.pi/180*12}
  87. ellipse(face_cheek, 0, math.pi*2,'red') # 右边
  88. #腮红有点遮住脸的轮廓,重新绘制一遍
  89. t.pencolor('black')
  90. t.width(8)
  91. ellipse(face, -math.pi/3, math.pi/3)
  92. # 定义一个画polygon的函数
  93. def draw_poly(poly_data,color=None):
  94. x=poly_data['x']
  95. y=poly_data['y']
  96. t.penup()
  97. t.goto(x[0], y[0])
  98. t.pendown()
  99. if color is not None:
  100. t.fillcolor(color)
  101. t.begin_fill()
  102. for i in range(len(x)):
  103. t.goto(x[i], y[i])
  104. if color is not None:
  105. t.end_fill()
  106. # 画头发
  107. poly_hair = {'x': [-258, -161, -74, 0, 55, 111, 211, 315, 362,
  108. 329, 293, 283, 269, 227, 269, 283, 208, 194,
  109. 160, 160, 85, 44, 61, 44, 31, 1, -33,
  110. 1, -60, -51, -60, -62, -129, -142, -144, -108,
  111. -144, -142, -209, -216, -200, -216, -258],
  112. 'y': [57, 187, 238, 267, 251, 296, 260, 171,
  113. 47, -9, 29, 61, 110, 166, 110, 61, 72, 132,
  114. 178, 178, 174, 162, 206, 162,
  115. 29, 35, 54, 35, 4, 40, 4, -37, -45, -8, 71,
  116. 152, 71, -8, -31, 31, 90, 31, 57]
  117. }
  118. draw_poly(poly_hair, '#D0CED1')
  119. # 头发下那个小三角
  120. hair2 = {'x': [160, 114, 85], 'y': [178, 218, 174]}
  121. draw_poly(hair2, '#797572')
  122. # 衣服
  123. poly_cloth = {'x': [-142, -112, -22, 50, 132, 218, 249, 247,
  124. 295, 328, 318, 321, 309, 338, 353, -167,
  125. -150, -165, -166, -150, -162, -157, -142],
  126. 'y': [-135, -155, -144, -140, -150, -166, -163, -150,
  127. -145, -165, -194, -233, -244, -290, -326, -328,
  128. -248, -233, -209, -195, -167, -146, -135]
  129. }
  130. draw_poly(poly_cloth, '#222222')
  131. cloth2 = {'x': [-58, 0, 89, 146, 205, 250, 212, 179, 89, 26, -27, -58],
  132. 'y': [-207, -203, -178, -184, -202, -208, -236, -246, -243, -237, -233, -207]
  133. }
  134. t.width(2)
  135. draw_poly(cloth2, '#0A0A0C') #中间黑的那一块
  136. # 左右背带
  137. strap1 = {'x': [-150, -92, -57, -41, -39, -46], 'y': [-220, -227, -243, -277, -306, -328]}
  138. strap2 = {'x': [309, 269, 238, 224, 222], 'y': [-222, -233, -257, -292, -326]}
  139. t.width(18)
  140. t.pencolor('#D3D1D4')
  141. draw_poly(strap1)
  142. draw_poly(strap2)
  143. #中间背带
  144. t.width(10)
  145. strap3 = {'x': [-17, 90, 209, 90, 93],'y': [-251, -273, -248, -273, -290]}
  146. draw_poly(strap3)
  147. t.width(2)
  148. t.pencolor('white')
  149. my_circle(30, 97, -320, '#D1D1D3')
  150. # 文字kun
  151. k = {'x': [78, 78, 78, 88, 78, 88], 'y': [-312, -326, -319, -312, -319, -326]}
  152. draw_poly(k)
  153. u = {'x': [93, 93, 98, 103, 103], 'y': [-312, -323, -326, -323, -312]}
  154. draw_poly(u)
  155. n = {'x': [109, 109, 119, 119], 'y': [-326, -312, -326, -312]}
  156. draw_poly(n)
  157. t.resizemode("user")
  158. t.shapesize(0.8, 0.8)
  159. t.hideturtle()
  160. t.done()

 大家喜欢也坤坤吗?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/81064
推荐阅读
相关标签
  

闽ICP备14008679号