gpt4 book ai didi

python - Jython 图像处理

转载 作者:太空狗 更新时间:2023-10-30 00:42:08 37 4
gpt4 key购买 nike

这个程序应该是画出一幅图像的轮廓,然后把它分成不同的象限,然后给它上色,比如安迪沃霍尔玛丽莲梦露的照片。

“Warholize”函数之前的每个函数都有效,但它卡在 warholize 函数下的 c=getPixel(picEdge,x,y) 上我不知道该怎么做。任何帮助将不胜感激。它应该做“让 c 成为 picEdge 中 x,y 位置像素的颜色”

def main():
pic= makePicture( pickAFile() )
show( pic )
threshold= 10
edgePic= makeOutline( pic, threshold )
warholize(pic)
show(warholize(pic))

def difference( a, b ):
if a > b :
return a - b
else:
return b - a

def intensity( px ) :
r= getRed( px )
g= getBlue( px )
b= getGreen( px )
avg= ( r + g + b ) / 3
return avg

def makeOutline( pic, threshold ):
w= getWidth( pic )
h= getHeight( pic )
edgePic= makeEmptyPicture( w, h )
for x in range(2,w-1) :
for y in range(2,h-1):
px= getPixel( pic, x, y )
pxLeft= getPixel( pic, x-1, y )
pxUp= getPixel( pic, x, y-1 )
leftDiff= difference( intensity(pxLeft), intensity(px) )
upDiff= difference( intensity(pxUp), intensity(px) )
if leftDiff > threshold or upDiff > threshold :
setColor( getPixel(edgePic,x,y), black )

def warholize(pic):
threshold=10
picEdge=makeOutline(pic,threshold)
w= getWidth( pic )
h= getHeight( pic )
picNew= makeEmptyPicture( w, h )

for x in range(0,w,2):
for y in range (0,h,2):
c=getPixel(picEdge,x,y)
px=getPixel(picNew,x/2,y/2)
if c is black:
setColor(px,blue)
else:
setColor(px,yellow)
return picNew

最佳答案

我将 difference 和 intensity 函数放入 makeOutline 函数中。我已经从 makeOutline 函数中调用了 warholize 函数。
此外,您需要获取单独象限的颜色,在这里,我只是使用 getRed 像素来查看它是黑色还是白色(是否是全彩色)。

在这种情况下,我使用了 100 的阈值来优化效果。我已经在 makeOutline 函数中设置了阈值,您可以随心所欲地使用它。

  def main():
pic= makePicture( pickAFile() )
show(pic)
newPic= makeOutline(pic )
show(newPic)

Pic

def makeOutline(pic ):
picEdge=makeEmptyPicture(getWidth(pic),getHeight(pic))
for x in range (0, getWidth(pic)-1):
for y in range (0, getHeight(pic)-1):
here=getPixel(picEdge,x,y)
down = getPixel(pic,x,y+1)
right = getPixel(pic, x+1,y)
hereL=(getRed(here)+getGreen(here)+getBlue(here))/3
downL=(getRed(down)+getGreen(down)+getBlue(down))/3
rightL=(getRed(right)+getGreen(right)+getBlue(right))/3
if abs (hereL-downL)>100 and abs(hereL-rightL)>100:
setColor(here,black)
if abs (hereL-downL)<=100 or abs(hereL-rightL)<=100:
setColor(here,white)
warholizedPic=warholize(picEdge)
return warholizedPic

def warholize(picEdge):
w= getWidth( picEdge )
h= getHeight( picEdge )
picNew= makeEmptyPicture( w, h )
for x in range(0,w/2):
for y in range (0,h/2):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,blue)
else:
setColor(pxNew,yellow)
for x in range (w/2,w):
for y in range (h/2,h):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,yellow)
else:
setColor(pxNew,blue)

for x in range(0,w/2):
for y in range (h/2,h):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,green)
else:
setColor(pxNew,red)
for x in range (w/2,w):
for y in range (0,h/2):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,red)
else:
setColor(pxNew,green)


return picNew

Warholized pic

您可以根据需要调整要着色的象限。

关于python - Jython 图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2337110/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com