python 图片截取

2018/12 25 11:12

截取 二维码 去空白

 

 























































































































































































#conding=utf-8

from PIL import Image
import imageio,os
def downpiont(img):
    img_array = img.load()
    w=int(img.size[0] 
)
    h=int(img.size[1] 







)
    k=h
    print(img.size)
    if w <h:
        k=w
    n=""
    for i_t in range(k):
        i=k-i_t-1
        t_i=img_array[int(i), int(i)] 










        if len(t_i)==4:
            xx = sum(t_i)-255
        else:
            xx = sum(t_i)
        if xx < 100:
            print(i)
            n = i
            break
    for i_i_i in range(n,w):
        wx=[] 


        for i in range(h):
            t_i=img_array[i_i_i, int(i)] 















            if len(t_i)==4:
                xx = sum(t_i)-255
            else:
                xx = sum(t_i)
            if xx < 156:
                wx.append(1)
            else:
                wx.append(0)
        if (sum(wx)) == 0:
            w_t = i_i_i+1
            print("w:", i_i_i)
            break

    for i_i_i in range(n,h):
        wx = [] 


        for i in range(w):
            t_i=img_array[int(i), i_i_i] 



















            if len(t_i)==4:
                xx = sum(t_i)-255
            else:
                xx = sum(t_i)
            if xx < 156:
                wx.append(1)
            else:
                wx.append(0)
        if (sum(wx)) == 0:
            h_t = i_i_i+1
            print("h:", i_i_i)
            break
    return w_t, h_t


def uppiont(img):
    w_t, h_t=0,0
    img_array = img.load()
    w=int(img.size[0] 
)
    h=int(img.size[1] 






)
    k = h
    if w < h:
        k = w
    n=""
    for i_t in range(k):
        i=i_t
        t_x=img_array[int(i), int(i)] 












        if len(t_x)==4:
            xx = sum(t_x)-255
        else:
            xx = sum(t_x)
        if xx <100: #hei 0<255bai
            print(i)
            n=i
            break
    #shu
    for i_i in range(n):
        n_t1=int(n)-int(i_i)
        wx=[] 


        for i in range(h):
            t_x=img_array[n_t1, int(i)] 
















            if len(t_x) == 4:
                xx = sum(t_x) - 255
            else:
                xx = sum(t_x)
            if xx<156:
                wx.append(1)
            else:
                wx.append(0)
        if (sum(wx) )==0:
            w_t = n_t1-1
            print("w:", w_t)
            break
    #heng
    for i_i in range(n):
        n_t1 = int(n) - int(i_i)
        wx = [] 


        for i in range(w):
            t_x=img_array[int(i), n_t1] 















            if len(t_x) == 4:
                xx = sum(t_x) - 255
            else:
                xx = sum(t_x)
            if xx < 156:
                wx.append(1)
            else:
                wx.append(0)
        if (sum(wx)) == 0:
            h_t = n_t1-1
            print("h:", h_t)
            break
    return w_t,h_t

def qubian(files,p=[] 




):
    print(files)
    img=Image.open(files)
    x2,y2=downpiont(img)
    x1,y1=uppiont(img)
    x=[x1,y1,x2,y2] 















    # #

    # print(x1,y1,x2,y2)
    img_x=img.crop(x)#裁剪
    if len(p)>0:
        out = img_x.resize((p), Image.ANTIALIAS)  # 缩放
        return out
    return img_x
    #


def merge(templatefiles,img_c,savefile='./out.png'):
    template_img = Image.open(templatefiles)
    box=(311,1028,762,1479)
    region = img_c.resize((box[2]  - box[0] , box[3]  - box[1] 





))
    template_img.paste(region, box)
    template_img.save(savefile)  # 保存图片


def lsfile(path_i='./',x='none'):
    if path_i[-1] 






=="/":
        filenames = sorted(str(path_i)+str(fn) for fn in os.listdir(path_i))
        print(filenames)
        if 'none' not in x:
            filenames = sorted((str(path_i)+str(fn) for fn in os.listdir(path_i) if fn.endswith(x)))
        # filenames.sort(reverse=True)

        # filenames = [] 



        return filenames
    else:
        return [] 







def create_gif(filenames,name='gif.gif'):
    filenames=lsfile(filenames)
    images = [] 














    for filename in filenames:
        images.append(imageio.imread(filename))
    imageio.mimsave(name, images,duration=1)

# create_gif('./pic/','s.gif')


# img_c = qubian(r"./2.jpg")
# img_c = qubian(r"./pic/17.png")
# hecheng('./template.jpg',img_c,"ss.png")
# lsfile('./pic/')
#
for i in lsfile('./pic/'):
    x=qubian(i,[400,400] 

)

    x.save(i)  # 保存

--转载请注明: http://91o.cc/python-%e5%9b%be%e7%89%87%e6%88%aa%e5%8f%96/

发表回复

(必填)