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

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 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀