← Back to team overview

phatch-dev team mailing list archive

[Merge] lp:~kenseehart/phatch/PGM_bugfix_472978_test into lp:phatch

 

Ken Seehart has proposed merging lp:~kenseehart/phatch/PGM_bugfix_472978_test into lp:phatch.

    Requested reviews:
    stani (stani)


Added verification that the file extension is appropriate for the format of the file content, on all output files.

-- 
https://code.launchpad.net/~kenseehart/phatch/PGM_bugfix_472978_test/+merge/19929
Your team Phatch Developers is subscribed to branch lp:phatch.
=== modified file 'phatch/lib/imtools.py'
--- phatch/lib/imtools.py	2009-10-06 05:47:25 +0000
+++ phatch/lib/imtools.py	2010-02-23 01:48:16 +0000
@@ -673,10 +673,8 @@
     elif format == 'GIF':
         return convert(image, 'P', palette=Image.ADAPTIVE)
     elif format == 'PBM':
-        if image.mode in ['P', 'CMYK', 'YCbCr']:
-            return image.convert('RGB')
-        if image.mode in ['LA']:
-            return image.convert('L')
+        if image.mode != '1':
+            return image.convert('1')
     elif format == 'PCX':
         if image.mode in ['RGBA', 'CMYK', 'YCbCr']:
             return image.convert('RGB')
@@ -688,9 +686,7 @@
         if image.mode in ['RGBA', 'YCbCr']:
             return image.convert('RGB')
     elif format == 'PGM':
-        if image.mode in ['P', 'CMYK', 'YCbCr']:
-            return image.convert('RGB')
-        if image.mode in ['LA']:
+        if image.mode !='L':
             return image.convert('L')
     elif format == 'PPM':
         if image.mode in ['P', 'CMYK', 'YCbCr']:

=== modified file 'tests/run_test.py'
--- tests/run_test.py	2009-10-06 23:07:02 +0000
+++ tests/run_test.py	2010-02-23 01:48:16 +0000
@@ -134,6 +134,7 @@
         default='',
         help='Command line options to pass to phatch'
     )
+
     options, args = parser.parse_args()
     if not options.no_execute and not os.path.exists(options.input):
         logging.error(
@@ -235,6 +236,14 @@
             report.info('Errors:\n\t%s' % '\n\t'.join(errors))
         else:
             logging.info('No errors')
+
+    for image in os.listdir(options.output):
+        path1 = os.path.join(options.output, image)
+        if os.path.exists(path1):
+            ok, reason = utils.verify_image_type(path1)
+            if not ok:
+                report.info('Format/Ext Mismatch: %s' % reason)
+    
     if options.compare:
         utils.create_path(config.OUT_DIFF)
         new = []

=== modified file 'tests/test_suite/utils.py'
--- tests/test_suite/utils.py	2009-10-06 01:07:49 +0000
+++ tests/test_suite/utils.py	2010-02-23 01:48:16 +0000
@@ -23,6 +23,7 @@
 import filecmp
 import os
 import shutil
+import imghdr, Image
 
 def system_path(path):
     """Convert a path string into the correct form"""
@@ -70,6 +71,38 @@
         return '%s_%s' % (name, index)
     return name
 
+def verify_image_type(name):
+    """Check that file extension matches the content
+    if successful: ok = True, reason = None
+    else: ok = False, reason = string describing error
+    """
+    altext = {
+        'jpeg': ['jpg', 'jpe'],
+        'tiff': ['tif'],
+        'eps': ['ps'],
+        'pgm': ['ppm'], # PIL bug: PIL saves greyscale PPM as PGM
+        'pbm': ['ppm'], # PIL bug: PIL saves black and white PPM as PBM
+        }
+    
+    # first use opinion of imghdr (python standard library)
+    t = imghdr.what(name)
+    ext = os.path.splitext(name)[1][1:]
+
+    if t is None:
+        # Get PILs opinion
+        # Known types support by PIL but not imghdr: im, pcx, jpeg(CMYK)
+        try:
+            im = Image.open(name)
+            t = im.format.lower()
+        except IOError:
+            return False, 'Content type not recognized in %s' % name
+        
+    if ext!=t and ext not in altext.get(t, []):
+        return False, 'Content type %s does not match extension in %s' % (t,name)
+    else:
+        return True, None
+    
+
 def compare(file1, file2):
     """Compare two files"""
     return filecmp.cmp(file1, file2)


Follow ups