问题遇到的现象和发生背景
编写字符画,想打开一个设置的json文件。文件有了,用open()打开时它说没有该文件。
问题相关代码,请勿粘贴截图
from PIL import Image
import json
class Img(Image.Image):
def __init__(self, img: Image.Image):
self.img = img.convert('L')
def get_img(self):
return self.img
@staticmethod
def getchar(gray, ls=list('W#C)|^. ')):
print(len(ls))
if 65536 % len(ls) != 0:
raise ValueError('Input data must divide 65536 (that is, a multiple of 4)')
return ls[int(gray // (256 // len(ls)))]
class write_file(object):
def __init__(self, filename, text, mode='w', ):
self.file = open(filename, mode)
self.file.write(text)
try:
self.close()
except:
pass
def close(self):
self.file.close()
def main(img_name='exp.jpg'):
setting_file = open('setting.json', 'r')
setting = json.load(setting_file)
t = 0.2
img = Image.open(img_name)
img = Img(img).get_img().resize((round(img.width*t), round(img.height*t)))
text = ''
for y in range(img.height):
for x in range(img.width):
text += Img.getchar(img.getpixel((x, y)))
text += '\n'
fo = write_file('ZiFuHua.txt', text)
运行结果及报错内容
Traceback (most recent call last):
File "D:\py_proj\XingYuan\字符画\字符画.py", line 62, in <module>
main()
File "D:\py_proj\XingYuan\字符画\字符画.py", line 34, in main
setting_file = open('setting.json', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'setting.json'
我想要达到的结果
它咋就看不着那个文件呢??