
import Image
import logging
import sys

def main(argv):
  pngFileName = sys.argv[1]
  baseFileName, _ = pngFileName.rsplit('.')

  rawFileName = '%s.raw' % baseFileName
  palFileName = '%s.pal' % baseFileName

  image = Image.open(pngFileName)

  with open(palFileName, 'w') as palFile:
    pal = map(chr, image.getpalette())
    palFile.write("".join(pal))

  with open(rawFileName, 'w') as rawFile:
    w, h = image.size
    for y in range(h):
      for x in range(w):
        pixel = image.getpixel((x,y))
        rawFile.write(chr(pixel))

if __name__ == '__main__':
  FORMAT = '%(levelname)s: %(message)s'
  logging.basicConfig(format=FORMAT, level=logging.DEBUG)
  main(sys.argv)


import Image
import argparse
import os

def main():
  parser = argparse.ArgumentParser(
      description='Converts input image to raw image and palette data.')
  parser.add_argument('-f', '--force', action='store_true',
      help='If output files exist, the tool will overwrite them.')
  parser.add_argument('input', metavar='INPUT', type=str,
      help='Input image filename.')
  parser.add_argument('output', metavar='OUTPUT', type=str,
      help='Output files basename (without extension).')
  args = parser.parse_args()

  inputPath = os.path.abspath(args.input)
  outputPath = os.path.abspath(args.output)

  rawFilePath = '%s.raw' % outputPath
  palFilePath = '%s.pal' % outputPath

  if not os.path.isfile(inputPath):
    raise SystemExit('Input file does not exists!')

  if any(map(os.path.isfile, [rawFilePath, palFilePath])) and not args.force:
    raise SystemExit('Will not overwrite output files!')

  try:
    image = Image.open(inputPath)
  except IOError as ex:
    raise SystemExit('Error: %s.' % ex)
  else:
    with open(palFilePath, 'w') as palFile:
      pal = map(chr, image.getpalette())
      palFile.write("".join(pal))

    with open(rawFilePath, 'w') as rawFile:
      w, h = image.size
      for y in range(h):
        for x in range(w):
          pixel = image.getpixel((x,y))
          rawFile.write(chr(pixel))

if __name__ == '__main__':
  main()

Add cmdline options parser and a sanity check.
Add cmdline options parser and a sanity check.

  
  import Image
- import logging
+ import argparse
- import sys
+ import os
  
- def main(argv):
+ def main():
-   pngFileName = sys.argv[1]
-   baseFileName, _ = pngFileName.rsplit('.')
+   parser = argparse.ArgumentParser(
+       description='Converts input image to raw image and palette data.')
+   parser.add_argument('-f', '--force', action='store_true',
+       help='If output files exist, the tool will overwrite them.')
+   parser.add_argument('input', metavar='INPUT', type=str,
+       help='Input image filename.')
+   parser.add_argument('output', metavar='OUTPUT', type=str,
+       help='Output files basename (without extension).')
+   args = parser.parse_args()
  
-   rawFileName = '%s.raw' % baseFileName
-   palFileName = '%s.pal' % baseFileName
+   inputPath = os.path.abspath(args.input)
+   outputPath = os.path.abspath(args.output)
  
-   image = Image.open(pngFileName)
+   rawFilePath = '%s.raw' % outputPath
+   palFilePath = '%s.pal' % outputPath
  
+   if not os.path.isfile(inputPath):
+     raise SystemExit('Input file does not exists!')
-   with open(palFileName, 'w') as palFile:
-     pal = map(chr, image.getpalette())
-     palFile.write("".join(pal))
  
+   if any(map(os.path.isfile, [rawFilePath, palFilePath])) and not args.force:
+     raise SystemExit('Will not overwrite output files!')
+ 
+   try:
+     image = Image.open(inputPath)
+   except IOError as ex:
+     raise SystemExit('Error: %s.' % ex)
+   else:
+     with open(palFilePath, 'w') as palFile:
+       pal = map(chr, image.getpalette())
+       palFile.write("".join(pal))
+ 
-   with open(rawFileName, 'w') as rawFile:
+     with open(rawFilePath, 'w') as rawFile:
-     w, h = image.size
+       w, h = image.size
-     for y in range(h):
+       for y in range(h):
-       for x in range(w):
+         for x in range(w):
-         pixel = image.getpixel((x,y))
+           pixel = image.getpixel((x,y))
-         rawFile.write(chr(pixel))
+           rawFile.write(chr(pixel))
  
  if __name__ == '__main__':
+   main()
-   FORMAT = '%(levelname)s: %(message)s'
-   logging.basicConfig(format=FORMAT, level=logging.DEBUG)
-   main(sys.argv)
  
Add cmdline options parser and a sanity check.
## Code Before:

import Image
import logging
import sys

def main(argv):
  pngFileName = sys.argv[1]
  baseFileName, _ = pngFileName.rsplit('.')

  rawFileName = '%s.raw' % baseFileName
  palFileName = '%s.pal' % baseFileName

  image = Image.open(pngFileName)

  with open(palFileName, 'w') as palFile:
    pal = map(chr, image.getpalette())
    palFile.write("".join(pal))

  with open(rawFileName, 'w') as rawFile:
    w, h = image.size
    for y in range(h):
      for x in range(w):
        pixel = image.getpixel((x,y))
        rawFile.write(chr(pixel))

if __name__ == '__main__':
  FORMAT = '%(levelname)s: %(message)s'
  logging.basicConfig(format=FORMAT, level=logging.DEBUG)
  main(sys.argv)

## Instruction:
Add cmdline options parser and a sanity check.
## Code After:

import Image
import argparse
import os

def main():
  parser = argparse.ArgumentParser(
      description='Converts input image to raw image and palette data.')
  parser.add_argument('-f', '--force', action='store_true',
      help='If output files exist, the tool will overwrite them.')
  parser.add_argument('input', metavar='INPUT', type=str,
      help='Input image filename.')
  parser.add_argument('output', metavar='OUTPUT', type=str,
      help='Output files basename (without extension).')
  args = parser.parse_args()

  inputPath = os.path.abspath(args.input)
  outputPath = os.path.abspath(args.output)

  rawFilePath = '%s.raw' % outputPath
  palFilePath = '%s.pal' % outputPath

  if not os.path.isfile(inputPath):
    raise SystemExit('Input file does not exists!')

  if any(map(os.path.isfile, [rawFilePath, palFilePath])) and not args.force:
    raise SystemExit('Will not overwrite output files!')

  try:
    image = Image.open(inputPath)
  except IOError as ex:
    raise SystemExit('Error: %s.' % ex)
  else:
    with open(palFilePath, 'w') as palFile:
      pal = map(chr, image.getpalette())
      palFile.write("".join(pal))

    with open(rawFilePath, 'w') as rawFile:
      w, h = image.size
      for y in range(h):
        for x in range(w):
          pixel = image.getpixel((x,y))
          rawFile.write(chr(pixel))

if __name__ == '__main__':
  main()
