img_util.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import hashlib
  5. import cStringIO
  6. from PIL import Image
  7. WHITE = (255, 255, 255)
  8. BLACK = (0, 0, 0)
  9. IMG_MODEL_FOLDER = os.path.join(os.path.dirname(__file__), 'img_model')
  10. img_value_dict = dict()
  11. def get_num(img_data, index_skip_info):
  12. f = cStringIO.StringIO(img_data)
  13. img = Image.open(f)
  14. width, height = img.size
  15. counter = 0
  16. last_width = 0
  17. for skip_w, skip_x in index_skip_info:
  18. counter += 1
  19. skip_w = int(skip_w)
  20. skip_x = int(skip_x)
  21. box = (skip_x, 0, skip_x + skip_w, height)
  22. new_img = img.crop(box)
  23. if counter == 1:
  24. end_img = Image.new('RGB', (100, new_img.size[1]))
  25. end_img.paste(new_img, (last_width, 0))
  26. last_width += new_img.size[0]
  27. return get_value_from_img(img=end_img)
  28. def get_value_from_img(fp=None, img=None):
  29. if not fp and not img:
  30. raise Exception('param error')
  31. if not img and fp:
  32. img = Image.open(fp)
  33. img = img.convert('RGB')
  34. img_data = img.load()
  35. img_width, img_height = img.size
  36. for x in range(img_width):
  37. for y in range(img_height):
  38. if img_data[x, y] != WHITE:
  39. img_data[x, y] = WHITE
  40. else:
  41. img_data[x, y] = BLACK
  42. small_imgs = split_img(img, img_data, img_width, img_height)
  43. return get_value_from_small_imgs(small_imgs)
  44. def get_value_from_small_imgs(small_imgs):
  45. global img_value_dict
  46. value = []
  47. for img in small_imgs:
  48. key = get_md5(img)
  49. value.append(img_value_dict[key])
  50. return "".join(value)
  51. def split_img(img, img_data, img_width, img_height):
  52. imgs = []
  53. split_info = []
  54. left = right = top = bottom = 0
  55. y_set = set()
  56. for x in range(img_width):
  57. all_is_white = True
  58. for y in range(img_height):
  59. if img_data[x, y] == WHITE:
  60. continue
  61. all_is_white = False
  62. if not left:
  63. left = x
  64. y_set.add(y)
  65. if all_is_white and left and not right:
  66. right = x
  67. top = min(y_set)
  68. bottom = max(y_set)
  69. split_info.append((left, right, top, bottom))
  70. left = right = top = bottom = 0
  71. y_set = set()
  72. for left, right, top, bottom in split_info:
  73. box = (left, top - 1, right, bottom + 1)
  74. new_img = img.crop(box)
  75. imgs.append(new_img)
  76. return imgs
  77. def get_md5(img):
  78. content_list = []
  79. img = img.convert('RGB')
  80. img_data = img.load()
  81. img_width, img_height = img.size
  82. for x in range(img_width):
  83. for y in range(img_height):
  84. content = 'x:{0},y:{1},{2}'.format(x, y, img_data[x, y])
  85. content_list.append(content)
  86. return hashlib.md5("".join(content_list)).hexdigest()
  87. def _load_imgs():
  88. global img_value_dict
  89. file_name_list = os.listdir(IMG_MODEL_FOLDER)
  90. for file_name in file_name_list:
  91. value = file_name.split('.')[0]
  92. file_path = os.path.join(IMG_MODEL_FOLDER, file_name)
  93. img = Image.open(file_path)
  94. key = get_md5(img)
  95. img_value_dict[key] = value
  96. _load_imgs()