【问题标题】:drawing box around image where there are black pixels in python在python中有黑色像素的图像周围绘制框
【发布时间】:2019-12-24 00:17:28
【问题描述】:

我想在有黑色像素的图像周围画一个框。

这是我的图片:(quiz.JPG):

我希望图像是这样的:

但是通过下面的代码我得到了这个(quiz2.JPG):

这是我尝试过的:

from PIL import Image,ImageDraw

image = Image.open("quiz.JPG").convert('L') #open and convert to black and white

pixels = image.load()
width, height = image.size 
black_pixels= []
for col in range(width):
    for row in range(height):
        pixel = pixels[col, row]
        if pixel == (0): #select pixels that are black
            black_pixels.append((col, row)) #add them to the list of black pixels

def sortCoordinates(lst): #this is just a bubble sort to get the coordinates in order of y values
    length = len(lst) -1
    solved = False

    while not solved:
        solved = True
        for i in range(length):
            if lst[i][1] > lst[i+1][1]:
                solved = False
                lst[i],lst[i+1] = lst[i+1],lst[i]
    return lst

black_pixels = sortCoordinates(black_pixels)

def getShape(black_pixels): # function to generate box from the pixels given
    firstPixelXValue = black_pixels[0][0]
    lastPixelXValue = black_pixels[-1][0]

    tallestPixel = 0
    for pixel in black_pixels:
        YValue = pixel[1]

        if YValue > tallestPixel:
            tallestPixel = YValue
    topMostPixelYValue = tallestPixel


    shortestPixel = pixel[1]
    for pixel in black_pixels:
        YValue = pixel[1]

        if YValue < shortestPixel:
            shortestPixel = YValue
    bottomMostPixelYValue = shortestPixel

    shape = [(firstPixelXValue-4, topMostPixelYValue+2), (lastPixelXValue+4,bottomMostPixelYValue-3)] 
    return shape

questions = {}

count = 0
count2 = 0
currentQuestionPixels = []
oldPixelY = black_pixels[0][1]
for newPixel in black_pixels:
    newPixelY = newPixel[1]
    if newPixelY > oldPixelY + 100 or len(black_pixels)-1 == count2: #run this if new question is found
        questions[str(count)] = getShape(currentQuestionPixels)
        currentQuestionPixels = []
        count += 1
    oldPixelY = newPixelY
    currentQuestionPixels.append(newPixel)
    count2 += 1

image = image.convert('RGB')
for question in questions: #draw shape around all questions found
    shape = questions.get(question)
    img1 = ImageDraw.Draw(image)   
    img1.rectangle(shape, outline ="red") 

image.save("quiz2.JPG") 

理论上这应该可行,因为我正在获取所有黑色像素坐标并将它们分类为它们的 Y 值。然后我循环遍历黑色像素,如果新像素 Y 值大于旧像素 Y 值 + 100,或者它是最后一个像素,则它必须是一个新问题。 所以我在所有问题周围画了一个框

【问题讨论】:

    标签: python image


    【解决方案1】:

    我修改了 getShape 函数 看看下面的代码

    from PIL import Image,ImageDraw
    
    image = Image.open("quiz.JPG").convert('L') #open and convert to black and white
    
    pixels = image.load()
    width, height = image.size 
    black_pixels= []
    for col in range(width):
        for row in range(height):
            pixel = pixels[col, row]
            if pixel == (0): #select pixels that are black
                black_pixels.append((col, row)) #add them to the list of black pixels
    
    def sortCoordinates(lst): #this is just a bubble sort to get the coordinates in order of y values
        length = len(lst) -1
        solved = False
    
        while not solved:
            solved = True
            for i in range(length):
                if lst[i][1] > lst[i+1][1]:
                    solved = False
                    lst[i],lst[i+1] = lst[i+1],lst[i]
        return lst
    
    black_pixels = sortCoordinates(black_pixels)
    
    def getShape(black_pixels): # function to generate box from the pixels given
        first_x = 0
        first_y = 0
        last_x = 99999999
        last_y = 99999999
    
        for x, y in black_pixels:
    
            if first_x < x :
                first_x = x
            if first_y < y :
                first_y = y
            if last_x > x :
                last_x = x
            if last_y > y :
                last_y = y
    
        shape = [(first_x-4, first_y+2), (last_x+4,last_y-3)]
        return shape
    
    questions = {}
    
    count = 0
    count2 = 0
    currentQuestionPixels = []
    oldPixelY = black_pixels[0][1]
    for newPixel in black_pixels:
        newPixelY = newPixel[1]
        if newPixelY > oldPixelY + 100 or len(black_pixels)-1 == count2: #run this if new question is found
            questions[str(count)] = getShape(currentQuestionPixels)
            currentQuestionPixels = []
            count += 1
        oldPixelY = newPixelY
        currentQuestionPixels.append(newPixel)
        # print(currentQuestionPixels)
        count2 += 1
    
    image = image.convert('RGB')
    for question in questions: #draw shape around all questions found
    
        shape = questions.get(question)
        print(shape)
        img1 = ImageDraw.Draw(image)   
        img1.rectangle(shape, outline ="red") 
    
    image.save("quiz2.JPG") 
    

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 2017-08-08
      • 2019-01-26
      • 1970-01-01
      • 2019-07-25
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      相关资源
      最近更新 更多