Mun Says
Mun Says

Reputation: 97

cannot open resource: OS Error

OSError Traceback (most recent call last) in ()

21 draw = PIL.ImageDraw.Draw(img) 22 print(os.path.join(path, filename)) ---> 23 draw.font = PIL.ImageFont.truetype((os.path.join(path, filename))+ '.ttf', 44) 24 t2 = get_display(t1) 25 w, h = draw.textsize(t2)

()

OSError: cannot open resource

I have this error in following does it PIL or i did some mistake. it only shows first file path and then this error appears.

unicode_text = u"\u0627"
list_of_letters = list (unicode_text) 
folder = 1 
n=1 
i=0
for i in range(0,158):
    path = r"E:\Dummy\fonts"
    dirs = os.listdir( path )
    for files in dirs:
        char = u''.join(list_of_letters) 
        t1 = arabic_reshaper.reshape(char) 
        W,H= (100, 100)
        img= PIL.Image.new('RGBA', (W, H), (255, 255, 255),)
        draw = PIL.ImageDraw.Draw(img)   
        print(os.path.join(path, filename))
        draw.font = PIL.ImageFont.truetype((os.path.join(path, filename)), 44)
        t2 = get_display(t1) 
        w, h = draw.textsize(t2)
        draw.text(((W-w)/2,(H-h)/2),t2, fill="#000000")
        path = 'E:\Dummy\sam\\'+ str(folder)
        if not os.path.exists(path):
            os.makedirs(path)
        img.save(path + '\\' + char+'.png', "PNG")
        folder+=1
            #i+=1

Upvotes: 2

Views: 1886

Answers (1)

physicalattraction
physicalattraction

Reputation: 6858

It's not very clear from your answer where the program is failing. It complains about not being able to open a file though. This line looks suspicious to me:

path = 'E:\Dummy\sam\\'+ str(folder)

I advise you to never concatenate paths with backslashes manually, but to always use the Python standard library to do this for you. You don't need to care about escaping certain characters for instance.

dir_path = os.path.join('E:', 'Dummy', 'sam', str(folder))
file_name = '{}.png'.format(char)
file_path = os.path.join(dir_path, file_name)

Another improvement in your code can be made if you don't add 1 to folder inside the loop, but use the built-in function enumerate:

for folder_index, file in enumerate(dirs, start=1):
    # Do your thing here
    # The variable folder_index is incremented automatically.

Upvotes: 2

Related Questions