fuill 2022-11-02 15:03 采纳率: 68.2%
浏览 64
已结题

python代码报错问题

img


python PIL库报错还是

img


安装了pillow的
运行报错为

Traceback (most recent call last):
  File "c:\Users\123\Desktop\Untitled-555.py", line 65, in <module>
    main((200,100),8,(255,255,255))
  File "c:\Users\123\Desktop\Untitled-555.py", line 26, in main
    font=ImageFont.truetype('d:\python\times.ttf',48)
  File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageFont.py", line 976, in truetype
    return freetype(font)
  File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageFont.py", line 973, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageFont.py", line 249, in __init__
    self.font = core.getfont(
OSError: cannot open resource
PS C:\Users\123>

代码


from importlib.machinery import OPTIMIZED_BYTECODE_SUFFIXES
import random
import string
from tkinter import OFF
from turtle import position
from xml.dom.pulldom import CHARACTERS
from PIL import Image, ImageDraw, ImageFont

characters=string.ascii_letters+string.digits

def selectCharacters(length):
    result=""
    for i in range(length):
        result+=random.choice(characters)
        return result

def getColor():
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    return (r, g, b)

def main(size=(200,100),characterNumber=6,bgcolor=(255,255,255)):
    imageTemp=Image.new('RGB',size,bgcolor)
    font=ImageFont.truetype('d:\python\times.ttf',48)
    draw=ImageDraw.Draw(imageTemp)
    text=selectedCharacters(characterNumber)
    width,hight=draw.textsize(text,font)
    offset=2
    for i in range(characterNumber):
        offset+=width//characterNumber
        position=(offset,(size[1]-height)//2+random.randint(-10,10))
        draw.text(xy=position,text=text[i],font=font,fill=getColor())
    imageFinal=Image.new('RGB',size,bgcolor)
    pixelsFinal=imageFinal.load()
    for y in range(0,size[1]):
        offset=random.randint(-1,1)
        for x in range(0,size[0]):
            newx=x+offset
            if newx>size[0]:
                newx=size[0]-1
            elif newx<0:
                newx=0
            pixelsFinal[newx,y]=pixelsTemp[x,y]
    draw=ImageDraw.Draw(imageFinal)

    for i in range(int(size[0]*size[1]*0.07)):
        draw.point((random.randint(0,size[0]),random.randint(0,size[1])),fill=getColor)

    for i in range(8):
        start=(0,random.randint(0,size[1]))
        end=(size[0],random.randint(0,size[1]-1))
        draw.line([star,end],fill=getColor(),width=1)

    for i in range(8):
        start=(-50,50)
        end=(size[0]+10,random.randint(0,size[1]+10))
        draw.arc(start+end,0,360,fill=getColor())

    imageFinal.save('result.jpg')
    imageFinal.show()

if __name__=='__main__':
    main((200,100),8,(255,255,255))
  • 写回答

2条回答 默认 最新

  • [小G] 2022-11-02 15:29
    关注

    26行,路径用正斜杠/或者反双斜杠\

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月10日
  • 已采纳回答 11月2日
  • 创建了问题 11月2日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!