Takže takhle?
import subprocess, os, re, locale, glob, shutil
encoding = locale.getdefaultlocale()[1]
zbarimg = "zbarimg"
def get_codes(filename):
try:
output = subprocess.check_output([zbarimg, "-q", filename])
lines = output.strip().split(b'\n')
lines = [line.decode(encoding) for line in lines]
codes = [line.split(':',1)[1].strip() for line in lines]
return codes if len(codes) else ["unreadable_code"]
except:
return ["unreadable_code"]
def make_path(code, filename):
safecode = re.sub("[:/\\\\]", "_", code)
fullpath = os.path.join(safecode, filename)
if os.path.exists(fullpath):
dirname = os.path.dirname(fullpath)
filename,fileext = os.path.splitext(os.path.basename(fullpath))
i = 0
while os.path.exists(fullpath):
i += 1
fullpath = os.path.join(dirname, "%s (%d)%s" % (filename, i, fileext))
return fullpath
image_files = glob.glob('*.JPG') + glob.glob('*.png')
print(image_files)
for image in image_files:
print("======")
print("file:", image)
codes = get_codes(image)
for code in codes:
print("code:", code)
dest = make_path(code, image)
print("dest:", dest)
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copyfile(image, dest)
os.remove(image)