#!/usr/bin/env python2.7 # coding=UTF-8 import os import sys import cv2 from pyzbar import pyzbar def main(image_folder_path, output_file_name): img_files = [f for f in os.listdir(image_folder_path) if f.endswith(('.png'))] qr_codes_found = [] print("Image files:") for img_file in img_files: print(img_file) for img_file in img_files: img_path = os.path.join(image_folder_path,img_file) img = cv2.imread(img_path) barcodes = pyzbar.decode(img) for barcode in barcodes: if barcode.type == 'QRCODE': qr_data = barcode.data.decode("utf-8") qr_codes_found.append((img_file, qr_data)) with open(output_file_name,'w') as f: for file_name,qr_content in qr_codes_found: file_name = os.path.splitext(file_name)[0] # 去掉文件名的扩展名 f.write(f"{file_name}: {qr_content}\n") if __name__ == "__main__": image_folder_path = '/root/pictures' output_file_name = 'qr_codes_found.txt' main(image_folder_path,output_file_name)运行后显示IndentationError: unindent does not match any outer indentation level root@myir:~# python2 shibie.py File "shibie.py", line 29 f.write(f"{file_name}: {qr_content}\n")
时间: 2024-04-13 21:31:04 浏览: 215
linux 下 pip3 报错“ File “/usr/bin/pip3”, line 9, in from pip import mainImportError: canno”处理
5星 · 资源好评率100%
这个错误是因为代码中的缩进有问题。在Python中,缩进是非常重要的,它决定了代码的结构和执行顺序。根据错误信息,我可以看到在第29行的`f.write(f"{file_name}: {qr_content}\n")`语句缩进错误。
请确保在该行以及其他使用缩进的地方使用相同数量的空格或制表符。在这个代码片段中,使用4个空格作为缩进。
修复后的代码应该是这样的:
```python
#!/usr/bin/env python2.7
# coding=UTF-8
import os
import sys
import cv2
from pyzbar import pyzbar
def main(image_folder_path, output_file_name):
img_files = [f for f in os.listdir(image_folder_path) if f.endswith(('.png'))]
qr_codes_found = []
print("Image files:")
for img_file in img_files:
print(img_file)
for img_file in img_files:
img_path = os.path.join(image_folder_path,img_file)
img = cv2.imread(img_path)
barcodes = pyzbar.decode(img)
for barcode in barcodes:
if barcode.type == 'QRCODE':
qr_data = barcode.data.decode("utf-8")
qr_codes_found.append((img_file, qr_data))
with open(output_file_name,'w') as f:
for file_name,qr_content in qr_codes_found:
file_name = os.path.splitext(file_name)[0] # 去掉文件名的扩展名
f.write(f"{file_name}: {qr_content}\n")
if __name__ == "__main__":
image_folder_path = '/root/pictures'
output_file_name = 'qr_codes_found.txt'
main(image_folder_path,output_file_name)
```
修复缩进错误后,您可以再次尝试运行代码。
阅读全文