该程序的目的是获取 ppm 图像并将其压印。(可以在这里找到整个项目的详细信息)我正在帮助对作业进行评分,但似乎找不到学生的错误。
我使用的原始图像如下所示:
结果应如下所示:
这是整个程序(在问题行周围有注释):
# making an image embossed
import numpy
def clamp(color):
if color<0:
return 0
elif color>255:
return 255
else:
return color
def get_num(jen):
variable = ''
ch = gwen.read(1)
while ch.startswith('#'):
while ch!='\n':
ch=gwen.read(1)
while ch.isspace():
ch = gwen.read(1)
while ch.isspace():
ch = gwen.read(1)
while ch.isdigit():
variable = variable + ch
ch=gwen.read(1)
if ch.startswith('#'):
while ch!='\n':
ch=gwen.read(1)
return int(variable)
def emboss(x,y):
d=numpy.empty((h,w*3),numpy.uint8)
print "len x[0]=",len(x[0])
print "len y=", len(y)
print "len y[0]=", len(y[0])
for i in xrange(len(x)):
for j in xrange(0,len(x[0]),3):
for k in xrange(3): #r,g,b loop
#if the next line is used a correct (but not embosed) image results
#d[i][j+k] = x[i][j+k]
sum = 0
for l in xrange(0,3,1):
for m in xrange(0,3,1):
#the next line embosses but causes a triple image in the process
sum = sum + ((x[(i+(l-1))%h][((j+k)+((m-1)*3))%w]) * y[l][m])
#the line below adjusts an embossed images brightness
#if not embossing comment out this line
d[i][j+k]=clamp(sum+127)
return d
name=raw_input('Please enter input name: ')
output= raw_input('Please enter output name: ')
gwen=open(name,"rb")
ch=gwen.read(1)
if ch=='P':
print ('This is P')
else:
print('Error in Header')
ch=gwen.read(1)
if ch=='6':
print ('This is 6')
else:
print('Error in Header')
jen=''
w=get_num(jen)
w=int(w)
print w
h=get_num(jen)
h=int(h)
print h
value=get_num(jen)
value=int(value)
print value
joe=open(output,"wb")
joe.write('P6'+' '+str(w)+' '+str(h)+' '+str(value)+'\n')
a=numpy.fromfile(gwen,numpy.uint8, w*h*3,'')
c=numpy.reshape(a,(h,w*3))
d=numpy.array([[1,1,1],[0,0,0],[-1,-1,-1]])
new=emboss(c,d)
for i in xrange(h):
for j in xrange(0,w*3,3):
r_value = new[i][j]
r=int(clamp(r_value))
g_value = new[i][j+1]
g=int(clamp(g_value))
b_value = new[i][j+2]
b=int(clamp(b_value))
joe.write('%c%c%c'%(r,g,b))
gwen.close()
joe.close()
在我看来,问题出在浮雕方法中,但我似乎无法解决。所以我包括了所有这些,甚至包括过滤掉 ppm 标题注释的部分。
就像现在一样,它会浮雕,但这样做会产生三重图像。去除压纹线后,三重图像消失。
如果您想自己尝试,这是我正在测试的文件
关于我应该改变什么来修复这个错误的任何建议?