miayang1661
miayang1661

Reputation: 1

problem with python pillow, not really sure what's wrong

So we're basically supposed to be making boxes looking like this with pillow: [enter image description here] 1 The boxes should be forming randomly.

def draw_quilt():
  width = 400
  height = 400
  grid_size = width / 4

  img = Image.new("RGB", (width, height))
  draw = ImageDraw.Draw(img)

  r = 0; g = 192; b = 192
  for higher_y in range(0, height, grid_size):
    lower_y = higher_y-grid_size
    r += 10; g-= 10; b-= 10 
    for higher_x in range(0,width, grid_size):
      lower_x = higher_x+grid_size
      draw_patches(draw, int(grid_size / randomint(1,3), top_x, top_y, bottom_x, bottom_y, (r, g, b))

def draw_patches(draw, patch_width, start_x, start_y, end_x, end_y, color):
  r = color[0]; g = color[1]; b = color[2]
  for top_y in range(start_y, end_y, patch_width):
    r += 10; g += 5; b += 3
    bottom_y = top_y + patch_width
    for top_x in range(start_x, end_x, patch_width):
      r += 10; g += 5; b += 3
      bottom_x = top_x + patch_width
      draw.rectangle((top_x, top_y, bottom_x, bottom_y), fill = (r, g, b))

if __name__ == "__main__":

    img  = Image.new("RGB", (width, height))
    draw = ImageDraw.Draw(img)
    
    draw_quilt()

    img.save('question4.png')
    img.show()

I'm not really sure about what's wrong, but it just keeps appearing "Syntax Error" on the screen

Upvotes: 0

Views: 64

Answers (2)

miayang1661
miayang1661

Reputation: 1

def draw_quilt():
  width = 400
  height = 400
  grid_size = int(width / 4)

  img = Image.new("RGB", (width, height))
  draw = ImageDraw.Draw(img)

  r = 150; g = 299; b = 299
  for top_y in range(0, height, grid_size):
    r += 10; g += 5; b += 3
    bottom_y = top_y + grid_size
    for top_x in range(0, width, grid_size):
      r += 10; g += 5; b += 3
      bottom_x = top_x + grid_size
      draw_patches(draw, int(grid_size // randint(1,3)), top_x, top_y, bottom_x, bottom_y, (r, g, b))

def draw_patches(draw, patch_width, start_x, start_y, end_x, end_y, color):
  r = color[0]; g = color[1]; b = color[2]
  for top_y in range(start_y, end_y, patch_width):
    r += 10; g += 5; b += 3
    bottom_y = top_y + patch_width
    for top_x in range(start_x, end_x, patch_width):
      r += 10; g += 5; b += 3
      bottom_x = top_x + patch_width
      draw.rectangle((top_x, top_y, bottom_x, bottom_y), fill = (r, g, b))

if __name__ == "__main__":

    img  = Image.new("RGB", (width, height))
    draw = ImageDraw.Draw(img)
    
    draw_quilt()

    img.save('question4.png')
    img.show()

Upvotes: 0

sj95126
sj95126

Reputation: 6898

You're missing a closing ) at the end of this line:

draw_patches(draw, int(grid_size / randomint(1,3), top_x, top_y, bottom_x, bottom_y, (r, g, b))

The SyntaxError you're getting is on the line that contains def draw_patches but the actual error is the line before.

Upvotes: 1

Related Questions