使用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
推荐你到饭客网络去看看