Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import os
  2. from PIL import Image, ImageOps
  3. import numpy as np
  4. import csv
  5. from scipy.misc import imsave
  6.  
  7. input_folder = './input/ე' #needs to be filled
  8.  
  9. def cutImg(path, rotate, size):
  10. im = Image.open(path)
  11. rgb_im = im.convert('RGB')
  12. arr = np.array(rgb_im)
  13.  
  14. startTop = 0
  15. endBot = arr.shape[1]
  16. letterFound = False
  17.  
  18. for i in range(arr.shape[0]):
  19. isLetter = False
  20. for j in range(arr.shape[1]):
  21. if arr[i][j][0] <= 210 and arr[i][j][1] <= 210 and arr[i][j][2] <= 210:
  22. isLetter = True
  23. break
  24.  
  25. if isLetter and not letterFound:
  26. letterFound = True
  27. startTop = i
  28.  
  29. if not isLetter and letterFound:
  30. letterFound = False
  31. endBot = i
  32.  
  33. letterFound = isLetter
  34.  
  35. cropped = rgb_im.crop((0, startTop, arr.shape[1], endBot))
  36.  
  37. if rotate == 1:
  38. arr1 = np.array(cropped)
  39. arr1 = np.rot90(arr1, axes=(0, -2))
  40. cropped = Image.fromarray(arr1)
  41. elif rotate == -1:
  42. arr1 = np.array(cropped)
  43. arr1 = np.rot90(arr1, axes=(-2, 0))
  44. cropped = Image.fromarray(arr1)
  45.  
  46. if size != 0:
  47. cropped = cropped.resize((size, size), resample=Image.BILINEAR)
  48.  
  49. cropped.save(path)
  50.  
  51.  
  52. def cropImg(path):
  53. cutImg(path, 1, 0)
  54. cutImg(path, -1, 64)
  55.  
  56. def add_border(input_image, border, color=0):
  57. img = Image.open(input_image)
  58. if isinstance(border, int) or isinstance(border, tuple):
  59. bimg = ImageOps.expand(img, border=border, fill=color)
  60. else:
  61. raise RuntimeError('Border is not an integer or tuple!')
  62. return bimg
  63.  
  64. def binarize_array(numpy_array, threshold=180):
  65. for i in range(len(numpy_array)):
  66. for j in range(len(numpy_array[0])):
  67. if numpy_array[i][j] > threshold:
  68. numpy_array[i][j] = 255
  69. else:
  70. numpy_array[i][j] = 0
  71. return numpy_array
  72.  
  73. def convert_bw(img):
  74. image = img.convert('L') # convert image to monochrome
  75. image_arr = np.array(image)
  76. image_arr = binarize_array(image_arr)
  77. return image_arr
  78.  
  79. def format_picture(image_path):
  80. img_borders = add_border(image_path, border=10, color='white')
  81.  
  82. img_convert = img_borders.convert("RGBA")
  83. fff = Image.new('RGBA', img_convert.size, (255,)*4)
  84. out = Image.composite(img_convert, fff, img_convert)
  85. out = out.convert(img_borders.mode)
  86.  
  87. img_bw = convert_bw(out)
  88. img_name = "{}.{}".format('test', 'jpg')
  89. save_path = "{}/{}".format(input_folder, img_name)
  90.  
  91. imsave(save_path, img_bw)
  92. img = Image.open(save_path)
  93. img = img.resize((40, 40), resample=Image.BILINEAR)
  94. img.save(save_path)
  95.  
  96. imsave("{}".format(save_path), img)
  97. cropImg("{}".format(save_path))
  98. image = Image.open(save_path)
  99. image = image.resize((40,40), resample = Image.BILINEAR)
  100.  
  101. return convert_bw(image)
  102.  
  103. def generate_ans(image):
  104. arr = np.asarray(image)
  105. return arr
  106.  
  107. if __name__ == "__main__":
  108. image_files = os.listdir(input_folder)
  109. result = []
  110. for index in range(len(image_files)):
  111. image_array = format_picture('{}/{}'.format(input_folder, image_files[index]))
  112. image_array = image_array/255
  113. print(image_array.shape)
  114. ans = generate_ans(image_array)
  115. result += [[str(index), 'a']]
  116. break
  117.  
  118. filename = 'result.csv'
  119. with open(filename, 'w') as f:
  120. csv.writer(f).writerows(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement