Python Turtle 透明度?

5

请问,是否可以让海龟使用半透明墨水进行绘制/填充?

类似于:

turtle.setfillopacity(50) # Would set it to 50% transparency

运行 Python 2.7


我找不到任何信息。通常,它将是一个可选的第四个颜色分量(r,g,b,a),但是文档没有提到它。 - Marcelo Cantos
7个回答

2
您可以通过以下方式实现:
import turtle
turtle = turtle.Turtle()
r = 100
g = 100
b = 100
a = 0.5
turtle.color(r,g,b,a)

(好的,也许它只适用于repl.it)
注:保留html标签,不做解释。

1

好的,你可以使用RGBA
首先,输入正常语句:

  1. import turtle
  2. t = turtle.Turtle()

然后,使用t.color(),但使用RGBA
RGBA的第一部分与RGB相同,最后一个值是不透明度的百分比(其中0为透明,1为不透明)。

  1. t.color(0,0,0,.5)

将使您以50%不透明度变黑。


1

这是不可能的,但你可以定义你的颜色,然后定义一个浅色版本,并使用它们。

Red = (255,0,0,0)
LRed = (100,0,0)

我认为这样可以达到类似的效果。当你想要半透明时,你只需要使用较浅的颜色。


3
如果你正在绘画的物体后面有东西,这种方法就不可行了。其颜色仍会被覆盖,而不是在上方颜色的一部分可见。 - mbomb007

1
也许这可以帮助你实现你所期望的结果。
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%的不透明度。


0

这个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的邮票未经修改。


0

不可能直接将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

这将给你一个像这样的结果: 一个红色圆圈上面有一个透明的绿色三角形。老实说,现在在2023年,如果绘画本身不太复杂,你也可以使用其他库(如matplotlib、seaborn、plotly或PIL)从头开始绘制你的绘画,尽管我仍然发现turtle有时对于绘制曲线或圆圈很有用。

-1

如果您想要完全不透明,可以使用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()

1
"turtle.hideturtle()" 不仅可以使海龟图形透明,还可以使其无法被点击。此外,完全的隐形并不是透明度的唯一用途。通常情况下,您需要部分透明度。 - ggorlen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接