2017-10-22 19 views
1

문제가 있습니다. 그래서이미지의 한 줄에 개체를 만듭니다.

,이 이미지를 가지고 있고, 나는 한 줄에 모든 숫자를 넣어 원하는 : enter image description here

하지만에만이있어 : enter image description here

을 당신이 볼 수 있듯이,이 1이면 , 0, 7 - 모든 괜찮지 만, 4, 3 ...

내 코드 :

def reshape_img(self): 
    width, height = self.img.size 
    new_img_list = [] 
    for x in range(width): 
     white_y = 0 
     start_nr = False 
     for y in range(height): 
      red, green, blue = self.img.getpixel((x, y)) # Current color 
      if red != 255: 
       start_nr = True 
       new_y = y - white_y + 5 
       new_img_list.append((x, new_y, (red, green, blue))) 
      elif red == 255 and not start_nr: 
       white_y += 1 
    return new_img_list 

def new_image(image_list): 
    background = (255, 255, 255, 255) 
    img = Image.new('RGB', (545, 20), background) 
    pixels = img.load() 
    for d in image_list: 
     pixels[d[0], d[1]] = d[2] 
    img.save('img2.png') 
+1

단지 '4'로 예제를 만들 수 있습니까? [mcve]를 만드는 방법을 참조하십시오. –

답변

1

대신 그 열의 첫 번째 비 - 백색 픽셀에 기초하여 각 열의 픽셀을 조정하고, 특정 범위 (전체 자릿수를 커버하기에 충분)에 인접한 열을보고 이들 모두의 최소값을 취하십시오. 이렇게하면 자릿수가 블록으로 위 아래로 움직이며 다른 열을 이동하면서 다른 값으로 왜곡되지 않습니다. 당신은 최소를 저장하는 목록을 사용하여, 패스의 몇을 수행 할 수 있습니다

def reshape_img(self): 
    width, height = self.img.size 
    y_start = [height] * width 

    # find the first non-white pixel of each column (checking only red channel): 
    for x in range(width): 
     for y in range(height): 
      red, green, blue = self.img.getpixel((x, y)) # Current color 
      if red != 255: 
       y_start[x] = y 
       break 

    new_img_list = [] 
    for x in range(width): 
     # find minimum of adjacent columns +/- 5 left and right: 
     white_y = min(y_start[min(0,x-5):max(width-1:x+5)]) 
     for y in range(white_y, height): 
      red, green, blue = self.img.getpixel((x, y)) # Current color 
      if red != 255: 
       new_y = y - white_y + 5 
       new_img_list.append((x, new_y, (red, green, blue))) 
    return new_img_list 

(테스트되지 않은 코드)

이 알고리즘은 이미지에 있기 때문에 (숫자 사이가되는 공간에 의존) 변경 한 경우 숫자의 크기와 간격을 기준으로 인접한 열의 수를 조정해야합니다. 연속되지 않는 연속되지 않은 열의 블록 만 가져 와서 더 견고하게 만들 수 있습니다. 따라서 모든 흰색 열이있는 경우 한쪽의 열은 다른 열의 열에 영향을 줄 수 없습니다.