diff --git a/main.py b/main.py index acfc3bc..c162b37 100644 --- a/main.py +++ b/main.py @@ -28,8 +28,8 @@ new_height = int(input("请输入新高度(px):")) # new_height = 128 # 读入图片 -imgFile = open(img, 'rb') -imgBytes = imgFile.read() +with open(img, 'rb') as f: + imgBytes = f.read() # 文件头处理 # 检验文件头是否为 BM @@ -170,7 +170,4 @@ for i in range(len(newpixels)): newimgBytes = bytes(newimgArray) with open(img[:-4] + "_new" + img[-4:], 'wb') as f: - f.write(newimgBytes) - -# 关闭文件 -imgFile.close() \ No newline at end of file + f.write(newimgBytes) \ No newline at end of file