5

Just wondering, is it possible to make a turtle draw/fill with semi-transparent ink?

Something like:

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

Running python 2.7

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
Brennan Wilkes
  • 213
  • 3
  • 4
  • 13
  • I can't find anything. Normally, it would be an optional fourth color component (r, g, b, a), but the documentation makes no mention of it. – Marcelo Cantos Dec 03 '13 at 10:30

7 Answers7

2

You can by doing

import turtle
turtle = turtle.Turtle()
r = 100
g = 100
b = 100
a = 0.5
turtle.color(r,g,b,a)

(well, maybe it only works for repl.it)

Tech Zachary ZN
  • 109
  • 1
  • 3
1

It's not possible to do that, but you could define your colors, and then a light equivalent, and use those.

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

I think that would achieve similar effects. You could then just use a lighter color when you want it semi-transparent.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 3
    This wouldn't work if there is something behind what you are painting on. It will still be overwritten with the solid color, rather than being partially visible through the colors on top. – mbomb007 Sep 07 '16 at 21:45
1

Well, you can use RGBA.
First, put in the normal statements:

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

Then, use t.color(), but use RGBA.
The first portion of RGBA is the same as RGB, and the last value is the percentage of opacity (where 0 is transparent, 1 is opaque.)

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

will get you black with 50% opacity.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
0

This python turtle example fades out the text while keeping the original turtle stamps unmodified:

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)

The text simulates an opacity increase to maximum. Alex's stamps are unmodified.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
0

It's not possible to directly apply an RGBA color as turtle.pencolor() or turtle.fillcolor() does not support it at all. Alternatively, I tried to create the solid color part by turtle and the part needs opacity with matplotlib, with thanks to svg-turtle package developed by @Don-kirkby. Here is an example of how this is achieved:

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

This will give you result like this: a red circle with a transparent green triangle on it. Honestly, now in 2023 you can also draw your painting from scratch with other libs like matplotlib, seaborn, plotly, or PIL if the painting itself is not too complicated, though I still found turtle sometimes useful for plotting curves or circles.

Community
  • 1
  • 1
Me W
  • 1
  • 2
0

Maybe this will help you to achieve your desired result.

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()

Here, the screen.bgcolor() method is called with a tuple (1, 1, 1, 0.5) to set the background color. The tuple represents the RGBA values, where (1, 1, 1) corresponds to full white and 0.5 corresponds to 50% opacity.

-1

you can do this by using turtle.hideturtle() if you want full opacity.

Like used here in the last line:

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()
therealemg
  • 35
  • 6
  • 1
    `turtle.hideturtle()` does more than opacity, it also makes it unclickable. Not only that, but total invisibility isn't the only use case for opacity. Often, you want partial transparency. – ggorlen Feb 05 '22 at 22:23