Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from PIL import Image, ImageOps
- import numpy as np
- import csv
- from scipy.misc import imsave
- input_folder = './input/ე' #needs to be filled
- def cutImg(path, rotate, size):
- im = Image.open(path)
- rgb_im = im.convert('RGB')
- arr = np.array(rgb_im)
- startTop = 0
- endBot = arr.shape[1]
- letterFound = False
- for i in range(arr.shape[0]):
- isLetter = False
- for j in range(arr.shape[1]):
- if arr[i][j][0] <= 210 and arr[i][j][1] <= 210 and arr[i][j][2] <= 210:
- isLetter = True
- break
- if isLetter and not letterFound:
- letterFound = True
- startTop = i
- if not isLetter and letterFound:
- letterFound = False
- endBot = i
- letterFound = isLetter
- cropped = rgb_im.crop((0, startTop, arr.shape[1], endBot))
- if rotate == 1:
- arr1 = np.array(cropped)
- arr1 = np.rot90(arr1, axes=(0, -2))
- cropped = Image.fromarray(arr1)
- elif rotate == -1:
- arr1 = np.array(cropped)
- arr1 = np.rot90(arr1, axes=(-2, 0))
- cropped = Image.fromarray(arr1)
- if size != 0:
- cropped = cropped.resize((size, size), resample=Image.BILINEAR)
- cropped.save(path)
- def cropImg(path):
- cutImg(path, 1, 0)
- cutImg(path, -1, 64)
- def add_border(input_image, border, color=0):
- img = Image.open(input_image)
- if isinstance(border, int) or isinstance(border, tuple):
- bimg = ImageOps.expand(img, border=border, fill=color)
- else:
- raise RuntimeError('Border is not an integer or tuple!')
- return bimg
- def binarize_array(numpy_array, threshold=180):
- for i in range(len(numpy_array)):
- for j in range(len(numpy_array[0])):
- if numpy_array[i][j] > threshold:
- numpy_array[i][j] = 255
- else:
- numpy_array[i][j] = 0
- return numpy_array
- def convert_bw(img):
- image = img.convert('L') # convert image to monochrome
- image_arr = np.array(image)
- image_arr = binarize_array(image_arr)
- return image_arr
- def format_picture(image_path):
- img_borders = add_border(image_path, border=10, color='white')
- img_convert = img_borders.convert("RGBA")
- fff = Image.new('RGBA', img_convert.size, (255,)*4)
- out = Image.composite(img_convert, fff, img_convert)
- out = out.convert(img_borders.mode)
- img_bw = convert_bw(out)
- img_name = "{}.{}".format('test', 'jpg')
- save_path = "{}/{}".format(input_folder, img_name)
- imsave(save_path, img_bw)
- img = Image.open(save_path)
- img = img.resize((40, 40), resample=Image.BILINEAR)
- img.save(save_path)
- imsave("{}".format(save_path), img)
- cropImg("{}".format(save_path))
- image = Image.open(save_path)
- image = image.resize((40,40), resample = Image.BILINEAR)
- return convert_bw(image)
- def generate_ans(image):
- arr = np.asarray(image)
- return arr
- if __name__ == "__main__":
- image_files = os.listdir(input_folder)
- result = []
- for index in range(len(image_files)):
- image_array = format_picture('{}/{}'.format(input_folder, image_files[index]))
- image_array = image_array/255
- print(image_array.shape)
- ans = generate_ans(image_array)
- result += [[str(index), 'a']]
- break
- filename = 'result.csv'
- with open(filename, 'w') as f:
- csv.writer(f).writerows(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement