Python-数字图像检测

Python-数字图像检测,python,image,python-imaging-library,Python,Image,Python Imaging Library,我希望第一张照片像第二张一样剪。 图像的所有维度都有可能吗?它必须适用于每种图像大小,而且我们不知道第一个像素黑色在哪里?它用于projet机器学习。我们需要的机器学习数字识别图像切割 from PIL import Image import numpy as np import matplotlib.pyplot as plt #couper l'image (entre le 1e et le dernier pixel) def cuttingImage(image): # o

我希望第一张照片像第二张一样剪。 图像的所有维度都有可能吗?它必须适用于每种图像大小,而且我们不知道第一个像素黑色在哪里?它用于projet机器学习。我们需要的机器学习数字识别图像切割

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

#couper l'image (entre le 1e et le dernier pixel)
def cuttingImage(image):

    # ouverture d’une image entant que noir/blanc:
    imageSource=Image.open(image).convert('L')

    #dimension de l'image
    width, height = imageSource.size
    #test
    #print(self.width,self.height)
    #prendre tous les pixels d'une image dans un array
    #diviser par 255 pour avoir 0 ou 1 comme valeur des pixels
    pixels=np.asarray(imageSource,dtype=np.float32)/255
    print("matriz de l'image",image)
    print(pixels)

    #créer un variable imageArray pour le tableau des pixels
    #plus lisible
    imageArray=pixels

    #longeur maximum
    maxWidth=width-1

    #une valeur maximal entre les dimensions de l'image diviser par 20
    #utilser pour couper l'image
    space=max(width//20,height//20)

    #rechercher le premier pixel
    for top, row in enumerate(imageArray):
        for left, pix in enumerate(row):
            if pix>=0.7:
               break

    #recherche le dernier pixel
    for down, row in enumerate(reversed(imageArray)):
        for right, pix in enumerate(reversed(row)):
            if maxWidth - right == left:
              # Image impossible
              break        
            elif pix>=0.7:
                break
    #longeur de l'image
    crop_width = abs(right - left) 
    # hauteur de l'image
    crop_height = abs(down - top) 
    #premier pixel sur l'axe des abscisses
    crop_center_x = int(left + crop_width/2)
    #premier pixel sur l'axe de l'ordonné
    crop_center_y = int(top + crop_height/2) 

    #tester
    #print(space)
    #definier les dimensions du nouveau image
    if(width<height):
        if(space>=50 and space<60):
            box=(((width-crop_center_x//2)//2,height-crop_center_y+3*space,crop_width-1.5*space,height+crop_height-3*space))
        elif(space<200):
            box=((-space+(width-crop_center_x//2)//2,height-crop_center_y+space,crop_width-space,height+crop_height-space))
        else:
            box=((-space+(width-crop_center_x//2)//2,height-crop_center_y+8*space,crop_width-1.5*space,height+crop_height-8*space))
    else:
        box=((-2*space+width-crop_center_x//2,height-crop_center_y+2*space,crop_width-3.5*space,height+crop_height-2*space))


    #Couper l'image avec l'aide de variable box
    imageSource=imageSource.crop(box)

    imageSource.save("ImageNorm_img/new-test.jpg")

    #fermer l'image
    imageSource.close()
从PIL导入图像
将numpy作为np导入
将matplotlib.pyplot作为plt导入
#Coupper l’image(中间像素)
def切割图像(图像):
#黑/白相间的欧维图尔酒店
imageSource=Image.open(Image.convert('L'))
#意象维度
宽度,高度=imageSource.size
#试验
#打印(自宽、自高)
#prendre tous les像素数据图像数据阵列
第255号倒数第0个欧式1个像素
像素=np.asarray(imageSource,dtype=np.float32)/255
打印(“图像矩阵”,图像)
打印(像素)
#créer非可变图像阵列像素表
#加号lisible
图像阵列=像素
#最大长欧元
最大宽度=宽度-1
第20类图像分割器的最大中心尺寸
#utilser pour Coupper l'image
空间=最大值(宽度//20,高度//20)
#勒普莱米尔像素酒店
对于顶部,枚举(imageArray)中的行:
对于左侧,枚举中的pix(行):
如果pix>=0.7:
打破
#勒德尼尔像素
对于down,枚举中的行(反转(imageArray)):
对于右侧,枚举中的pix(反向(行)):
如果maxWidth-right==left:
#图像不可能
打破
elif pix>=0.7:
打破
#朗格尔德勒形象酒店
裁剪宽度=绝对值(右-左)
#傲慢的形象
裁剪高度=abs(向下-顶部)
#横轴上的premier pixel
裁剪中心=整数(左+裁剪宽度/2)
#奥多涅大道上的总理像素酒店
裁剪中心y=int(顶部+裁剪高度/2)
#测试员
#打印(空格)
#新形象的定义

如果(宽度=50,空间我想你就是想要这样的东西

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Load image, and make into Numpy array
im = Image.open('text.jpg')
na = np.array(im.convert('L'))

# Stretch the contrast to range 0..255 to maximize chances of splitting digits from background
na = ((na.astype(np.float)-na.min())*255.0/(na.max()-na.min())).astype(np.uint8)

# Threshold image to pure black and white
blk = np.array([0],  np.uint8)
wht = np.array([255],np.uint8)
thr = np.where(na>128, blk, wht)

# Go back to PIL Image from Numpy array
res = Image.fromarray(thr)

# Get bounding box from thresholded image
bbox = res.getbbox()
print('Bounding box:',bbox)

# Apply bounding box to original image and save
result = im.crop(bbox)
result.save('result.png')

我会使用Pytesseract