1.我才知道a=input()读入的竟然是字符串。。。还要强制转化成int型,这玩意搞了我好久
2.对照片进行裁剪,将照片周围多余的白色部分去除(也就是让图像尽可能撑满整个框)
由上一张裁成下一张
from PIL import Image
import os
SCALE = 1
#等比例缩放
def get_char(pixel, blank_char='0', fill_char='1'):
return blank_char if pixel == 0 else fill_char
def shang():
for i in range(width):
for j in range(height):
if im.getpixel((i, j)) == 0:
return i
def xia():
for i in reversed(range(width)):
for j in range(height):
if im.getpixel((i,j)) == 0:
return i
def zuo():
for i in range(height):
for j in range(width):
if im.getpixel((j, i)) == 0:
return i
def you():
for i in reversed(range(height)):
for j in range(width):
if im.getpixel((j, i)) == 0:
return i
im = Image.open(r"C:\Users\DELL\Desktop\活动\离散大创\新建文件夹\gou.jpg")
size = im.size
#size[0]*size[1] 横宽像素
width, height = int(size[0] * SCALE), int(size[1] * SCALE)
im = im.resize((width, height))
im = im.convert('1')
#print(width,height)
s=shang()
x=xia()
z=zuo()
y=you()
box=(s,z,x,y)
#s z x y
im = im.crop(box)
im.save("C:\\Users\\DELL\\Desktop\\活动\\离散大创\\图像库\\狗.png")
#800 645
#txt = ""
#im1 = Image.open(r"C:\Users\DELL\Desktop\活动\离散大创\新建文件夹\apple.png")
#print(txt)
#f = open(r'gou.txt', 'w')
#print(txt, file=f)
#f.close()
其中的四个函数分别是定位上下左右的坐标,
im.paste(image,box)
将一张图粘贴到另一张图像上。变量box或者是一个给定左上角的2元组,或者是定义了左,上,右和下像素坐标的4元组,或者为空(与(0,0)一样)。如果给定4元组,被粘贴的图像的尺寸必须与区域尺寸一样。
im.crop()函数的参数形式是(x1, y1, x2, y2)这个元组是作为一个参数的
3.`
用以输出路径下的子目录
file_dir = r"C:\Users\DELL\Desktop\活动\离散大创\测试"
for root, dirs, files in os.walk(file_dir):
print(files) #当前路径下所有子目录
打开指定文件夹下的指定文件
img_path = 'C:\\Users\DELL\Desktop\活动\离散大创\图像库\\'
img_list = os.listdir(r'C:\Users\DELL\Desktop\活动\离散大创\图像库')
Ym = Image.open(img_path + img_list[i])
图像识别功能基本上已经实现,但是准确率等有待提高
而且使用优化方面尚且做的不足
from PIL import Image
import os
SCALE = 800#对比参数
#等比例缩放
SUM=6#数据库中数量
def get_char(pixel, blank_char='0', fill_char='1'):
return blank_char if pixel == 0 else fill_char
def shang(width,height):
for i in range(width):
for j in range(height):
if im.getpixel((i, j)) == 0:
return i
def xia(width,height):
for i in reversed(range(width)):
for j in range(height):
if im.getpixel((i,j)) == 0:
return i
def zuo(width,height):
for i in range(height):
for j in range(width):
if im.getpixel((j, i)) == 0:
return i
def you(width,height):
for i in reversed(range(height)):
for j in range(width):
if im.getpixel((j, i)) == 0:
return i
def cmp():
sum=0
for i in range(SCALE):
for j in range(SCALE):
if ng.getpixel((i, j))==Ym.getpixel((i, j))==0:
sum=sum+1
return sum
file_dir = r"C:\Users\DELL\Desktop\活动\离散大创\测试"
for root, dirs, files in os.walk(file_dir):
print(files) #当前路径下所有子目录
img_path = 'C:\\Users\DELL\Desktop\活动\离散大创\测试\\'
img_list = os.listdir(r'C:\Users\DELL\Desktop\活动\离散大创\测试')
a=input("您要测试的照片编号:(从左到右编号从1依次增大)")
a=int(a)-1
im = Image.open(img_path + img_list[a])#读入你要测试的图片编号
size = im.size
#im.show()
#size[0]*size[1] 横宽像素
width, height = int(size[0]), int(size[1])
im = im.resize((width, height))
im = im.convert('1')
#print(width,height)
s=shang(width,height)
x=xia(width,height)
z=zuo(width,height)
y=you(width,height)
box=(s,z,x,y)
#s z x y
ng = im.crop(box)
#ng.save("C:\\Users\\DELL\\Desktop\\活动\\离散大创\\测试\\chuan1.png")
#ng.show()
pos=-1
MAXN=-1
for i in range(SUM):
img_path = 'C:\\Users\DELL\Desktop\活动\离散大创\图像库\\'
img_list = os.listdir(r'C:\Users\DELL\Desktop\活动\离散大创\图像库')
Ym = Image.open(img_path + img_list[i])
Ym.show()
ng = ng.resize((SCALE,SCALE))
Ym = Ym.resize((SCALE,SCALE))
print("相似度为=",cmp())
if i==0 or MAXN<cmp():#保存相似度最大的数据
pos=i
MAXN=cmp()
filedir = r"C:\Users\DELL\Desktop\活动\离散大创\图像库"
for root, dirs, files in os.walk(filedir):
print("相似度最高的是第",pos+1,"个,为数据库中的图片",files[pos])
#800 645
#txt = ""
#im1 = Image.open(r"C:\Users\DELL\Desktop\活动\离散大创\新建文件夹\apple.png")
#print(txt)
#f = open(r'gou.txt', 'w')
#print(txt, file=f)
#f.close()
#561 850 558 472 923 650
#253 372 252 257 85 259
# 417 365 621 476
测试结果