pythonimaging
A. 如何安装python图型处理库Python Imaging Library
在Python中对图片的处理采用了PIL库,这个库可用于图片的一些常用操作,如改变尺寸、格式、色彩、旋转等处理。
首先要先安装Python(在这里不做介绍)。
安装完Python后,安装PIL库就非常简单了,只需按如下步骤进行
1、下载PIL的Source Kit(因为这个包支持全部平台) Imaging--1.1.6.tar.gz
2、解压缩包 tar -zxvf Imaging-1.1.6.tar.gz
3、进入到解压后的目录 cd Imaging-1.1.6
4、Build pakage: python setup.py build_ext -i
5、测试; python selftest.py
6、安装 python setup.py install
执行完上述操作后,可以直接在程序用使用 import Image进行使用PIL中的Image类
B. Python如何图像识别
首先,先定位好问题是属于图像识别任务中的哪一类,最好上传一张植物叶子的图片。因为目前基于深度学习的卷积神经网络(CNN)确实在图像识别任务中取得很好的效果,深度学习属于机器学习,其研究的范式,或者说处理图像的步骤大体上是一致的。
1、第一步,准备好数据集,这里是指,需要知道输入、输出(视任务而定,针对你这个问题,建议使用有监督模型)是什么。你可以准备一个文件夹,里面存放好植物叶子的图像,而每张图像对应一个标签(有病/没病,或者是多类别标签,可能具体到哪一种病)。
具体实现中,会将数据集分为三个:训练集(计算模型参数)、验证集(调参,这个经常可以不需要实现划分,在python中可以用scikit-learn中的函数解决。测试集用于验证模型的效果,与前面两个的区别是,模型使用训练集和验证集时,是同时使用了输入数据和标签,而在测试阶段,模型是用输入+模型参数,得到的预测与真实标签进行对比,进而评估效果。
2、确定图像识别的任务是什么?
图像识别的任务可以分为四个:图像分类、目标检测、语义分割、实例分割,有时候是几个任务的结合。
图像分类是指以图像为输入,输出对该图像内容分类的描述,可以是多分类问题,比如猫狗识别。通过足够的训练数据(猫和狗的照片-标签,当然现在也有一系列的方法可以做小样本训练,这是细节了,这里并不敞开讲),让计算机/模型输出这张图片是猫或者狗,及其概率。当然,如果你的训练数据还有其它动物,也是可以的,那就是图像多分类问题。
目标检测指将图像或者视频中的目标与不感兴趣的部分区分开,判断是否存在目标,并确定目标的具体位置。比如,想要确定这只狗所佩戴的眼睛的位置,输入一张图片,输出眼睛的位置(可视化后可以讲目标区域框出来)。
看到这里,应该想想植物叶子诊断疾病的问题,只需要输入一整张植物叶子的图片,输出是哪种疾病,还是需要先提取叶子上某些感兴趣区域(可能是病变区域),在用病变区域的特征,对应到具体的疾病?
语义分割是当今计算机视觉领域的关键问题之一,宏观上看,语义分割是一项高层次的任务。其目的是以一些原始图像作为输入,输出具有突出显示的感兴趣的掩膜,其实质上是实现了像素级分类。对于输入图片,输出其舌头区域(注意可以是不规则的,甚至不连续的)。
而实例分割,可以说是在语义分割的基础上,在像素层面给出属于每个实例的像素。
看到这里,可以具体思考下自己的问题是对应其中的哪一类问题,或者是需要几种任务的结合。
3、实际操作
可以先通过一个简单的例子入手,先了解构建这一个框架需要准备什么。手写数字识别可以说是深度学习的入门数据集,其任务也经常作为该领域入门的案例,也可以自己在网上寻找。
C. Python 3.3 PIL : The _imaging C mole is not installed
首先,检查一下你是否有_imaging模块。
在windows平台下看看有没有_imaging.pyd文件(有些情况为_imaging.dll),我的目录为C:\Python27\Lib\site-packages\PIL,在Unix下找个叫_imaging.so 或者_imagingmole.so
的文件,有些Unix的平台的扩展名可能为.sl。
以下方法用于检查目录:
打开命令提示符输入python -v ,再输入import Image
另外一种方法是import sys,然后print sys.path
最后,如果到此都行,在交互模式下输入import _imaging还提示
那你再检查一下,安装的PIL 是否跟你的电脑匹配,32位还是64位。重新安装
D. 执行这段脚本的时候 ./setup-seafile-mysql.sh 提示没有安装python-imaging 可是我真真的已经安装了
如果是这样的话就是你检测函数的问题啦。
你检测函数是不是检测了python-imaging的版本号。你安装的和你检测的不是同一个版本。
E. python有什么比较好的图像颜色相似度对比的库么
需要使用Python Imaging Library,下代是python2.x的代码:
from itertools import izip
import Image
i1 = Image.open("image1.jpg")
i2 = Image.open("image2.jpg")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
print "Difference (percentage):", (dif / 255.0 * 100) / ncomponents
F. 用Python的PIL模块的image模块打开的图片位于哪个文件夹
你所问的问题,其实是属于: 1.先参考 【教程】Python中的内置的模块 和第三方的模块 搞懂PIL是属于第三方Python模块 2.再参考: 【待完善】【总结】Python安装第三方的库、package的方法 去安装PIL模块。 3.关于PIL的一些使用,可以参考: 【已解决】Python中通过Image的open之后,去show结果打不开bmp图片,无法正常显示图片 再针对PIL来说就是: 1. 这里不给贴地址,所以只能靠你自己用google搜: python pil 第一个就是: Python Imaging Library (PIL) 点击进去后,找到自己python版本的PIL,比如: Python Imaging Library 1.1.7 for Python 2.7 (Windows only) 下载,双击,安装,即可。 2. 如果下载到的是PIL源码, 则打开cmd,切换到其目录 然后执行 setup.py install 就可以通过源码方式安装了。 这些方法,上面帖子其实都有总结的。
G. 使用Python 制作对比图片相似度的程序
import media
def red_average(pic):
'''Return an integer that represents the average red of the picture.
'''
total=0
for pixel in pic:
total = total + media.get_red(pixel)
red_average = total / (media.get_width(pic)*media.get_height(pic))
return red_average
def green_average(pic):
'''Return an integer that represents the average green of the picture
'''
total = 0
for pixel in pic:
total = total + media.get_green(pixel)
green_average = total / (media.get_width(pic)*media.get_height(pic))
return green_average
def blue_average(pic):
'''Return an integer that represents the average blue of the picture
'''
total = 0
for pixel in pic:
total = total + media.get_blue(pixel)
blue_average = total / (media.get_width(pic)*media.get_height(pic))
return blue_average
def scale_red(pic, value):
'''Return the picture that the average of the red is value which has been set.
'''
averaged = red_average(pic)
factor = float(value) / averaged
for pixel in pic:
new_red = min(255, int(factor * media.get_red(pixel)))
media.set_red(pixel,new_red)
return pic
def scale_green(pic, value):
'''Return the picture that the average of the green is value which has been set.
'''
averaged = green_average(pic)
factor = float(value) / averaged
for pixel in pic:
new_green = min(255, int(factor * media.get_green(pixel)))
media.set_green(pixel,new_green)
return pic
def scale_blue(pic, value):
'''Return the picture that the average of the blue is value which has been set.
'''
averaged = blue_average(pic)
factor = float(value) / averaged
for pixel in pic:
new_blue = min(255, int(factor * media.get_blue(pixel)))
media.set_blue(pixel,new_blue)
return pic
def expand_height(pic, factor):
'''Return a newpicture that has been vertically stretched by the factor which has been set.
'''
new_width = pic.get_width()
new_height = pic.get_height()*factor
newpic = media.create_pic(new_width, new_height, media.black)
for pixel in pic:
x = media.get_x(pixel)
y = media.get_y(pixel)
newpixel = media.get_pixel(newpic, x, y*factor)
for newpixel in newpic:
new_red = media.get_red(pixel)
new_green = media.get_green(pixel)
new_blue = media.get_blue(pixel)
media.set_red(newpixel,new_red)
media.set_green(newpixel,new_green)
media.set_blue(newpixel,new_blue)
return newpic
def expand_width(pic,factor):
'''Return a newpicture that has been horizontally stretched by the factor which has been set.
'''
new_width = pic.get_width() * factor
new_height = pic.get_height()
newpic = media.create_pic(new_width,new_height,media.black)
for newpixel in newpic:
x = media.get_x(newpixel)
y = media.get_y(newpixel)
pixel = media.get_pixel(pic,x / factor, y)
new_red = media.get_red(pixel)
new_green = media.get_green(pixel)
new_blue = media.get_blue(pixel)
media.set_red(newpixel,new_red)
media.set_green(newpixel,new_green)
media.set_blue(newpixel,new_blue)
return newpic
def rece_height(pic, factor):
'''return a new pic that has been compressed vertically by the factor which has been set
'''
# Create a new, all-black pic with the appropriate new height and
# old width; (all colour components are zero).
new_width = pic.get_width
new_height = (pic.get_height() - 1) / factor + 1
newpic = media.create_pic(new_width, new_height, media.black)
# Iterate through all the pixels in the original (large) image, and
# a portion of each pixel's colour components into the correct
# pixel position in the smaller image.
for pixel in pic:
# Find the corresponding pixel in the new pic.
x = media.get_x(pixel)
y = media.get_y(pixel)
newpixel = media.get_pixel(newpic, x, y / factor)
# Add the appropriate fraction of this pixel's colour components
# to the components of the corresponding pixel in the new pic.
new_red = newpixel.get_red()+pixel.get_red()/factor
new_green = newpixel.get_green()+pixel.get_green()/factor
new_blue = newpixel.get_blue()+pixel.get_blue()/fctor
media.set_red(newpixel,new_red)
media.set_green(newpixel,new_green)
media.set_blue(newpixel,new_blue)
return newpic
def rece_width(pic,factor):
'''Return a newpic that has been horizontally compressed by the factor which has been set.
'''
new_width = (media.get_width() - 1) / factor + 1
new_height = media.get_height()
newpic = media.create_pic(new_width, new_height, media.black)
for pixel in pic:
x = media.get_x(pixel)
y = media.get_y(pixel)
new_pixel = media.get_pixel(newpic, x / factor, y)
new_red = newpixel.get_red() + pixel.get_red() / factor
new_green = newpixel.get_green() + pixel.get() / factor
new_blue = newpixel.get_blue() + pixel.get()/factor
media.set_red(newpixel, new_red)
media.set_green(newpixel, new_green)
media.set_blue(newpixel, new_blue)
return newpic
def distance(pixel1, pixel2):
red1 = media.get_red(pixel1)
green1 = media.get_green(pixel1)
blue1 = media.get_blue(pixel1)
red2 = media.get_red(pixel2)
green2 = media.get_green(pixel2)
blue2 = media.get_blue(pixel2)
sum = abs(red1 -red2) + abs(green1 - green2) + abs(blue1 - blu2)
return sum
def simple_difference(pic1, pic2):
for pixel in pic1:
x = media.get_x(pixel)
y = media.get_y(pixel)
pixel2 = media.get_pixel(pic2, x, y)
sum = media.distance(pixel, pixel2)
return sum
def smart_difference(pic1,pic2):
height1 = media.get_height(pic1)
height2 = media.get_height(pic2)
factorh = float(height1 / height2)
if factorh >= 1:
height1 = media.rece_height(pic1, factorh)
else:
height2 = media.rece_height(pic2, 1 / factorh)
width1 = media.get_width(pic1)
width2 = media.get_width(pic2)
factorw = float(width1 / width2)
if factorw >= 1:
width1 = rece_width(pic1, factorw)
else:
width2 = rece_width(pic2, 1 / factorw)
red1 = red_average(pic1)
green1 = green_average(pic1)
blue1 = blue_average(pic1)
red2 = media.scale_red(pic2, red1)
green2 = media.scale_green(pic2, green1)
blue2 = media.scale_blue(pic2, blue1)
#if __name__ == '__main__':
#media.show(newpic)