from PIL import Image
########## Begin ##########
#调整一个像素的亮度
def enhancePixel(r, g, b, n):
er, eg, eb = r + n, g + n, b + n
if255< er:
er =255
elif 0> er:
er =0if255< eg:
eg =255
elif 0> eg:
eg =0if255< eb:
eb =255
elif 0> eb:
eb =0return er, eg, eb
########## End ##########
#调整图像文件1中所有像素的亮度,并将调整结果保存为图像文件2
#图像文件1和2的路径分别为path1和path2,n为调整量
def enhanceImg(path1, path2, n):
img = Image.open(path1)
w, h = img.size
for x in range(w):for y in range(h):
r,g,b=img.getpixel((x, y)) #取出颜色
er, eg, eb =enhancePixel(r, g, b, n) #调整亮度
img.putpixel((x,y),(er, eg, eb)) #放回像素
img.save(path2)
path1 ='src/step1/source/step1.jpg' #原始图像文件
path2 ='src/step1/student/step1_2.jpg'#调整亮度后的图像文件
n =50 #调整量
enhanceImg(path1, path2,50)
第2关:灰度图
from PIL import Image
#将一个RGB颜色转换成灰度值,结果保留整数
def RGBtoGray(r, g, b):
########## Begin ##########
gray =round(r*0.299+ g*0.587+ b*0.114)
########## End ##########
return gray
#将真彩色图像转换成灰度图
#真彩色和灰度图的文件路径分别为path1和path2
def toGrayImage(path1, path2):
img1 = Image.open(path1) #真彩色图像,像素中是RGB颜色
w, h = img1.size
img2 = Image.new('L',(w, h)) #新建一个灰度图像,像素中是灰度值
########## Begin ##########
#此部分功能:依次取出img1中每个像素的RGB颜色,转换成灰度值,再放到img2的对应位置
for x in range(w):for y in range(h):
r,g,b=img1.getpixel((x, y)) #取出颜色
gray =RGBtoGray(r, g, b)
img2.putpixel((x,y), gray) #放回像素
########## End ##########
img2.save(path2)
path1 ='src/step2/source/step2.jpg' #真彩色图像
path2 ='src/step2/student/step2_2.jpg' #灰度图像
toGrayImage(path1, path2)
第3关:字符画
from PIL import Image
#将一个灰度值转换成对应的字符
def GraytoChar(gray):
allChar ="""@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'"""
########## Begin ##########
char= allChar[int(len(allChar)*gray/256)]
########## End ##########
returnchar
#将真彩色图像转换成字符画,真彩色图像文件路径为path
def toCharImage(path):
img = Image.open(path)
ow, oh = img.size #原始图像的宽和高
w =80 #调整后的宽度(即字符画每行有w个字符)
h = w*oh//ow #高度也按比例调整(即字符画总共有h行)
h = h//2 #在文本中,行与行之间有间隔,要进一步缩小高度才能得到较好效果
img = img.resize((w, h)) #调整图片大小
img = img.convert('L') #将真彩色图像转换为灰度图
charImg ='' #用于存储字符画
########## Begin ##########
#将img中的灰度值逐一转换成字符,每得到一个字符,添加到charImg最后,转换完一行要添加换行符
for y in range(h):
s =''for x in range(w):
gray = img.getpixel((x, y)) # 取出灰度值
char=GraytoChar(gray)
s = s +char
charImg = charImg + s +'\n'
########## End ##########
return charImg
第4关:图像降噪
from PIL import Image
#求图像img中(x,y)处像素的中值c
def median(img, x, y):
########## Begin ##########
L =[]
xl =[x-1,x,x+1]
yl =[y-1,y,y+1]for i in xl:for j in yl:
gray = img.getpixel((i, j)) # 取出灰度值
L.append(gray)
L.sort()
c = L[4]
########## End ##########
return c
#对图像文件1进行降噪,并将结果保存为图像文件2
#图像文件1和2的路径分别为path1和path2
def denoise(path1, path2):
img1 = Image.open(path1) #图像1
img1 = img1.convert('L') #将图像1转换为灰度图
w, h = img1.size
img2 = Image.new('L',(w, h),'white') #图像2for x in range(1, w-1):for y in range(1, h-1):
c =median(img1, x, y) #求中值
img2.putpixel((x,y), c) #将灰度设置为中值
img2.save(path2)
path1 ='src/step4/source/step4.bmp' #带噪声的图像
path2 ='src/step4/student/step4_2.bmp' #降噪后的图像
denoise(path1, path2)
第5关:图像边缘检测
from PIL import Image
#求图像img中(x,y)处像素的卷积c
def convolute(img, x, y):
########## Begin ##########
juanjihe =[1,1,1,1,-8,1,1,1,1]
L =[]
xl =[x -1, x, x +1]
yl =[y -1, y, y +1]for j in yl:for i in xl:
gray = img.getpixel((i, j)) # 取出灰度值
L.append(gray)
c =0for i,j in zip(juanjihe,L):
c = c + i*j
########## End ##########
return c
#对图像文件1进行边缘检测,并将结果保存为图像文件2
#图像文件1和2的路径分别为path1和path2
def detectEdge(path1, path2):
img1 = Image.open(path1) #图像1
img1 = img1.convert('L') #将图像1转换为灰度图
w, h = img1.size
img2 = Image.new('L',(w, h),'white') #图像2
########## Begin ##########
##此部分功能:依次求img1中每个像素的卷积c,再将c放到img2的对应位置
for x in range(1, w -1):for y in range(1, h -1):
c =convolute(img1, x, y)if c>0:
s=0else:
s=255
img2.putpixel((x, y), s)
########## End ##########
img2.save(path2)
path1 ='src/step5/source/step5.bmp' #原始图像
path2 ='src/step5/student/step5_2.bmp' #检测到的边缘图像
detectEdge(path1, path2)
第6关:图像置换加密
from PIL import Image
########## Begin ##########
def helper(x, y, n):
a =1
b =2
x2 =(x + b * y)% n
y2 =(a * x +(a * b +1)* y)% n
return x2,y2
def enImg(path1, path2):
img1 = Image.open(path1)
w, h = img1.size
img2 = Image.new('RGB',(w, h),'white')for x in range(w):for y in range(h):
gray = img1.getpixel((x, y))
x,y =helper(x,y,w)
img2.putpixel((x, y), gray)
img2.save(path2)
########## End ##########
path1 ='src/step6/source/step6.bmp' #原始图像
path2 ='src/step6/student/step6_2.bmp' #加密后的图像
enImg(path1, path2)