/usr/lib/python3/dist-packages/diffoscope/comparators/image.py is in diffoscope 93ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | # -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2015 Chris Lamb <lamby@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import re
import base64
import logging
import subprocess
from diffoscope.config import Config
from diffoscope.tools import tool_required
from diffoscope.tempfiles import get_named_temporary_file
from diffoscope.difference import Difference, VisualDifference
from .utils.file import File
from .utils.command import Command
re_ansi_escapes = re.compile(r'\x1b[^m]*m')
logger = logging.getLogger(__name__)
class Img2Txt(Command):
@tool_required('img2txt')
def cmdline(self):
return [
'img2txt',
'--width', '60',
'--format', 'utf8',
self.path,
]
def filter(self, line):
# Strip ANSI escapes
return re_ansi_escapes.sub('', line.decode('utf-8')).encode('utf-8')
class Identify(Command):
ATTRIBUTES = (
'Image format: %m',
'File size: %b',
'Height: %[height]',
'Width: %[width]',
'Orientation: %[orientation]',
'Compression type: %[compression]',
'Compression quality: %[quality]',
'Colorspace: %[colorspace]',
'Channels: %[channels]',
'Depth: %[depth]',
'Interlace mode: %[interlace]',
'Rendering intent: %[rendering-intent]',
'X resolution: %[resolution.x]',
'Y resolution: %[resolution.y]',
'Resolution units: %[units]',
'Transparency channel enabled: %A',
'Gamma: %[gamma]',
'Number of unique colors: %[colors]',
'Comment: %c',
'EXIF data: %[EXIF:*]',
)
@tool_required('identify')
def cmdline(self):
return [
'identify',
'-format',
'\n'.join(self.ATTRIBUTES),
self.path,
]
@tool_required('compare')
def pixel_difference(image1_path, image2_path):
compared_filename = get_named_temporary_file(suffix='.png').name
try:
subprocess.check_call((
'compare',
image1_path,
image2_path,
'-compose', 'src',
compared_filename,
))
except subprocess.CalledProcessError as e:
# ImageMagick's `compare` will return 1 if images are different
if e.returncode == 1:
pass
with open(compared_filename, 'rb') as f:
content = base64.b64encode(f.read()).decode('utf8')
return VisualDifference('image/png;base64', content, "Pixel difference")
@tool_required('convert')
def flicker_difference(image1_path, image2_path):
compared_filename = get_named_temporary_file(suffix='.gif').name
subprocess.check_call((
'convert',
'-delay', '50',
image1_path,
image2_path,
'-loop', '0',
'-compose', 'difference',
compared_filename,
))
with open(compared_filename, 'rb') as f:
content = base64.b64encode(f.read()).decode('utf8')
return VisualDifference('image/gif;base64', content, "Flicker difference")
@tool_required('identify')
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):
DESCRIPTION = "JPEG images"
FILE_TYPE_RE = 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().compute_visual_diffs and \
same_size(self, other):
try:
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 [
content_diff,
Difference.from_command(
Identify,
self.path,
other.path,
source="Image metadata",
),
]
class ICOImageFile(File):
DESCRIPTION = "Microsoft Windows icon files"
FILE_TYPE_RE = re.compile(r'\bMS Windows icon resource\b')
def compare_details(self, other, source=None):
differences = []
# img2txt does not support .ico files directly so convert to .PNG.
try:
png_a, png_b = [ICOImageFile.convert(x) for x in (self, other)]
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().compute_visual_diffs 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(
Identify,
self.path,
other.path,
source="Image metadata",
))
return differences
@staticmethod
@tool_required('convert')
def convert(file):
result = get_named_temporary_file(suffix='.png').name
subprocess.check_call(('convert', file.path, result))
return result
|