请问,是否可以让海龟使用半透明墨水进行绘制/填充?
类似于:
turtle.setfillopacity(50) # Would set it to 50% transparency
运行 Python 2.7
请问,是否可以让海龟使用半透明墨水进行绘制/填充?
类似于:
turtle.setfillopacity(50) # Would set it to 50% transparency
运行 Python 2.7
import turtle
turtle = turtle.Turtle()
r = 100
g = 100
b = 100
a = 0.5
turtle.color(r,g,b,a)
好的,你可以使用RGBA。
首先,输入正常语句:
import turtle
t = turtle.Turtle()
然后,使用t.color()
,但使用RGBA。
RGBA的第一部分与RGB相同,最后一个值是不透明度的百分比(其中0为透明,1为不透明)。
t.color(0,0,0,.5)
将使您以50%不透明度变黑。
这是不可能的,但你可以定义你的颜色,然后定义一个浅色版本,并使用它们。
Red = (255,0,0,0)
LRed = (100,0,0)
我认为这样可以达到类似的效果。当你想要半透明时,你只需要使用较浅的颜色。
import turtle
# Create a turtle object
tr = turtle.Turtle()
# Set up the screen
screen = turtle.Screen()
screen.setup(800, 600)
# Set the background color with alpha value (RGBA format)
screen.bgcolor((1, 1, 1, 0.5)) # 50% transparency (0.5 alpha value)
# Draw using the turtle
tr.begin_fill()
tr.circle(80)
tr.end_fill()
# Keep the turtle window open until it's closed by the user
turtle.done()
在这里,使用元组(1, 1, 1, 0.5)调用screen.bgcolor()方法来设置背景颜色。该元组表示RGBA值,其中(1, 1, 1)对应完全白色,0.5对应50%的不透明度。
这个Python Turtle示例可以在保持原始海龟图章不变的同时淡化文本:
import turtle
import time
alex = turtle.Turtle()
alex_text = turtle.Turtle()
alex_text.goto(alex.position()[0], alex.position()[1])
alex_text.pencolor((0, 0, 0)) #black
alex_text.write("hello")
time.sleep(1)
alex_text.clear()
alex_text.pencolor((.1, .1, .1)) #dark grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((.5, .5, .5)) #Grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((.8, .8, .8)) #Light grey
alex_text.write("hello")
time.sleep(1)
alex_text.pencolor((1, 1, 1)) #white
alex_text.write("hello")
time.sleep(1)
alex_text.clear() #gone
time.sleep(1)
该文本模拟了不透明度增加到最大。Alex的邮票未经修改。
不可能直接将RGBA颜色应用于turtle.pencolor()
或turtle.fillcolor()
,因为它们完全不支持。作为替代方案,我尝试使用turtle
创建实色部分,并使用matplotlib
为需要透明度的部分添加透明度,感谢@Don-kirkby开发的svg-turtle package。以下是实现此目标的示例:
pip install svg_turtle
pip install matplotlib
pip install svgutils
import turtle
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import svgutils.transform as st
from svg_turtle import SvgTurtle
import os
def draw_turtle_part(file_path):
# draw a circle with solid color
t = SvgTurtle(100, 100)
t.up()
t.goto(0, -50)
t.down()
t.begin_fill()
t.fillcolor('red')
t.circle(40)
t.end_fill()
# save it as a svg file
t.save_as(file_path)
return
def draw_opacity_part(file_path):
# draw a triangle with a color of 50% opacity
fig, ax = plt.subplots(figsize=(10, 10), dpi=300)
ax.set_xlim(-50, 50) # set the canvas size scale the same as turtle
ax.set_ylim(-50, 50) # set the canvas size scale the same as turtle
ax.axis('off') # hide the axis
# draw and fill a triangle with an RGBA color
polygon = patches.Polygon(
[(-50, 0), (0, 50), (50, 0), (-50, 0)],
closed=True, linewidth=0, fill=True, color=(0, 0.8, 0, 0.5))
ax.add_patch(polygon)
# save the figure, remove the paddings and white space surrounding the plot
plt.savefig(file_path, format='svg', transparent=True, bbox_inches='tight', pad_inches=0)
return
def combine_two_parts(file_1, file_2):
x, y = 100, 100
fig1 = st.fromfile(file_1)
# resize the figure to make sure the two align
fig1.set_size((f'{x}pt', f'{y}pt'))
fig2 = st.fromfile(file_2)
# resize the figure to make sure the two align
fig2.set_size((f'{x}px', f'{y}px'))
fig1.append(fig2)
fig1.save('result.svg')
if __name__ == '__main__':
draw_turtle_part('test1.svg')
draw_opacity_part('test2.svg')
combine_two_parts('test1.svg', 'test2.svg')
os.remove('test1.svg') # optional
os.remove('test2.svg') # optional
turtle
有时对于绘制曲线或圆圈很有用。如果您想要完全不透明,可以使用turtle.hideturtle()
来实现。
就像在这里的最后一行中所使用的一样:
import turtle
t = turtle.Turtle()
t.speed(1)
t.color("blue")
t.begin_fill()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("red")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("green")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.color("yellow")
t.begin_fill()
t.forward(101)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.end_fill()
t.hideturtle()