def test_nonetype(self): # Fails if the _delegate function doesn't return a valid function xres = IFDRational(72) yres = IFDRational(72) self.assertIsNotNone(xres._val) self.assertIsNotNone(xres.numerator) self.assertIsNotNone(xres.denominator) self.assertIsNotNone(yres._val) self.assertTrue(xres and 1) self.assertTrue(xres and yres)
def test_nonetype(): # Fails if the _delegate function doesn't return a valid function xres = IFDRational(72) yres = IFDRational(72) assert xres._val is not None assert xres.numerator is not None assert xres.denominator is not None assert yres._val is not None assert xres and 1 assert xres and yres
def test_ifd_rational_save(self): for libtiff in (True, False): TiffImagePlugin.WRITE_LIBTIFF = libtiff im = hopper() out = self.tempfile('temp.tiff') res = IFDRational(301, 1) im.save(out, dpi=(res, res), compression='raw') reloaded = Image.open(out) self.assertEqual(float(IFDRational(301, 1)), float(reloaded.tag_v2[282]))
def test_nonetype(self): " Fails if the _delegate function doesn't return a valid function" xres = IFDRational(72) yres = IFDRational(72) self.assert_(xres._val is not None) self.assert_(xres.numerator is not None) self.assert_(xres.denominator is not None) self.assert_(yres._val is not None) self.assert_(xres and 1) self.assert_(xres and yres)
def test_sanity(self): self._test_equal(1, 1, 1) self._test_equal(1, 1, Fraction(1, 1)) self._test_equal(2, 2, 1) self._test_equal(1.0, 1, Fraction(1, 1)) self._test_equal(Fraction(1, 1), 1, Fraction(1, 1)) self._test_equal(IFDRational(1, 1), 1, 1) self._test_equal(1, 2, Fraction(1, 2)) self._test_equal(1, 2, IFDRational(1, 2))
def test_sanity(): _test_equal(1, 1, 1) _test_equal(1, 1, Fraction(1, 1)) _test_equal(2, 2, 1) _test_equal(1.0, 1, Fraction(1, 1)) _test_equal(Fraction(1, 1), 1, Fraction(1, 1)) _test_equal(IFDRational(1, 1), 1, 1) _test_equal(1, 2, Fraction(1, 2)) _test_equal(1, 2, IFDRational(1, 2))
def test_ifd_rational_save(tmp_path): methods = (True, False) if not features.check("libtiff"): methods = (False, ) for libtiff in methods: TiffImagePlugin.WRITE_LIBTIFF = libtiff im = hopper() out = str(tmp_path / "temp.tiff") res = IFDRational(301, 1) im.save(out, dpi=(res, res), compression="raw") with Image.open(out) as reloaded: assert float(IFDRational(301, 1)) == float(reloaded.tag_v2[282])
def test_write_metadata(self): """ Test metadata writing through the python code """ img = Image.open("Tests/images/hopper.tif") f = self.tempfile("temp.tiff") img.save(f, tiffinfo=img.tag) with Image.open(f) as loaded: original = img.tag_v2.named() reloaded = loaded.tag_v2.named() for k, v in original.items(): if isinstance(v, IFDRational): original[k] = IFDRational(*_limit_rational(v, 2**31)) elif isinstance(v, tuple) and isinstance(v[0], IFDRational): original[k] = tuple( IFDRational(*_limit_rational(elt, 2**31)) for elt in v) ignored = [ "StripByteCounts", "RowsPerStrip", "PageNumber", "StripOffsets" ] for tag, value in reloaded.items(): if tag in ignored: continue if isinstance(original[tag], tuple) and isinstance( original[tag][0], IFDRational): # Need to compare element by element in the tuple, # not comparing tuples of object references self.assert_deep_equal( original[tag], value, "{} didn't roundtrip, {}, {}".format( tag, original[tag], value), ) else: self.assertEqual( original[tag], value, "{} didn't roundtrip, {}, {}".format( tag, original[tag], value), ) for tag, value in original.items(): if tag not in ignored: self.assertEqual(value, reloaded[tag], "%s didn't roundtrip" % tag)
def test_ifd_rational_save(self): methods = (True, False) if "libtiff_encoder" not in dir(Image.core): methods = (False, ) for libtiff in methods: TiffImagePlugin.WRITE_LIBTIFF = libtiff im = hopper() out = self.tempfile("temp.tiff") res = IFDRational(301, 1) im.save(out, dpi=(res, res), compression="raw") reloaded = Image.open(out) self.assertEqual(float(IFDRational(301, 1)), float(reloaded.tag_v2[282]))
def test_read_metadata(self): with Image.open("Tests/images/hopper_g4.tif") as img: self.assertEqual( { "YResolution": IFDRational(4294967295, 113653537), "PlanarConfiguration": 1, "BitsPerSample": (1, ), "ImageLength": 128, "Compression": 4, "FillOrder": 1, "RowsPerStrip": 128, "ResolutionUnit": 3, "PhotometricInterpretation": 0, "PageNumber": (0, 1), "XResolution": IFDRational(4294967295, 113653537), "ImageWidth": 128, "Orientation": 1, "StripByteCounts": (1968, ), "SamplesPerPixel": 1, "StripOffsets": (8, ), }, img.tag_v2.named(), ) self.assertEqual( { "YResolution": ((4294967295, 113653537), ), "PlanarConfiguration": (1, ), "BitsPerSample": (1, ), "ImageLength": (128, ), "Compression": (4, ), "FillOrder": (1, ), "RowsPerStrip": (128, ), "ResolutionUnit": (3, ), "PhotometricInterpretation": (0, ), "PageNumber": (0, 1), "XResolution": ((4294967295, 113653537), ), "ImageWidth": (128, ), "Orientation": (1, ), "StripByteCounts": (1968, ), "SamplesPerPixel": (1, ), "StripOffsets": (8, ), }, img.tag.named(), )
def test_write_metadata(self): """ Test metadata writing through the python code """ img = Image.open('Tests/images/hopper.tif') f = self.tempfile('temp.tiff') img.save(f, tiffinfo=img.tag) loaded = Image.open(f) original = img.tag_v2.named() reloaded = loaded.tag_v2.named() for k, v in original.items(): if type(v) == IFDRational: original[k] = IFDRational(*_limit_rational(v, 2**31)) if type(v) == tuple and \ type(v[0]) == IFDRational: original[k] = tuple([IFDRational( *_limit_rational(elt, 2**31)) for elt in v]) ignored = ['StripByteCounts', 'RowsPerStrip', 'PageNumber', 'StripOffsets'] for tag, value in reloaded.items(): if tag in ignored: continue if (type(original[tag]) == tuple and type(original[tag][0]) == IFDRational): # Need to compare element by element in the tuple, # not comparing tuples of object references self.assert_deep_equal(original[tag], value, "%s didn't roundtrip, %s, %s" % (tag, original[tag], value)) else: self.assertEqual(original[tag], value, "%s didn't roundtrip, %s, %s" % (tag, original[tag], value)) for tag, value in original.items(): if tag not in ignored: self.assertEqual( value, reloaded[tag], "%s didn't roundtrip" % tag)
def test_read_metadata(self): img = Image.open('Tests/images/hopper_g4.tif') self.assertEqual( { 'YResolution': IFDRational(4294967295, 113653537), 'PlanarConfiguration': 1, 'BitsPerSample': (1, ), 'ImageLength': 128, 'Compression': 4, 'FillOrder': 1, 'RowsPerStrip': 128, 'ResolutionUnit': 3, 'PhotometricInterpretation': 0, 'PageNumber': (0, 1), 'XResolution': IFDRational(4294967295, 113653537), 'ImageWidth': 128, 'Orientation': 1, 'StripByteCounts': (1968, ), 'SamplesPerPixel': 1, 'StripOffsets': (8, ) }, img.tag_v2.named()) self.assertEqual( { 'YResolution': ((4294967295, 113653537), ), 'PlanarConfiguration': (1, ), 'BitsPerSample': (1, ), 'ImageLength': (128, ), 'Compression': (4, ), 'FillOrder': (1, ), 'RowsPerStrip': (128, ), 'ResolutionUnit': (3, ), 'PhotometricInterpretation': (0, ), 'PageNumber': (0, 1), 'XResolution': ((4294967295, 113653537), ), 'ImageWidth': (128, ), 'Orientation': (1, ), 'StripByteCounts': (1968, ), 'SamplesPerPixel': (1, ), 'StripOffsets': (8, ) }, img.tag.named())
def _test_equal(self, num, denom, target): t = IFDRational(num, denom) self.assertEqual(target, t) self.assertEqual(t, target)
def _test_equal(num, denom, target): t = IFDRational(num, denom) assert target == t assert t == target
def test_ranges(): for num in range(1, 10): for denom in range(1, 10): assert IFDRational(num, denom) == IFDRational(num, denom)