Skip to content

Instantly share code, notes, and snippets.

@yunsu3042
Created November 30, 2016 17:27
Show Gist options
  • Save yunsu3042/fb797204f8aecb0b29afd40033edfa64 to your computer and use it in GitHub Desktop.
Save yunsu3042/fb797204f8aecb0b29afd40033edfa64 to your computer and use it in GitHub Desktop.
# 이미지 내의 한줄로 구성된 문자들이 있을 경우 문자들을 구분해주는 흰 여백의 x좌표를 list로 반환한다.
def x_cordinate(url):
arr = image_into_array(url,(1000,1000))
img = Image.open(url)
xcrop = [0]
satisfied = 0
width,height = img.size
for y in range(width):
for x in range(height):
if arr[x][y] == 1:
satisfied = 1
if satisfied ==1 and 1 not in [arr[x][y] for x in range(height)]:
xcrop.append(y)
satisfied = 0
return xcrop
# 이미지 객체를 받아 한줄로 구성된 문자들이 있을 경우 문자들을 구분해주는 흰 여백의 x좌표를 list로 반환한다.
def x_cordinate_img(img):
xcrop = [0]
satisfied = 0
width,height = img.size
for y in range(width):
for x in range(height):
if arr[x][y] == 1:
satisfied = 1
if satisfied ==1 and 1 not in [arr[x][y] for x in range(height)]:
xcrop.append(y)
satisfied = 0
return xcrop
# 이미지 내의 한줄로 구성된 문자들이 있을 경우 문자들을 구분해서 각각 저장할 수 있으며, 리스트로 각 알파뱃을 저장해서 반환한다.
def x_crop(url):
cropped = []
img = Image.open(url)
width,height = img.size
cropby = x_cordinate(url)
for i in range(len(cropby)-1):
temp = img.crop((cropby[i],0,cropby[i+1],height))
# temp.save("/Users/yunsu/Desktop/python_ocr/phonts/phont3/v_split/h_split/{}.jpg".format(i+12))
cropped.append(temp)
return cropped
## 파일 폴더를 받아서 그 폴더 하위에 있는 파일들의 파일명을 filenames에 저장한다. for 구문을 돌면서 filename을 파일명을 텍스트와 확장자로 구분한후,
## 텍스트(ex "A01")을 숫자 부분만 [1:]로 가져온후 int화 한후 배열로 정렬해서 반환한다.
def search(dirname):
import os
splitnames = []
filenames = os.listdir(dirname)
for filename in filenames:
splitnames.append(int(os.path.splitext(filename)[1:]))
return sorted(splitnames)
# url을 받아, y_crop을 통해 이미지에 있는 글로 이루어진 단락을 각각의 줄들로 분리한다.
# x_cordinate_img 함수를 사용해, 각 줄에 대해 알파뱃들 사이를 채우고 있는 흰 여백의 x 축 좌표를 구한다.(함수이름에 신경써야한다.)
# crop한 알파뱃을 저장하고 싶은 위치를 설정한다. 여기서는 (/Users/yunsu/Destop/python_ocr/<alphabet>)
# temp.show()가 이미지를 보여주면, 사용자는 알파뱃을 대소문자를 구분해서 값을 입력하면, 입력받은 알파뱃을 폴더명이 없다면 생성해준다.
# 알파뱃 이미지를 폴더 내에 저장할 때는, 나중에 다른 알파뱃 이미지들과 섞였을 때 구분할 수 있도록, 알파뱃과 숫자값을 결합한 "A11"의 형식으로 저장한다.
# 숫자를 차곡차곡 지정하기 위해서 폴더내에 이미 존재했던 파일들을 search함수를 통해 가장 높은 숫자값을 구한후 그 값에 1을 더한 값을 숫자값으로 저장한다.
# 원하는 경로를
def crop_alphabet(url):
import os
lines = y_crop(url)
for img in lines:
width,height = img.size
cropped = []
cropby = x_cordinate_img(img)
for i in range(len(cropby)-1):
temp = img.crop((cropby[i],0,cropby[i+1],height))
temp.show()
alphabet = input("눈에 보이는 알파뱃을 대소문자 가려서 입력해주세요")
# 원하는 경로를 디렉토리에
directory = "/Users/yunsu/Desktop/python_ocr/alphabet/{}".format(alphabet)
if not os.path.exists(directory):
os.makedirs(directory)
try:
num = search(directory)[-1]
#디렉토리에 적었던 경로를 조금만 수정해서 적어주세요.
temp.save("/Users/yunsu/Desktop/python_ocr/alphabet/{alphabet_folder}/{alphabet}{number}.jpg".format(
alphabet_folder= alphabet,alphabet=alphabet,number=num+1))
except:
#디렉토리에 적었던 경로를 조금만 수정해서 적어주세요.
temp.save("/Users/yunsu/Desktop/python_ocr/alphabet/{alphabet_folder}/{alphabet}{number}.jpg".format(
alphabet_folder= alphabet,alphabet=alphabet,number=0))
cropped.append(temp)
return cropped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment