Python Forum

Full Version: modify an image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys, i've got a problem with my program; i want to add a square with random colors on a picture thanks to a double loop but python show me that there's a syntax error on x=x+1 and y=y+1

Can someone help me please ?

from PIL import Image
from random import randint
im1 = Image.open("lena_std.tif")
print("Les dimensions de l'image sont de",im1.size,"pixels")
print("Les valeurs du pixel à la position (10,10) sont de",im1.getpixel((10,10)))
X=206
y=206
while y<307:
while X<307:
im1.putpixel((X,y),(random.randint(0,255),random.randint(0,255),random.randint(0,255))
X = X+1
y=y+1
With some tiny changes your program runs well:

#!/usr/bin/python3
from PIL import Image
from random import randint

im1 = Image.open("lena.jpg") 
print("Les dimensions de l'image sont de",im1.size,"pixels")
print("Les valeurs du pixel à la position (10,10) sont de",im1.getpixel((10,10)))

y=206
while y<307:
  x=206
  while x<307:
    im1.putpixel((x,y), (randint(0,255),randint(0,255),randint(0,255)))
    x = x + 1
  y = y + 1

im1.show()
Thank's a lot for your help Dance
You are welcome.