code: def convert_pattern_to_pil(pattern, version=1):
      
      from PIL import Image
      mode = get_pil_mode(pattern.image_mode.name, False)
      # The order is different here.
      size = pattern.data.rectangle[3], pattern.data.rectangle[2]
      channels = [
          _create_channel(size, c.get_data(version), c.pixel_depth).convert('L')
          for c in pattern.data.channels if c.is_written
      ]
      if len(channels) == len(mode) + 1:
          mode += 'A'  # TODO: Perhaps doesn't work for some modes.
      if mode == 'P':
          image = channels[0]
          image.putpalette([x for rgb in pattern.color_table for x in rgb])
      else:
          image = Image.merge(mode, channels)
      if mode == 'CMYK':
          image = image.point(lambda x: 255 - x)
      return image
docs: Convert Pattern to PIL Image.