Question

I am trying to implement a program, that will increase the width of an image by one pixel. I then want to take the new maximum x ordinate and put this with a random y ordinate (that is within the range of the image) to create a new pixel.

 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)

I get this error:

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

I understand it is out of the range. What am I doing wrong?

Was it helpful?

Solution

Here is generalized approach to increase the size of an image keeping its current content:

Feel free to adapt.

# 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)


Output (Painting by Jean-Michel Basquiat):


................enter image description here...........................enter image description here................


OTHER TIPS

How can you get something that an object does not have?

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

The original image remains sized at getWidth(pic) but you are asking for a pixel at getWidth(pic) + 1 which does not exist.

You can enlarge the image by copying it to a new picture similar to this answer.

... 
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) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top