utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. def toStringDigit(number, digit):
  2. snumber = str(number)
  3. return '0'*(digit-len(snumber)) +snumber
  4. def checkString(s,dictConfig):
  5. res = []
  6. begin=0
  7. # prefix
  8. prefixes = dictConfig['prefix']
  9. for p in prefixes:
  10. lenp =len(p)
  11. if s[0:lenp]==p:
  12. res.append(True)
  13. begin = begin + lenp
  14. break
  15. # digit
  16. nbDigit = int(dictConfig['numberDigit'])
  17. sDigit =s[begin:begin+nbDigit]
  18. if sDigit.isdigit():
  19. res.append(True)
  20. begin = begin + nbDigit
  21. # suffix
  22. suffixes = dictConfig['suffix']
  23. for su in suffixes:
  24. lens =len(su)
  25. if s[begin:begin+lens]==su:
  26. res.append(True)
  27. begin = begin + lens
  28. # dot
  29. if s[begin:begin+1]== '.':
  30. res.append(True)
  31. begin = begin+1
  32. # ext
  33. exts = dictConfig['ext']
  34. for e in exts:
  35. if e == s[begin:]:
  36. res.append(True)
  37. break
  38. if len(res) == 5:
  39. return True
  40. else:
  41. return False
  42. def filename_to_number(s,dictConfig):
  43. res = None
  44. # prefix
  45. prefix = dictConfig['prefix'][0]
  46. lenp =len(prefix)
  47. # digit
  48. nbDigit = int(dictConfig['numberDigit'])
  49. sDigit =s[lenp:lenp+nbDigit]
  50. if sDigit.isdigit():
  51. res = int(sDigit)
  52. return res
  53. def number_to_filenames(num,dictConfig):
  54. res = []
  55. # prefix
  56. prefixes = dictConfig['prefix']
  57. for p in prefixes:
  58. res.append(dictConfig['path']+p)
  59. # digit and expo
  60. nbDigit = int(dictConfig['numberDigit'])
  61. expo = dictConfig['expo']
  62. newres = []
  63. for f in res:
  64. for e in expo:
  65. expoNum = int(e)
  66. sDigit =toStringDigit(num+expoNum, nbDigit)
  67. newres.append(f+sDigit)
  68. res = newres
  69. # suffix
  70. suffixes = dictConfig['suffix']
  71. newres = []
  72. for f in res:
  73. for su in suffixes: newres.append(f+su)
  74. res = newres
  75. # dot
  76. newres = []
  77. for f in res: newres.append(f+'.')
  78. res= newres
  79. # ext
  80. exts = dictConfig['ext']
  81. newres = []
  82. for f in res:
  83. for e in exts: newres.append(f+e)
  84. res = newres
  85. return res
  86. def img_url_2_file_ext(imagePath,url,minRange=-3,maxRange=-1):
  87. dat = url.split('/')[minRange:maxRange] # split url file name (/) and keep the part between minRange and maxRange
  88. retStr = url.split("/")[-1].split(".") # last element id the image file name
  89. retStr = retStr[0:-1]
  90. file = '.'.join(retStr)
  91. ext = url.split("/")[-1].split(".")[-1] # image extension
  92. retStr = dat+[file]
  93. retStr = '_'.join(retStr)
  94. retStr = imagePath+"/"+retStr
  95. return retStr,ext