1

我正在尝试实现一个程序,它将图像的宽度增加一个像素。然后我想采用新的最大 x 坐标并将其与随机 y 坐标(即在图像范围内)一起创建一个新像素。

 for x in range (0,getWidth(pic)):
    for y in range (0,getHeight(pic)):
      X=getWidth(pic)
      newX = (X+1)
      colr=(255,0,0)
      newPixel = getPixel (pic, newX, y)//line 25
      setColor(newPixel, colr)
      Y=getHeight(pic)
      newY= (Y+1)
      newPixel = getPixel( pic,x, newY)
      setColor(newPixel, colr)

我收到此错误:

getPixel(picture,x,y): x (= 226) is less than 0 or bigger than the width (= 224)
The error was:
Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
Please check line 25 of D:\bla bla

我知道它超出了范围。我究竟做错了什么?

4

2 回答 2

4

这是增加图像大小保持其当前内容的通用方法:

随意适应。

# Increase a picture given an offset, a color and the anciant 
# content must be centered or not.
# Offsets must be positive.
def increaseAndCopy(pic, offsetX, offsetY, bg_color=black, center=True):

  # Offsets must be positive
  if (offsetX < 0.0) or (offsetY < 0.0):
    printNow("Error: Offsets must be positive !")
    return None

  new_w = pic.getWidth() + int(2*offsetX)
  new_h = pic.getHeight() + int(2*offsetY)

  startX = 0
  startY = 0
  if (center) and (offsetX > 1.0):
    startX = int(offsetX)
  if (center) and (offsetY > 1.0):
    startY = int(offsetY)

  new_pic = makeEmptyPicture(new_w, new_h)
  # Fill with background color
  setAllPixelsToAColor(new_pic, bg_color)

  # Process copy
  for x in xrange(pic.getWidth()):
    for y in xrange(pic.getHeight()):
      px = getPixel(pic, x, y)
      new_px = getPixel(new_pic, x + startX, y + startY)
      setColor(new_px, getColor(px))

  return new_pic

file = pickAFile()
picture = makePicture(file)

# Pass an offset of 0.5 to increase by 1 pixel
#new_picture = increaseAndCopy(picture, 0.5, 0, blue)
new_picture = increaseAndCopy(picture, 10, 20, gray, True)

if (new_picture):
  writePictureTo(new_picture, "/home/biggerPic.png")
  show(new_picture)


输出(Jean-Michel Basquiat 的绘画):


…………………………………………………………………………………………………………………………………… 在此处输入图像描述_ 在此处输入图像描述_ …………


于 2013-06-28T13:53:08.987 回答
4

你怎么能得到一个物体没有的东西?

newPixel = getPixel (pic, newX, y)//line 25

原始图像的大小保持在 getWidth(pic) 但您要求的像素getWidth(pic) + 1不存在。

您可以通过将图像复制到类似于此答案的新图片来放大图像。

... 
newPic=makeEmptyPicture(newX,newY)
xstart=0
ystart=0

for y in range(ystart,newY):

   for x in range(xstart, newX):

     if x == newX or y == newY: 
        colour=(255,0,0)
     else:
        oldPixel=getPixel(oldPic,x,y)
        colour=getColor(oldPixel) 

     newPixel=getPixel(newPic,x,y)
     setColor(newPixel,colour)


     explore(newPic) 
于 2013-06-22T04:46:15.150 回答