[diffoscope] 05/06: Reflow indentation for visual differences.

Maria Glukhova siamezzze-guest at moszumanska.debian.org
Sat Apr 29 19:16:59 CEST 2017


This is an automated email from the git hooks/post-receive script.

siamezzze-guest pushed a commit to branch siamezzze/image-visual-comparison
in repository diffoscope.

commit 2371e5884fd8c6aa07baf53a16c06a82655ae6c7
Author: Maria Glukhova <siamezzze at gmail.com>
Date:   Sat Apr 29 01:27:06 2017 +0300

    Reflow indentation for visual differences.
    
    Move checks into separate functions.
---
 diffoscope/comparators/gif.py   | 49 ++++++++++++++++++-----------
 diffoscope/comparators/image.py | 68 +++++++++++++++++++++++++----------------
 diffoscope/comparators/png.py   | 33 ++++++++++++--------
 3 files changed, 92 insertions(+), 58 deletions(-)

diff --git a/diffoscope/comparators/gif.py b/diffoscope/comparators/gif.py
index fb60416..d29bd43 100644
--- a/diffoscope/comparators/gif.py
+++ b/diffoscope/comparators/gif.py
@@ -27,7 +27,7 @@ from diffoscope.config import Config
 
 from .utils.file import File
 from .utils.command import Command
-from .image import pixel_difference, flicker_difference, get_image_size
+from .image import pixel_difference, flicker_difference, same_size
 
 logger = logging.getLogger(__name__)
 
@@ -48,9 +48,18 @@ class Gifbuild(Command):
         return line
 
 @tool_required('identify')
-def is_image_static(image_path):
-    return subprocess.check_output(('identify', '-format',
-                                    '%n', image_path)) == b'1'
+def is_image_static(image):
+    try:
+        return subprocess.check_output((
+            'identify',
+            '-format', '%n',
+            image.path)) == b'1'
+    except subprocess.CalledProcessError:
+        return False
+
+def can_compose_gif_images(image1, image2):
+    return same_size(image1, image2) and is_image_static(image1) and \
+                is_image_static(image2)
 
 class GifFile(File):
     RE_FILE_TYPE = re.compile(r'^GIF image data\b')
@@ -63,21 +72,25 @@ class GifFile(File):
             source='gifbuild',
         )
         differences = [gifbuild_diff]
-        if (gifbuild_diff is not None) and Config().html_output and \
-                (get_image_size(self.path) == get_image_size(other.path)):
+        if gifbuild_diff is not None and Config().html_output and \
+                can_compose_gif_images(self, other):
             try:
-                own_size = get_image_size(self.path)
-                other_size = get_image_size(other.path)
-                self_static = is_image_static(self.path)
-                other_static = is_image_static(other.path)
-                if (own_size == other_size) and self_static and other_static:
-                    logger.debug('Generating visual difference for %s and %s',
-                                 self.path, other.path)
-                    content_diff = Difference(None, self.path, other.path,
-                                              source='Image content')
-                    content_diff.add_visuals([pixel_difference(self.path, other.path),
-                                             flicker_difference(self.path, other.path)])
-                    differences.append(content_diff)
+                logger.debug(
+                    'Generating visual difference for %s and %s',
+                    self.path,
+                    other.path,
+                )
+                content_diff = Difference(
+                    None,
+                    self.path,
+                    other.path,
+                    source='Image content',
+                )
+                content_diff.add_visuals([
+                    pixel_difference(self.path, other.path),
+                    flicker_difference(self.path, other.path),
+                ])
+                differences.append(content_diff)
             except subprocess.CalledProcessError:  #noqa
                 pass
         return differences
diff --git a/diffoscope/comparators/image.py b/diffoscope/comparators/image.py
index 6ba7353..f6e36ef 100644
--- a/diffoscope/comparators/image.py
+++ b/diffoscope/comparators/image.py
@@ -115,23 +115,34 @@ def get_image_size(image_path):
     return subprocess.check_output(('identify', '-format',
                                     '%[h]x%[w]', image_path))
 
+def same_size(image1, image2):
+    try:
+        return get_image_size(image1.path) == get_image_size(image2.path)
+    except subprocess.CalledProcessError:  # noqa
+        return False
+
 class JPEGImageFile(File):
     RE_FILE_TYPE = re.compile(r'\bJPEG image data\b')
 
     def compare_details(self, other, source=None):
-        content_diff = Difference.from_command(Img2Txt, self.path, other.path,
-                                               source='Image content')
-        if (content_diff is not None) and Config().html_output:
+        content_diff = Difference.from_command(
+            Img2Txt,
+            self.path,
+            other.path,
+            source="Image content",
+        )
+        if content_diff is not None and Config().html_output and \
+                same_size(self, other):
             try:
-                own_size = get_image_size(self.path)
-                other_size = get_image_size(other.path)
-                if own_size == other_size:
-                    logger.debug('Generating visual difference for %s and %s',
-                                 self.path, other.path)
-                    content_diff.add_visuals([
-                        pixel_difference(self.path, other.path),
-                        flicker_difference(self.path, other.path)
-                    ])
+                logger.debug(
+                    'Generating visual difference for %s and %s',
+                    self.path,
+                    other.path,
+                )
+                content_diff.add_visuals([
+                    pixel_difference(self.path, other.path),
+                    flicker_difference(self.path, other.path),
+                ])
             except subprocess.CalledProcessError:  # noqa
                 pass
         return [
@@ -156,21 +167,24 @@ class ICOImageFile(File):
         except subprocess.CalledProcessError:  # noqa
             pass
         else:
-            content_diff = Difference.from_command(Img2Txt, png_a, png_b,
-                                                   source='Image content')
-            if (content_diff is not None) and Config().html_output:
-                try:
-                    own_size = get_image_size(self.path)
-                    other_size = get_image_size(other.path)
-                    if own_size == other_size:
-                        logger.debug('Generating visual difference for %s and %s',
-                                     self.path, other.path)
-                        content_diff.add_visuals([
-                            pixel_difference(self.path, other.path),
-                            flicker_difference(self.path, other.path)
-                        ])
-                except subprocess.CalledProcessError:  # noqa
-                    pass
+            content_diff = Difference.from_command(
+                Img2Txt,
+                png_a,
+                png_b,
+                source="Image content",
+            )
+            if content_diff is not None and Config().html_output and \
+                    same_size(self, other):
+                if get_image_size(self.path) == get_image_size(other.path):
+                    logger.debug(
+                        'Generating visual difference for %s and %s',
+                        self.path,
+                        other.path,
+                    )
+                    content_diff.add_visuals([
+                        pixel_difference(self.path, other.path),
+                        flicker_difference(self.path, other.path),
+                    ])
             differences.append(content_diff)
 
         differences.append(Difference.from_command(
diff --git a/diffoscope/comparators/png.py b/diffoscope/comparators/png.py
index a24a8f4..521fbb0 100644
--- a/diffoscope/comparators/png.py
+++ b/diffoscope/comparators/png.py
@@ -28,7 +28,7 @@ from diffoscope.config import Config
 
 from .utils.file import File
 from .utils.command import Command
-from .image import pixel_difference, flicker_difference, get_image_size
+from .image import pixel_difference, flicker_difference, same_size
 
 logger = logging.getLogger(__name__)
 
@@ -50,18 +50,25 @@ class PngFile(File):
     def compare_details(self, other, source=None):
         sng_diff = Difference.from_command(Sng, self.path, other.path, source='sng')
         differences = [sng_diff]
-        if (sng_diff is not None) and Config().html_output:
+        if sng_diff is not None and Config().html_output and \
+                same_size(self, other):
             try:
-                own_size = get_image_size(self.path)
-                other_size = get_image_size(other.path)
-                if own_size == other_size:
-                    logger.debug('Generating visual difference for %s and %s',
-                                 self.path, other.path)
-                    content_diff = Difference(None, self.path, other.path,
-                                              source='Image content')
-                    content_diff.add_visuals([pixel_difference(self.path, other.path),
-                                             flicker_difference(self.path, other.path)])
-                    differences.append(content_diff)
-            except subprocess.CalledProcessError:  #noqa
+                logger.debug(
+                    'Generating visual difference for %s and %s',
+                    self.path,
+                    other.path,
+                )
+                content_diff = Difference(
+                    None,
+                    self.path,
+                    other.path,
+                    source="Image content",
+                )
+                content_diff.add_visuals([
+                    pixel_difference(self.path, other.path),
+                    flicker_difference(self.path, other.path),
+                ])
+                differences.append(content_diff)
+            except subprocess.CalledProcessError:  # noqa
                 pass
         return differences

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list