问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

使用Python 制作对比图片相似度的程序

发布网友 发布时间:2022-04-18 09:23

我来回答

6个回答

热心网友 时间:2022-04-18 10:53

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 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 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)

热心网友 时间:2022-04-18 12:11

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

热心网友 时间:2022-04-18 13:45

用pll可以处理图片,至于怎么处理请你去看征服python的多媒体编程,授人与鱼不如授人与渔

热心网友 时间:2022-04-18 15:37

百度的分数没有比money更有吸引力

热心网友 时间:2022-04-18 17:45

推荐你到饭客网络去看看
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
导购什么字 店面导购员是什么意思 HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPlayer\SafeVersions... 无法安装adobeflashplayer,说无法注册ACTIVEX的控件 无法注册flash player的activex怎么办 omniverse create 他总说无法注册Flash player的Active控件 然后什么访问以下链接。不要... excel如何用进度条的形式表示完成率 买了件速干衣,北面的,求大神看一下真假。 秦皇岛银谷全城热恋是不是要预定票啊 如何将手机正在播放的短视频投屏到电脑上 长沙教育服务平台公众号客服热线 怎么把jpeg格式的图片改成bmp格式 python怎么识别类似目标图形并选出 如何把其他格式的图片改成BMP格式 属于温性水果有哪些 什么是温性的水果 急需一篇“学好(数控)专业技能”的演讲稿,1000字左右. 那些水果属于温性水果 以如何端正专业思想,热爱自己的专业,努力学习专业知识为主题的演讲稿 如何学好专业作文 专业学习应该既重知识又重技能演讲稿 财产保险理赔程序包括哪些 保险索赔程序有哪些 急求一篇关于理想的350字作文 兰州市燃气卡丢了怎么补 ===Word2010打印问题=== 办公室电脑今天刚开机,桌面图标全无,右击桌面无效,左键双击弹出开始菜单,开始,任务栏都是正常的。 请问我的word2010的图标怎么变成这样了? 办公桌面有没有什么能让他整洁点的办法? 如何下载百度hi 百度Hi怎么下? 百度Hi下载完不是*.exe的可执行文件,应该用哪种方式打开? 小学生的作业该如何布置呢 什么是百度Hi客户端 小学生作业怎样布置合理 有谁下载了百度hi,好用吗, 牡丹江播音主持艺考培训哪里好? 黑龙江省内有推荐的艺考培训学校么? 黑龙江哪里有艺考培训学校? 浅谈如何布置小学生作业 艺考培训学校哪里好? 教师短论文 小学生家庭作业怎样布置才合理 黑龙江舞蹈大学有哪些? 哪个艺考培训比较好啊? 艺考培训的学校都有哪些 艺考考前集训学校有哪些 有哪些不错的学表演的艺考培训学校? 路由器fast_5b3ee4 普联路由器如何查询真伪