You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Turtle绘制Damien Hirst风格画作报错:颜色序列无效

Python Turtle绘制Damien Hirst风格画作时的"bad color sequence"错误修复

问题场景

想用Python Turtle绘制Damien Hirst风格的彩色圆点画作,使用turtle.dot()函数,通过colorgram包提取自定义颜色,但运行代码时触发"bad color sequence"错误,课程里的实现方式和当前思路完全不同,未找到解决办法。

原代码

#create a hirst art piece
#download the colorgram art package

import os #import files on os comp
import colorgram #import package

from turtle import *
import turtle as t
import random

tim = t.Turtle()

"""
# Find the desktop path
desktop_path = os.path.expanduser("~/Desktop")

# Construct the full file path
image_path = os.path.join(desktop_path, "download.jpg")

# Extract colors from the image
colours = colorgram.extract(image_path, 12)

# Convert to RGB format
#rgb_colors = [(color.rgb.r, color.rgb.g, color.rgb.b) for color in colours] #
#rgb = proportion of each colour of light(red blue and green) in a given colour

print(rgb_colors)

#alternative:
rgb_colors = [] #create empty loop for colours
for color in colours:
rgb_colors.append((color.rgb.r, color.rgb.g, color.rgb.b)) #add the colour
#will print 12 values because we asked it to print 12 colours

print(rgb_colors)

"""

#colour list final
#use the things obtained via colorgram
#omit colours with rgb near 255(= white)

finalColourList = [(240, 232, 52), (225, 153, 66), (195, 12, 37), (189, 67, 29), (239, 44, 127), (37, 84, 178), (48, 214, 62), (233, 230, 4)]

for i in range(1, 100):
    colourChoice = random.choice(finalColourList)
    tim.penup()
    tim.pencolor(colourChoice)
    tim.dot(50)  # Set the size
    tim.forward(20)  # Move the turtle forward


screen = Screen()
screen.exitonclick()

报错信息

/Users/k/PycharmProjects/TurtleGraphics/venv/bin/python /Users/k/PycharmProjects/TurtleGraphics/art.piece.py 
Traceback (most recent call last):
  File "/Users/k/PycharmProjects/TurtleGraphics/art.piece.py", line 49, in <module>
    tim.pencolor(colourChoice)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/turtle.py", line 2253, in pencolor
    color = self._colorstr(args)
            ^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/turtle.py", line 2697, in _colorstr
    return self.screen._colorstr(args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/turtle.py", line 1167, in _colorstr
    raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (189, 67, 29)

Process finished with exit code 1

问题原因及修复方案

原因

Turtle库默认的颜色模式是使用0-1之间的浮点数表示RGB值,而colorgram提取的是0-255范围的整数RGB值,直接传入pencolor()会触发无效颜色序列错误。

修复步骤

方案一:设置颜色模式为255(推荐)

在初始化Turtle对象后添加一行代码,告诉Turtle允许使用0-255的整数RGB值:

tim = t.Turtle()
t.colormode(255)  # 新增该行,无需修改原颜色列表

方案二:转换RGB值为浮点数

将颜色列表中的每个整数除以255,转换为0-1之间的浮点数:

# 手动转换现有颜色列表
finalColourList = [(r/255, g/255, b/255) for r, g, b in finalColourList]

额外优化建议

原代码会让圆点一直向右排列超出画布,可修改为网格布局,且dot()方法可直接传入颜色参数,无需单独调用pencolor():

tim = t.Turtle()
t.colormode(255)
tim.speed("fastest")
tim.penup()
tim.hideturtle()

finalColourList = [(240, 232, 52), (225, 153, 66), (195, 12, 37), (189, 67, 29), (239, 44, 127), (37, 84, 178), (48, 214, 62), (233, 230, 4)]

# 设置起始位置,从画布左下角开始绘制
tim.setposition(-200, -200)

for row in range(10):
    for _ in range(10):
        colourChoice = random.choice(finalColourList)
        tim.dot(50, colourChoice)
        tim.forward(60)
    # 移动到下一行的起始位置
    tim.setposition(-200, tim.ycor() + 60)

内容的提问来源于stack exchange,提问作者Vaibhavi Jha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 14:16:15