IndexError: image index out of range using PIL

0

I'm starting to work with image processing and I need to do a simple exercise to double the size of one.

I have the following script:

from PIL import Image

imagem = Image.open("local_da_Imagem", "r")

largura, altura = imagem.size

fator = 2

W = largura
H = altura

nova_largura = int(largura*fator)
nova_altura = int(altura*fator)

nova_imagem = Image.new("RGB", (nova_largura, nova_altura))

for col in range(nova_largura):
  for row in range(nova_altura):
      p = imagem.getpixel((col, row))
      nova_imagem.putpixel((col*fator, row*fator), 100)

nova_imagem.show()

But when I run this script, I get the error: IndexError: image index out of range.

Someone could help me. Showing me where I'm going wrong in this script?

I thank all those who can.

    
asked by anonymous 20.08.2017 / 15:32

1 answer

1

Both are being spanned by new height and new_large, which are the original height and width multiplied by 2, and you are using getPixel to get the pixel of the image in position. That is, you are scrolling the image by 2x the size of it as it traverses it with values multiplied by 2. Outside you are passing in putPixel the value 100, not the value returned by getPixel. And according to the algorithm you passed to me the 0x0 would look like 0x1, 1x0, 1x1.

I came up with something like this:

from PIL import Image

imagem = Image.open("Downloads/rgb.png", "r")

largura, altura = imagem.size

fator = 2

W = largura
H = altura

nova_largura = int(largura*fator)
nova_altura = int(altura*fator)

nova_imagem = Image.new("RGB", (nova_largura, nova_altura))

for col in range(largura):
  for row in range(altura):
      p = imagem.getpixel((col, row))
      nova_imagem.putpixel((col*fator, row*fator), p)
      nova_imagem.putpixel((col*fator+1, row*fator+1), p)
      nova_imagem.putpixel((col*fator+1, row*fator), p)
      nova_imagem.putpixel((col*fator, row*fator+1), p)


nova_imagem.show()
    
20.08.2017 / 15:45