python有什么比较好的图像颜色相似度对比的库么

作者&投稿:布澜 (若有异议请与网页底部的电邮联系)
使用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 reduce_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 copy
# 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 reduce_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.reduce_height(pic1, factorh)
else:
height2 = media.reduce_height(pic2, 1 / factorh)

width1 = media.get_width(pic1)
width2 = media.get_width(pic2)
factorw = float(width1 / width2)
if factorw >= 1:
width1 = reduce_width(pic1, factorw)
else:
width2 = reduce_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)

基于用户相似度的推荐中,一般采用Pearson相关系数;
基于物品相似度的推荐中,改进的余弦相似度效果更好。

需要使用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


python比java简单吗?
Java是一种严格的类型语言,意味着必须声明变量名,相对比之下,动态类型的Python就不需要声明变量。Java是可以创建跨平台的应用程序,而Python几乎可以兼容当前的所有程序。对于新手来说,Python比Java更加简单,而且代码易读性强,Python要比简单很多,Java是非常复杂的,对于没有技术基础的人来说,刚入手就...

JAVA和Python有啥区别?
python是C语言库支持的。更接近函数式编程。同时与操作系统底层也通过C,直接打交道。java这方面就弱多。java更适合做网站,做分布式计算。所以科学上,网络上用得多。python覆盖面广。从教育,科研,运维管理,到一般的网站全都有。主要侧重在运维管理方面。本来它创建时就为了管理用的。java当时创建就...

Python能干什么
2、网络爬虫 网络爬虫是Python比较常用的一个场景,国际上,google在早期大量地使用Python语言作为网络爬虫的基础,带动了整个Python语言的应用发展。以前国内很多人用采集器搜刮网上的内容,现在用Python收集网上的信息比以前容易很多了,如:从各大网站爬取商品折扣信息,比较获取最优选择;对社交网络上发言...

为什么选择Python编程
4、生产力:相比其他编程语言可选的今天,一个可以得到很多功能在几行python编程代码下就可以完成。你可以完成一项任务在每次都比其他语言更少的代码,进而增加了生产力和降低了精力的花费。Python的未来以上所有点考虑,python似乎有明亮而清晰的未来。像YouTube这样的公司就是一个很好的例子,美国银行(Bank...

python和java哪个简单
Python比Java更简单。Python的语法简洁易懂,使用了缩进来定义代码块,不需要像Java那样使用大括号。这种简洁的语法使得Python更容易上手。Python的动态类型系统也使其在编程时不需要像Java那样进行显式的类型声明,从而减少了编码时的繁琐。此外,Python的面向对象编程特性也更容易理解和实现。Python的设计初衷...

学Python有前途么?
最后,薪资水平。目前初级Python工程师薪资待遇就达10-15K,而随着开发年限的增加,Python开发者薪资呈直线上升的变化趋势,工作8年的Python薪资攀升至25K左右。此外,国家也在加大培养Python人才。国务院发布《新一代人工智能发展规划》,人工智能正式纳入国家发展战略,并且已经有数个省份将Python纳入到高考体系...

python与c++学哪个好
综上所述,Python和C++都是非常重要的编程语言,但是它们有各自的优缺点和适用场景。对于初学者来说,学习Python要比学习C++容易得多。但是,如果你想从事游戏开发、操作系统等领域的编程工作,那么学习C++也是非常必要的。如果你想从事数据分析、机器学习等领域的编程工作,那么学习Python是更好的选择。

python是什么语言
Python特点主要有以下几个方面:1、简单:Python是一种代表简单主义思想的语言。阅读一个良好的Python程序就感觉像是在读英语一样。它使你能够专注于解决问题而不是去搞明白语言本身。2、易学:Python极其容易上手,因为Python有极其简单的说明文档。3、速度快:Python 的底层是用 C 语言写的,很多标准库...

大学学习py可以做什么?
学习Pythonon数据可以做什么 游戏开发 python 写游戏的优势应该是能够让人员比较稳定。然后就是 python 比 lua 有更高的抽象能力,可以用更少的代码描述游戏业务逻辑,与luq 相比,python 更适合作为一种host 语言,即程序的入口点是在 python 那一端会比较好,然后用 c\/c++ 在非常必要的时候写一些扩展 python 非常适...

Python用来做什么开发比较有优势
python在Web开发、数据分析、机器学习等方面,有较好的优势。1、Web开发 Django和Flask等基于Python的Web框架最近在Web开发中非常流行。这些Web框架可以帮助你用Python编写服务器端代码(后端代码)。这是在你的额服务器上运行的代码,而不是运行在用户设备和浏览器的代码(前端代码)。2、数据科学 数据科学,...

源城区18326276563: 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....

源城区18326276563: python matplotlib 有几种不同颜色的黑白图 -
标伊烯丙: 下面的两行程序通过调用plot函数在当前的绘图对象中进行绘图:plt.plot(years, price, 'b*')#,label=$cos(x^2)$)plt.plot(years, price, 'r')

源城区18326276563: python 3.6 turtle 都有哪些颜色 -
标伊烯丙: turtle.color(*args) Return or set pencolor and fillcolor. Several input formats are allowed. They use 0 to 3 arguments as follows: color() Return the current pencolor and the current fillcolor as a pair of color specification strings or tuples as returned by ...

源城区18326276563: 用python软件画填色的图案,为什么只有海龟路线上有颜色 -
标伊烯丙: 用python软件画填色的图案,只有海龟路线上有颜色,是设置错误造成的,解决方法如下: 1、首先用opencv模块读取图片数据,得到一个三维矩阵. 2、然后用numpy模块构造一个二维0矩阵,规模和图像的大小一样.3、接着把图片的第一个通道的像素值置零,就相当于去掉了图片的蓝色:a[:,:,0] = c. 4、如果想单独分离出绿色,还需要把第三个通道的像素值置零:a[:,:,0] = ca[:,:,2] = c. 5、单独显示红色:a[:,:,0] = ca[:,:,1] = c. 6、最后不构造零矩阵也是可以的,这样就不用调用numpy模块了.

源城区18326276563: 如何用python将文件夹中图片根据颜色分类 -
标伊烯丙: 本文实例讲述了Python通过PIL获取图片主要颜色并和颜色库进行对比的方法.分享给大家供大家参考.具体分析如下:这段代码主要用来从图片提取其主要颜色,类似Goolge和Baidu的图片搜索时可以指定按照颜色搜索,所以我们先需要将每张...

源城区18326276563: 怎么样Python通过实现将颜色hex值转换成rgb -
标伊烯丙: 可以考虑使用python+opencv,比源生的python自己编程转换要方便得多. 另外一个选择就是用python自己的库:PIL colorsys.rgb_to_hsv

源城区18326276563: python的pillow库怎么处理灰度图像 -
标伊烯丙: Pillow是Python里的图像处理库(PIL:Python Image Library),提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存、图像显示、格式转换以及基本的图像处理操作等.1)使用 Image 类 PIL最重要的类是 Image class, 你可...

源城区18326276563: 用python写识别图片主要颜色的程序 -
标伊烯丙: # -*- coding: utf-8 -*- import colorsys def get_dominant_color(image): #颜色模式转换,以便输出rgb颜色值 image = image.convert('RGBA') #生成缩略图,减少计算量,减小cpu压力 image.thumbnail((200, 200)) max_score = None dominant_...

源城区18326276563: 现在比较好用的前端开发工具有哪些啊? -
标伊烯丙: 作为一个前端开发工程师,使用一款自己上手且功能强大的开发工具是非常重要的,但是面对这么多开发工具,到底哪个比较好呢?下面我个人推荐几款自己感觉还不错的前端开发工具,希望作为大家的参考. 1、hbuilder 作为一个编程的菜鸟...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网