就是给出以下几个function的def 越多越好:
1、 red_average(Picture) 算出pic众pixels的平均红值 。
2、scale_red(Picture, int) 调整图片红值 并确保其不超过255 。
3、expand_width(Picture, int) 。
4、reduce_width(Picture, int) 放大和缩小宽值 都是乘或者除的 ,distance(Pixel, Pixel) 以红蓝绿值为标准 计算两个pixel之间的距离(类似于xyz坐标轴中两点距离)。
5、simple_difference(Picture,Picture) 简单计算两张图片有多相似 不必考虑长宽。
6、smart_difference(Picture,Picture) 这个方程的步骤需为: 判断图片大小 。如必要 乘除高度 。 如必要 乘除宽度。 调整图片颜色使之相同平均红蓝绿值 。
Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中 有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。
import mediadef 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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)