title: Rotating a square in PIL
body: <p>With PIL, I want to draw a rotated square on an image, by specifying the square side length, and the rotation angle. The square should be white, and the background grey. For example, the following image has a rotation of 45 degrees:</p>
  
  <p><a href="https://i.stack.imgur.com/jqp8i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jqp8i.png" alt="enter image description here"></a></p>
  
  <p>The only way I know how to do rotations in PIL is by rotating the entire image. But if I start with the following image:</p>
  
  <p><a href="https://i.stack.imgur.com/VVeuY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VVeuY.png" alt="enter image description here"></a></p>
  
  <p>And then rotate it by 45 degrees, I get this:</p>
  
  <p><a href="https://i.stack.imgur.com/H6uIK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H6uIK.png" alt="enter image description here"></a></p>
  
  <p>That method just introduces black parts to fill in the "undefined" region of the image.</p>
  
  <p>How can I rotate just the square?</p>
  
  <p>The code to generate my original square (the second figure) is as follows:</p>
  
  <pre><code>from PIL import Image
  
  image = Image.new('L', (100, 100), 127)
  pixels = image.load()
  
  for i in range(30, image.size[0] - 30):
      for j in range(30, image.size[1] - 30):
          pixels[i, j] = 255
  
  rotated_image = image.rotate(45)
  rotated_image.save("rotated_image.bmp")
  </code></pre>