import os

from PIL import Image

def mkthumb(fp, h=128, w=128):
    """ Create a thumbnail for an image

        fp  filesystem path to the full size image
        h   height (default is 128)
        w   width  (default is 128)

        The thumbnail will be unceremoniously dumped in the same
        directory with 'thumbnail' between the file name and extension.

     """

    size = (h, w)
    f, ext = os.path.splitext(fp)

    im = Image.open(fp)
    im.thumbnail(size, Image.ANTIALIAS)
    im.save('.thumbnail'.join([f, ext]))

import os

from PIL import Image

def mkthumb(fp, h=128, w=128):
    """ Create a thumbnail for an image

        fp  filesystem path to the full size image
        h   height (default is 128)
        w   width  (default is 128)

        The thumbnail will be unceremoniously dumped in the same
        directory with 'thumbnail' between the file name and extension.

     """

    size = (h, w)
    f, ext = os.path.splitext(fp)

    with Image.open(fp) as im:
        im.thumbnail(size, Image.ANTIALIAS)
        im.save('.thumbnail'.join([f, ext]))

Use context manager for PIL Open
Use context manager for PIL Open

This is currently kind of buggy with Python3. A harmless warning is
emitted since the handle is still closed late inside PIL/Pillow.

  import os
  
  from PIL import Image
  
  def mkthumb(fp, h=128, w=128):
      """ Create a thumbnail for an image
  
          fp  filesystem path to the full size image
          h   height (default is 128)
          w   width  (default is 128)
  
          The thumbnail will be unceremoniously dumped in the same
          directory with 'thumbnail' between the file name and extension.
  
       """
  
      size = (h, w)
      f, ext = os.path.splitext(fp)
  
-     im = Image.open(fp)
+     with Image.open(fp) as im:
-     im.thumbnail(size, Image.ANTIALIAS)
+         im.thumbnail(size, Image.ANTIALIAS)
-     im.save('.thumbnail'.join([f, ext]))
+         im.save('.thumbnail'.join([f, ext]))
  
Use context manager for PIL Open
## Code Before:
import os

from PIL import Image

def mkthumb(fp, h=128, w=128):
    """ Create a thumbnail for an image

        fp  filesystem path to the full size image
        h   height (default is 128)
        w   width  (default is 128)

        The thumbnail will be unceremoniously dumped in the same
        directory with 'thumbnail' between the file name and extension.

     """

    size = (h, w)
    f, ext = os.path.splitext(fp)

    im = Image.open(fp)
    im.thumbnail(size, Image.ANTIALIAS)
    im.save('.thumbnail'.join([f, ext]))

## Instruction:
Use context manager for PIL Open
## Code After:
import os

from PIL import Image

def mkthumb(fp, h=128, w=128):
    """ Create a thumbnail for an image

        fp  filesystem path to the full size image
        h   height (default is 128)
        w   width  (default is 128)

        The thumbnail will be unceremoniously dumped in the same
        directory with 'thumbnail' between the file name and extension.

     """

    size = (h, w)
    f, ext = os.path.splitext(fp)

    with Image.open(fp) as im:
        im.thumbnail(size, Image.ANTIALIAS)
        im.save('.thumbnail'.join([f, ext]))
