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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
|
# -*- coding: utf-8 -*-
#
# These test cover the Image methods that directly map to C-API function calls.
#
import io
import warnings
from pytest import mark, raises
from wand.color import Color
from wand.exceptions import (MissingDelegateError, OptionError,
WandRuntimeError)
from wand.image import Image
from wand.font import Font
from wand.version import MAGICK_VERSION_NUMBER
def test_adaptive_blur():
with Image(filename='rose:') as img:
was = img.signature
img.adaptive_blur(8, 3)
assert was != img.signature
was = img.signature
img.adaptive_blur(8, 3, channel='red')
assert was != img.signature
def test_adaptive_resize():
with Image(filename='rose:') as img:
img.adaptive_resize(140, 92)
assert 140, 92 == img.size
with Image(filename='rose:') as img:
_, h = img.size
img.adaptive_resize(columns=140)
assert img.size == (140, h)
img.adaptive_resize(rows=92)
assert img.size == (140, 92)
@mark.skipif(MAGICK_VERSION_NUMBER < 0x690,
reason='-adaptive-sharpen fixed in version 6.8.9-2')
def test_adaptive_sharpen():
with Image(width=100, height=100, pseudo='plasma:') as img:
was = img.signature
img.adaptive_sharpen(15, 3)
assert was != img.signature
with Image(width=100, height=100, pseudo='plasma:') as img:
was = img.signature
img.adaptive_sharpen(15, 3, channel='red')
assert was != img.signature
def test_adaptive_threshold():
with Image(filename='rose:') as img:
was = img.signature
offset = 0.1 * img.quantum_range
img.adaptive_threshold(15, 15, offset)
assert was != img.signature
def test_annotate(fx_asset):
from wand.drawing import Drawing
with Image(filename='rose:') as img:
was = img.signature
with Drawing() as ctx:
ctx.font = str(fx_asset.joinpath('League_Gothic.otf'))
ctx.font_size = 32
img.annotate('Hello', ctx, left=10, baseline=img.height-10)
assert was != img.signature
with raises(TypeError):
with Image(filename='rose:') as img:
img.annotate('Hello', 0xDEADBEEF)
def test_auto_gamma():
with Image(filename='rose:') as img:
was = img.signature
img.auto_gamma()
assert was != img.signature
def test_auto_level():
with Image(filename='rose:') as img:
was = img.signature
img.auto_level()
assert was != img.signature
def test_auto_orientation(fx_asset):
with Image(filename=str(fx_asset.joinpath('beach.jpg'))) as img:
# if orientation is undefined nothing should be changed
before = img[100, 100]
img.auto_orient()
after = img[100, 100]
assert before == after
assert img.orientation == 'top_left'
fpath = str(fx_asset.joinpath('orientationtest.jpg'))
with Image(filename=fpath) as original:
with original.clone() as img:
# now we should get a flipped image
assert img.orientation == 'bottom_left'
before = img[100, 100]
img.auto_orient()
after = img[100, 100]
assert before != after
assert img.orientation == 'top_left'
assert img[0, 0] == original[0, -1]
assert img[0, -1] == original[0, 0]
assert img[-1, 0] == original[-1, -1]
assert img[-1, -1] == original[-1, 0]
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Auto Threshold requires ImageMagick-7.0.8.')
def test_auto_threshold():
with Image(filename='rose:') as img:
was = img.signature
img.auto_threshold()
assert was != img.signature
def test_black_threshold():
with Image(filename='rose:') as img:
was = img.signature
img.black_threshold(Color('gray(50%)'))
img.black_threshold('gray(50%)')
assert was != img.signature
with raises(TypeError):
img.white_threshold(0xDEADBEEF)
def test_blue_shift():
with Image(filename='rose:') as img:
was = img.signature
img.blue_shift(1.5)
assert was != img.signature
with raises(TypeError):
img.blue_shift('NaN')
def test_blur():
with Image(filename='rose:') as img:
was = img.signature
img.blur(30, 10)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.blur(30, 10, 'red')
assert was != img.signature
def test_border():
with Image(filename='rose:') as img:
was = img.signature
with Color('red') as color:
img.border(color, 2, 5)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.border('pink', 2, 5)
assert was != img.signature
def test_brightness_contrast():
with Image(filename='rose:') as img:
was = img.signature
img.brightness_contrast(-10.0, 50)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.brightness_contrast(-10.0, 50, 'red')
assert was != img.signature
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason="Canny requires ImageMagick-7.0.8")
def test_canny():
with Image(filename='rose:') as img:
img.transform_colorspace('gray')
was = img.signature
img.canny(1, 3)
assert was != img.signature
def test_caption(fx_asset):
with Image(width=144, height=192, background=Color('#1e50a2')) as img:
font = Font(
path=str(fx_asset.joinpath('League_Gothic.otf')),
color=Color("gold"),
size=12,
antialias=False
)
img.caption(
'Test message',
font=font,
left=5, top=144,
width=134, height=20,
gravity='center'
)
def test_caption_without_font():
with Image(width=144, height=192, background=Color('#1e50a2')) as img:
with raises(TypeError):
img.caption(
'Test message',
left=5, top=144,
width=134, height=20,
gravity='center'
)
def test_charcoal():
with Image(filename='rose:') as img:
img.transform_colorspace('gray')
was = img.signature
img.charcoal(1, 2)
assert was != img.signature
def test_chop():
with Image(filename='rose:') as img:
w, h = img.size
img.chop(10, 10, 10, 10)
assert img.width == (w - 10)
assert img.height == (h - 10)
def test_chop_gravity():
with Image(filename='rose:') as img:
img.chop(width=10, height=10, gravity='south_east')
assert (60, 36, 0, 0) == img.page
with raises(ValueError):
with Image(filename='rose:') as img:
img.chop(x=10, gravity='north')
@mark.skipif(MAGICK_VERSION_NUMBER < 0x709,
reason="Clahe requires Imagemagick-7.0.9.")
def test_clahe():
with Image(filename='rose:') as img:
was = img.signature
img.clahe(10, 10, 128, 3)
assert was != img.signature
def test_clamp():
with Image(filename='rose:') as img:
assert img.clamp(channel='red')
def test_clut():
with Image(filename='rose:') as img:
was = img.signature
with Image(img) as clut:
clut.unique_colors()
clut.flop()
img.clut(clut)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
with Image(img) as clut:
clut.unique_colors()
clut.flop()
img.clut(clut, channel='red')
assert was != img.signature
with raises(TypeError):
img.clut(0xDEADBEEF)
def test_coalesce(fx_asset):
with Image(filename=str(fx_asset.joinpath('nocomments.gif'))) as img1:
with Image(img1) as img2:
img2.coalesce()
assert img1.signature != img2.signature
assert img1.size == img2.size
def test_color_decision_list():
ccc = """
<ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2">
<ColorCorrection id="cc03345">
<SOPNode>
<Slope> 0.9 1.2 0.5 </Slope>
<Offset> 0.4 -0.5 0.6 </Offset>
<Power> 1.0 0.8 1.5 </Power>
</SOPNode>
<SATNode>
<Saturation> 0.85 </Saturation>
</SATNode>
</ColorCorrection>
</ColorCorrectionCollection>
"""
with Image(filename='rose:') as img:
was = img.signature
assert img.color_decision_list(ccc)
assert was != img.signature
with Image(filename='rose:') as img:
assert img.cdl(ccc)
def test_color_map(fx_asset):
with Image(filename=str(fx_asset.joinpath('trim-color-test.png'))) as img:
img.type = 'palette'
assert img[0, 0] == img.color_map(0)
orange = Color('ORANGE')
assert orange == img.color_map(0, orange)
assert orange == img.color_map(0, 'ORANGE')
assert orange == img.color_map(0)
with raises(TypeError):
img.color_map('NaN')
with raises(TypeError):
img.color_map(0, 0xDEADBEEF)
with raises(ValueError):
img.color_map(-1, orange)
def test_color_matrix():
with Image(filename='rose:') as img:
was = img.signature
matrix = [
[0.9, 0.0, 0.0],
[0.0, 0.9, 0.0],
[0.0, 0.0, 1.3]
]
img.color_matrix(matrix)
assert was != img.signature
with raises(TypeError):
img.color_matrix(0xDEADBEEF)
with raises(TypeError):
img.color_matrix((0, 0, 0, 0, 0))
with raises(ValueError):
img.color_matrix([[1, 0], [0, 1, 0]])
@mark.skipif(MAGICK_VERSION_NUMBER < 0x70A,
reason='Color Threshold requires ImageMagick-7.0.10')
def test_color_threshold():
with Image(width=100, height=100, pseudo='plasma:') as img:
was = img.signature
img.color_threshold(start='#000', stop='#ccc')
assert was != img.signature
def test_colorize():
with Image(filename='rose:') as img:
was = img.signature
img.colorize('blue', 'blue')
assert was != img.signature
with raises(TypeError):
img.colorize(0xDEADBEEF, Color('blue'))
with raises(TypeError):
img.colorize(Color('blue'), 0xDEADBEEF)
def test_combine():
with Image() as img:
img.pseudo(width=1, height=1, pseudo='xc:grey100')
img.pseudo(width=1, height=1, pseudo='xc:grey50')
img.pseudo(width=1, height=1, pseudo='xc:grey0')
assert len(img.sequence) == 3
img.colorspace = 'rgb'
img.combine()
assert len(img.sequence) == 1
pixel = img[0, 0]
assert pixel.red > pixel.green > pixel.blue
def test_compare():
with Image(filename="rose:") as img1:
with img1.clone() as img2:
img2.import_pixels(10, 10, 1, 1, 'R', 'char', b'\00')
cmp_img, err = img1.compare(img2, 'absolute',
highlight=Color('orange'),
lowlight=Color('gray90'))
assert cmp_img
assert err >= 0.0
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason="Complex requires ImageMagick-7.0.8.")
def test_complex():
with Image(width=1, height=1, pseudo='xc:gray25') as a:
with Image(width=1, height=1, pseudo='xc:gray50') as b:
a.image_add(b)
a.iterator_reset()
with a.complex('add') as img:
assert a.signature != img.signature
def test_composite():
with Image(filename='rose:') as img:
was = img.signature
with Image(filename='rose:') as fg:
img.composite(fg, 5, 10)
assert img.signature != was
def test_composite_arguments():
with Image(filename='rose:') as img:
base = img.signature
left = base
right = base
with img.clone() as img1:
with Image(width=img.width,
height=img.height,
pseudo='gradient:') as mask:
img1.composite(mask, operator='blend')
left = img1.signature
assert base != left
with img.clone() as img2:
with Image(width=img.width,
height=img.height,
pseudo='gradient:') as mask:
img2.composite(mask, operator='blend', arguments='7,7')
right = img2.signature
assert base != right
assert left != right
def test_composite_gravity():
green = Color('GREEN')
red = Color('RED')
with Image(width=100, height=100, background=green) as src:
with Image(width=10, height=10, background=red) as dst:
src.composite(dst, gravity='east')
assert src[0, 50] == green
assert src[99, 50] == red
def test_composite_channel(fx_asset):
with Image(filename=str(fx_asset.joinpath('beach.jpg'))) as orig:
w, h = orig.size
left = w // 4
top = h // 4
right = left * 3 - 1
bottom = h // 4 * 3 - 1
# List of (x, y) points that shouldn't be changed:
outer_points = [
(0, 0), (0, h - 1), (w - 1, 0), (w - 1, h - 1),
(left, top - 1), (left - 1, top), (left - 1, top - 1),
(right, top - 1), (right + 1, top), (right + 1, top - 1),
(left, bottom + 1), (left - 1, bottom), (left - 1, bottom + 1),
(right, bottom + 1), (right + 1, bottom), (right + 1, bottom + 1)
]
if MAGICK_VERSION_NUMBER < 0x700:
channel_name = 'red'
else:
channel_name = 'default_channels'
with orig.clone() as img:
with Color('black') as color:
with Image(width=w // 2, height=h // 2,
background=color) as cimg:
img.composite_channel(channel_name, cimg, 'copy_red',
w // 4, h // 4)
# These points should be not changed:
for point in outer_points:
assert orig[point] == img[point]
# Inner pixels should lost its red color (red becomes 0)
for point in zip([left, right], [top, bottom]):
with orig[point] as oc:
with img[point] as ic:
assert not ic.red
assert ic.green == oc.green
assert ic.blue == oc.blue
def test_composite_channel_arguments():
channel_name = 'default_channels'
with Image(filename='rose:') as img:
base = img.signature
left = base
right = base
with img.clone() as img1:
with Image(width=img.width,
height=img.height,
pseudo='gradient:') as mask:
img1.composite_channel(channel_name, mask, 'blend')
left = img1.signature
assert base != left
with img.clone() as img2:
with Image(width=img.width,
height=img.height,
pseudo='gradient:') as mask:
img2.composite_channel(channel_name, mask, 'blend',
arguments='7,7')
right = img2.signature
assert base != right
assert left != right
def test_composite_channel_gravity():
green = Color('GREEN')
red = Color('RED')
channel_name = 'default_channels'
with Image(width=100, height=100, background=green) as src:
with Image(width=10, height=10, background=red) as dst:
src.composite_channel(channel_name, dst, 'over', gravity='east')
assert src[0, 50] == green
assert src[99, 50] == red
def test_concat():
with Image(filename='rose:') as img:
img.read(filename='rose:')
with Image(img) as row:
row.concat()
assert row.size == (140, 46)
with Image(img) as row:
row.concat(True)
assert row.size == (70, 92)
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Connected Components requires ImageMagick-7.0.8.')
def test_connected_components(fx_asset):
with Image(filename=str(fx_asset.joinpath('ccobject.png'))) as img:
objects = img.connected_components()
assert 2 == len(objects)
def test_contrast():
with Image(filename='rose:') as img:
was = img.signature
img.contrast()
assert was != img.signature
def test_contrast_stretch():
with Image(filename='rose:') as img:
was = img.signature
img.contrast_stretch(0.15)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.contrast_stretch(0.15, channel='red')
assert was != img.signature
def test_contrast_stretch_user_error():
with Image(filename='rose:') as img:
with raises(TypeError):
img.contrast_stretch('NaN')
with raises(TypeError):
img.contrast_stretch(0.1, 'NaN')
with raises(ValueError):
img.contrast_stretch(0.1, channel='Not a channel')
@mark.skipif(MAGICK_VERSION_NUMBER < 0x70A,
reason='Convex Hull requires ImageMagick-7.0.10.')
def test_convex_hull(fx_asset):
fpath = str(fx_asset.joinpath('horizon_sunset_border2.jpg'))
with Image(filename=fpath) as img:
points = img.convex_hull(background='black')
assert len(points) > 0
def test_crop(fx_asset):
"""Crops in-place."""
with Image(filename=str(fx_asset.joinpath('croptest.png'))) as img:
with img.clone() as cropped:
assert cropped.size == img.size
cropped.crop(100, 100, 200, 200)
assert cropped.size == (100, 100)
with Color('#000') as black:
for row in cropped:
for col in row:
assert col == black
with img.clone() as cropped:
assert cropped.size == img.size
cropped.crop(100, 100, width=100, height=100)
assert cropped.size == (100, 100)
with img.clone() as cropped:
assert cropped.size == img.size
cropped.crop(left=150, bottom=150)
assert cropped.size == (150, 150)
with img.clone() as cropped:
assert cropped.size == img.size
cropped.crop(left=150, height=150)
assert cropped.size == (150, 150)
with img.clone() as cropped:
assert cropped.size == img.size
cropped.crop(-200, -200, -100, -100)
assert cropped.size == (100, 100)
with img.clone() as cropped:
assert cropped.size == img.size
cropped.crop(top=100, bottom=200)
assert cropped.size == (300, 100)
with img.clone() as cropped:
width, height = img.size
assert cropped.crop(left=0, top=0, width=width, height=height)
with raises(ValueError):
img.crop(0, 0, 500, 500)
with raises(ValueError):
img.crop(290, 290, 50, 50)
with raises(ValueError):
img.crop(290, 290, width=0, height=0)
def test_crop_error(fx_asset):
"""Crop errors."""
with Image(filename=str(fx_asset.joinpath('croptest.png'))) as img:
with raises(TypeError):
img.crop(right=1, width=2)
with raises(TypeError):
img.crop(bottom=1, height=2)
def test_crop_gif(tmp_path, fx_asset):
fpath = str(fx_asset.joinpath('nocomments-delay-100.gif'))
with Image(filename=fpath) as img:
with img.clone() as d:
assert d.size == (350, 197)
for s in d.sequence:
assert s.delay == 100
d.crop(50, 50, 200, 150)
d.save(filename=str(tmp_path / '50_50_200_150.gif'))
with Image(filename=str(tmp_path / '50_50_200_150.gif')) as d:
assert len(d.sequence) == 46
assert d.size == (150, 100)
for s in d.sequence:
assert s.delay == 100
def test_crop_gravity(fx_asset):
with Image(filename=str(fx_asset.joinpath('croptest.png'))) as img:
width = int(img.width / 3)
height = int(img.height / 3)
mid_width = int(width / 2)
mid_height = int(height / 2)
with img.clone() as center:
center.crop(width=width, height=height, gravity='center')
assert center[mid_width, mid_height] == Color('black')
with img.clone() as northwest:
northwest.crop(width=width, height=height, gravity='north_west')
assert northwest[mid_width, mid_height] == Color('transparent')
with img.clone() as southeast:
southeast.crop(width=width, height=height, gravity='south_east')
assert southeast[mid_width, mid_height] == Color('transparent')
def test_crop_gravity_error():
with Image(filename='rose:') as img:
with raises(TypeError):
img.crop(gravity='center')
with raises(ValueError):
img.crop(width=1, height=1, gravity='nowhere')
def test_crop_issue367():
with Image(filename='rose:') as img:
expected = img.size
for gravity in ('north_west', 'north', 'north_east',
'west', 'center', 'east',
'south_west', 'south', 'south_east',):
with Image(img) as actual:
actual.crop(width=200, height=200, gravity=gravity)
assert actual.size == expected
def test_cycle_color_map(fx_asset):
with Image(filename=str(fx_asset.joinpath('trim-color-test.png'))) as img:
img.type = 'palette'
lime = img[0, 0]
img.cycle_color_map(1)
assert img[-1, 0] == lime
img.cycle_color_map(-1)
assert img[0, 0] == lime
with raises(TypeError):
img.cycle_color_map('NaN')
def test_deskew():
with Image(filename='rose:') as img:
was = img.signature
img.deskew(0.4 * img.quantum_range)
assert was != img.signature
def test_despeckle():
with Image(filename='rose:') as img:
was = img.signature
img.despeckle()
assert was != img.signature
def test_distort():
"""Distort image."""
with Image(filename='rose:') as img:
was = img.signature
w, h = img.size
img.distort('resize', (w*2, h*2))
neu = img.signature
assert was != neu
# Let's ensure a distorted image with a defined filter doesn't
# match default.
with Image(filename='rose:') as img:
was = img.signature
w, h = img.size
img.distort('resize', (w*2, h*2), filter='lanczos')
assert neu != img.signature
def test_distort_error():
"""Distort image with user error"""
with Image(filename='rose:') as img:
with raises(ValueError):
img.distort('mirror', (1,))
with raises(TypeError):
img.distort('perspective', 1)
def test_edge():
with Image(filename='rose:') as img:
was = img.signature
img.edge(1.5)
assert was != img.signature
def test_emboss():
with Image(filename='rose:') as img:
was = img.signature
img.emboss(1.5, 0.25)
assert was != img.signature
def test_encipher_decipher():
with Image(filename='rose:') as img:
img.depth = 8 # Safety
was = img.signature
img.encipher(passphrase='secret')
assert was != img.signature
img.decipher(passphrase='secret')
assert was == img.signature
def test_enhance():
with Image(filename='rose:') as img:
was = img.signature
img.enhance()
assert was != img.signature
def test_equalize():
with Image(filename='rose:') as img:
was = img.signature
img.equalize()
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.equalize(channel='red')
assert was != img.signature
def test_evaluate():
with Image(filename='hald:3') as img:
with img.clone() as percent_img:
fifty_percent = percent_img.quantum_range * 0.5
percent_img.evaluate('set', fifty_percent)
with percent_img[10, 10] as gray:
assert abs(gray.red - Color('gray50').red) < 0.01
with img.clone() as literal_img:
literal_img.evaluate('divide', 2, channel='red')
with img[0, 0] as org_color:
expected_color = (org_color.red_int8 * 0.5)
with literal_img[0, 0] as actual_color:
assert abs(expected_color - actual_color.red_int8) < 1
def test_evaluate_user_error():
with Image(filename='hald:3') as img:
with raises(ValueError):
img.evaluate(operator='Nothing')
with raises(TypeError):
img.evaluate(operator='set', value='NaN')
with raises(ValueError):
img.evaluate(operator='set', value=1.0, channel='Not a channel')
def test_export_pixels():
with Image(filename='hald:2') as img:
img.depth = 8 # Not need, but want to match import.
data = img.export_pixels(x=0, y=0, width=4, height=1,
channel_map='RGBA', storage='char')
expected = [0x00, 0x00, 0x00, 0xFF,
0x55, 0x00, 0x00, 0xFF,
0xAA, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0xFF]
assert data == expected
# Test Bad value
with raises(TypeError):
img.export_pixels(x='NaN')
with raises(TypeError):
img.export_pixels(y='NaN')
with raises(TypeError):
img.export_pixels(width='NaN')
with raises(TypeError):
img.export_pixels(height='NaN')
with raises(TypeError):
img.export_pixels(channel_map=0xDEADBEEF)
with raises(ValueError):
img.export_pixels(channel_map='NaN')
with raises(ValueError):
img.export_pixels(storage='NaN')
def test_export_pixels_issue_413():
x = 10
y = 10
width = 20
height = 10
with Image(width=50, height=50, background=Color('GREEN')) as img:
export = img.export_pixels(x=x, y=y,
width=width, height=height)
assert export
export = img.export_pixels(x=x, y=y,
width=width + x, height=height + y)
assert export
def test_extent():
with Image(filename='rose:') as img:
with img.clone() as extended:
assert extended.size == img.size
extended.extent(width=500)
assert extended.width == 500
assert extended.height == img.height
with img.clone() as extended:
assert extended.size == img.size
extended.extent(height=500)
assert extended.width == img.width
assert extended.height == 500
with raises(ValueError):
img.extent(width=-10)
with raises(ValueError):
img.extent(height=-10)
def test_extent_gravity():
with Image(filename='rose:') as img:
img.extent(width=10, height=10, gravity='south_east')
assert (10, 10, 0, 0) == img.page
img.extent(width=100, height=100, gravity='center')
assert (100, 100, 0, 0) == img.page
with raises(ValueError):
with Image(filename='rose:') as img:
img.extent(x=10, gravity='north')
def test_features():
with Image(filename='rose:') as img:
cf = img.features(10)
assert 'red' in cf
assert 'contrast' in cf['red']
assert 'horizontal' in cf['red']['contrast']
def test_flip():
with Image(filename='rose:') as img:
with img.clone() as flipped:
flipped.flip()
assert flipped[0, 0] == img[0, -1]
assert flipped[0, -1] == img[0, 0]
assert flipped[-1, 0] == img[-1, -1]
assert flipped[-1, -1] == img[-1, 0]
def test_flop():
with Image(filename='rose:') as img:
with img.clone() as flopped:
flopped.flop()
assert flopped[0, 0] == img[-1, 0]
assert flopped[-1, 0] == img[0, 0]
assert flopped[0, -1] == img[-1, -1]
assert flopped[-1, -1] == img[0, -1]
@mark.fft
def test_forward_fourier_transform():
with Image(filename='rose:') as img:
was = img.signature
img.forward_fourier_transform()
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.fft()
assert was != img.signature
def test_frame():
with Image(filename='rose:') as img:
img.frame(width=4, height=4)
assert img[0, 0] == img[-1, -1]
assert img[-1, 0] == img[0, -1]
with Color('green') as green:
with Image(filename='rose:') as img:
img.frame(matte=green, width=2, height=2)
assert img[0, 0] == green
assert img[-1, -1] == green
with Image(filename='rose:') as img:
img.frame(matte='green', width=2, height=2)
assert img[0, 0] == green
assert img[-1, -1] == green
def test_frame_error():
with Image(filename='rose:') as img:
with raises(TypeError):
img.frame(width='one')
with raises(TypeError):
img.frame(height=3.5)
with raises(TypeError):
img.frame(inner_bevel=None)
with raises(TypeError):
img.frame(outer_bevel='large')
def test_function():
with Image(filename='wizard:') as img:
was = img.signature
img.function(function='polynomial',
arguments=(4, -4, 1))
assert was != img.signature
was = img.signature
img.function(function='sinusoid',
arguments=(1,),
channel='red')
assert was != img.signature
def test_function_error():
with Image(filename='rose:') as img:
with raises(ValueError):
img.function('bad function', 1)
with raises(TypeError):
img.function('sinusoid', 1)
with raises(ValueError):
img.function('sinusoid', (1,), channel='bad channel')
def test_fx():
with Image(width=2, height=2, background=Color('black')) as xc1:
# NavyBlue == #000080
with xc1.fx('0.5019', channel='blue') as xc2:
assert abs(xc2[0, 0].blue - Color('navy').blue) < 0.0001
with Image(width=2, height=1, background=Color('white')) as xc1:
with xc1.fx('0') as xc2:
assert xc2[0, 0].red == 0
def test_fx_error():
with Image() as empty_wand:
with raises(WandRuntimeError):
with empty_wand.fx('8') as _:
pass
with Image(filename='rose:') as xc:
with raises(OptionError):
with xc.fx('NULL'):
pass
with raises(TypeError):
with xc.fx(('p[0,0]',)):
pass
with raises(TypeError):
with xc.fx('p[0,0]', True):
pass
def test_gamma():
# Value under 1.0 is darker, and above 1.0 is lighter
middle_point = 35, 23
with Image(filename='rose:') as img:
with img.clone() as lighter:
lighter.gamma(1.5)
assert img[middle_point].red < lighter[middle_point].red
with img.clone() as darker:
darker.gamma(0.5)
assert img[middle_point].red > darker[middle_point].red
def test_gamma_channel():
# Value under 1.0 is darker, and above 1.0 is lighter
middle_point = 35, 23
with Image(filename='rose:') as img:
with img.clone() as lighter:
lighter.gamma(1.5, channel='red')
assert img[middle_point].red < lighter[middle_point].red
with img.clone() as darker:
darker.gamma(0.5, channel='red')
assert img[middle_point].red > darker[middle_point].red
def test_gamma_user_error():
with Image(filename='rose:') as img:
with raises(TypeError):
img.gamma('NaN;')
with raises(ValueError):
img.gamma(0.0, 'no channel')
def test_gaussian_blur():
with Image(filename='rose:') as img:
was = img.signature
img.gaussian_blur(30, 10)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.gaussian_blur(30, 10, channel='red')
assert was != img.signature
def test_get_image_distortion():
with Image(filename='wizard:') as orig:
with orig.clone() as img:
img.implode(0.5)
err = orig.get_image_distortion(img, 'absolute')
assert err >= 0.0
def test_hald_clut():
with Image(filename='rose:') as img:
was = img.signature
with Image(filename='hald:3') as hald:
hald.gamma(0.367)
img.hald_clut(hald)
assert was != img.signature
was = img.signature
with Image(filename='hald:3') as hald:
hald.gamma(0.367)
img.hald_clut(hald, channel='red')
assert was != img.signature
with raises(TypeError):
img.hald_clut(0xDEADBEEF)
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Hough Lines requires ImageMagick-7.0.8.')
def test_hough_lines(fx_asset):
with Image(filename=str(fx_asset.joinpath('ccobject.png'))) as img:
was = img.signature
img.hough_lines(width=3, height=3)
assert was != img.signature
def test_implode():
with Image(filename='rose:') as img:
was = img.signature
img.implode(amount=1.0)
assert was != img.signature
def test_import_pixels():
data = [0xFF, 0x00, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00]
with Image(width=4, height=1, background=Color('BLACK')) as dst:
dst.depth = 8 # For safety
was = dst.signature
dst.import_pixels(x=0, y=0, width=4, height=1,
channel_map='RGBA', storage='char',
data=data)
assert was != dst.signature
with raises(TypeError):
dst.import_pixels(x='NaN')
with raises(TypeError):
dst.import_pixels(y='NaN')
with raises(TypeError):
dst.import_pixels(width='NaN')
with raises(TypeError):
dst.import_pixels(height='NaN')
with raises(TypeError):
dst.import_pixels(channel_map=0xDEADBEEF)
with raises(ValueError):
dst.import_pixels(channel_map='NaN')
with raises(ValueError):
dst.import_pixels(storage='NaN')
with raises(TypeError):
dst.import_pixels(data=0xDEADBEEF)
with raises(ValueError):
dst.import_pixels(data=[0x00, 0xFF])
def test_import_pixels_issue_413():
x = 10
y = 10
width = 20
height = 10
with Image(width=50, height=50, background=Color('GREEN')) as img:
blank = [0xFF] * 600
img.import_pixels(x=x, y=y,
width=width, height=height,
channel_map='RGB',
data=blank)
assert img
blank = [0xFF] * 1800
img.import_pixels(x=x, y=y,
width=width + x, height=height + y,
channel_map='RGB',
data=blank)
assert img
@mark.fft
def test_inverse_fourier_transform(fx_asset):
magnitude = str(fx_asset.joinpath('ccobject_magnitude.png'))
phase = str(fx_asset.joinpath('ccobject_phase.png'))
with Image(filename=magnitude) as a:
was = a.signature
with Image(filename=phase) as b:
a.inverse_fourier_transform(b)
assert was != a.signature
with Image(filename=magnitude) as a:
with Image(filename=phase) as b:
assert a.ift(b)
with raises(TypeError):
with Image(filename=magnitude) as a:
a.inverse_fourier_transform(0xDEADBEEF)
def test_iterator(fx_asset):
with Image(filename=str(fx_asset.joinpath('animation.gif'))) as img:
length = img.iterator_length()
assert length == 4
img.iterator_reset()
idx = img.iterator_get()
assert idx == 0
while img.iterator_next():
pass
idx = img.iterator_get()
assert idx == 3
img.iterator_first()
idx = img.iterator_get()
assert idx == 0
img.iterator_last()
while img.iterator_previous():
pass
idx = img.iterator_get()
assert idx == 0
img.iterator_set(2)
idx = img.iterator_get()
assert idx == 2
@mark.skipif(MAGICK_VERSION_NUMBER < 0x70B,
reason='Kmeans requires ImageMagick-7.0.11')
def test_kmeans():
with Image(filename='rose:') as img:
was = img.signature
img.kmeans(64)
assert was != img.signature
def test_kurtosis_channel():
with Image(filename='rose:') as img:
r = img.kurtosis_channel('red')
assert len(r) == 2
with raises(ValueError):
img.kurtosis_channel('unknown')
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Kuwahara requires ImageMagick-7.0.8.')
def test_kuwahara():
with Image(filename='rose:') as img:
was = img.signature
img.kuwahara(3.0)
assert was != img.signature
def test_label(fx_asset):
font_path = str(fx_asset.joinpath('League_Gothic.otf'))
with Image(filename='rose:') as img:
was = img.signature
img.label('a', left=0, top=0, font=Font(font_path, 12))
now = img.signature
assert now != was
img.label('b', font=Font(font_path, 12), gravity='south')
assert img.signature != now
with raises(TypeError):
with Image(filename='rose:') as img:
img.label('x')
def test_level():
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust the levels to make this image entirely black
was = img.signature
img.level(black=0.99, white=1.0)
assert was != img.signature
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust the levels to make this image entirely white
was = img.signature
img.level(0, 0.01)
assert was != img.signature
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust the image's gamma to darken its midtones
was = img.signature
img.level(gamma=0.5)
assert was != img.signature
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust the image's gamma to lighten its midtones
was = img.signature
img.level(0, 1, 2.5)
assert was != img.signature
def test_level_channel():
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust each channel level to make it entirely black
was = img.signature
img.level(0.99, 1.0, channel='red')
assert was != img.signature
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust each channel level to make it entirely white
was = img.signature
img.level(0.0, 0.01, channel='green')
assert was != img.signature
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust each channel's gamma to darken its midtones
was = img.signature
img.level(gamma=0.5, channel='blue')
assert was != img.signature
with Image(width=100, height=100, pseudo='gradient:') as img:
# Adjust each channel's gamma to lighten its midtones
was = img.signature
img.level(0, 1, 2.5, 'red')
assert was != img.signature
def test_level_user_error():
with Image(width=100, height=100, pseudo='gradient:') as img:
with raises(TypeError):
img.level(black='NaN')
with raises(TypeError):
img.level(white='NaN')
with raises(TypeError):
img.level(gamma='NaN')
with raises(ValueError):
img.level(channel='404')
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Levelize requires ImageMagick-7.0.8.')
def test_levelize():
with Image(filename='rose:') as img:
was = img.signature
img.levelize(3.0)
assert was != img.signature
def test_linear_stretch():
with Image(width=100, height=100, pseudo='gradient:') as img:
was = img.signature
img.linear_stretch(black_point=0.15,
white_point=0.15)
assert was != img.signature
def test_linear_stretch_user_error():
with Image(width=100, height=100, pseudo='gradient:') as img:
with raises(TypeError):
img.linear_stretch(white_point='NaN',
black_point=0.5)
with raises(TypeError):
img.linear_stretch(white_point=0.5,
black_point='NaN')
def test_liquid_rescale():
with Image(filename='wizard:') as orig:
with orig.clone() as img:
try:
img.liquid_rescale(600, 600)
except MissingDelegateError:
warnings.warn('skip liquid_rescale test; has no LQR delegate')
else:
assert img.size == (600, 600)
@mark.skipif(MAGICK_VERSION_NUMBER < 0x693,
reason='Local Contrast not supported.')
def test_local_contrast():
with Image(filename='rose:') as img:
was = img.signature
img.local_contrast()
assert was != img.signature
def test_mean_channel():
with Image(filename='rose:') as img:
r = img.mean_channel('red')
assert len(r) == 2
with raises(ValueError):
img.mean_channel('unknown')
def test_magnify():
with Image(filename='rose:') as img:
expected = img.width * 2
img.magnify()
assert expected == img.width
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Mean Shift requires ImageMagick-7.0.8.')
def test_mean_shift():
with Image(filename='rose:') as img:
was = img.signature
img.mean_shift(width=5, height=5)
assert was != img.signature
def test_merge_layers():
for method in ['merge', 'flatten', 'mosaic']:
with Image(filename='wizard:') as img1:
with Image(filename='logo:') as img2:
img1.sequence.append(img2)
assert len(img1.sequence) == 2
img1.merge_layers(method)
assert len(img1.sequence) == 1
def test_merge_layers_bad_method():
with Image(filename='rose:') as img:
for method in ('', 'mosaic' 'junk'):
with raises(ValueError):
img.merge_layers(method)
with raises(TypeError):
img.merge_layers(None)
def test_merge_layers_method_flatten():
with Image(width=16, height=16) as img1:
img1.background_color = Color('black')
img1.alpha_channel = False
with Image(width=32, height=32) as img2:
img2.background_color = Color('white')
img2.alpha_channel = False
img2.transform(crop='16x16+8+8')
img1.sequence.append(img2)
img1.merge_layers('flatten')
assert img1.size == (16, 16)
def test_merge_layers_method_merge():
with Image(width=16, height=16) as img1:
img1.background_color = Color('black')
img1.alpha_channel = False
with Image(width=32, height=32) as img2:
img2.background_color = Color('white')
img2.alpha_channel = False
img2.transform(crop='16x16+8+8')
img1.sequence.append(img2)
img1.merge_layers('merge')
assert img1.size == (24, 24)
def test_merge_layers_method_merge_neg_offset():
with Image(width=16, height=16) as img1:
img1.background_color = Color('black')
img1.alpha_channel = False
with Image(width=16, height=16) as img2:
img2.background_color = Color('white')
img2.alpha_channel = False
img2.page = (16, 16, -8, -8)
img1.sequence.append(img2)
img1.merge_layers('merge')
assert img1.size == (24, 24)
def test_merge_layers_method_mosaic():
with Image(width=16, height=16) as img1:
img1.background_color = Color('black')
img1.alpha_channel = False
with Image(width=32, height=32) as img2:
img2.background_color = Color('white')
img2.alpha_channel = False
img2.transform(crop='16x16+8+8')
img1.sequence.append(img2)
img1.merge_layers('mosaic')
assert img1.size == (24, 24)
def test_merge_layers_method_mosaic_neg_offset():
with Image(width=16, height=16) as img1:
img1.background_color = Color('black')
img1.alpha_channel = False
with Image(width=16, height=16) as img2:
img2.background_color = Color('white')
img2.alpha_channel = False
img2.page = (16, 16, -8, -8)
img1.sequence.append(img2)
img1.merge_layers('mosaic')
assert img1.size == (16, 16)
@mark.skipif(MAGICK_VERSION_NUMBER < 0x70A,
reason='Minimum Bounding Box requires ImageMagick-7.0.10.')
def test_minimum_bounding_box():
with Image(filename='wizard:') as img:
img.fuzz = 0.1 * img.quantum_range
img.background_color = 'white'
mbr = img.minimum_bounding_box()
assert img.width > mbr.get('width', img.width)
assert img.height > mbr.get('height', img.height)
def test_mode():
with Image(filename='rose:') as img:
was = img.signature
img.mode(5)
assert was != img.signature
def test_modulate():
with Image(filename='rose:') as img:
was = img.signature
img.modulate(120, 120, 120)
assert was != img.signature
def test_morphology_builtin():
known = []
args = (('erode', 'ring'),
('dilate', 'disk:5'),
('open', 'octagon'),
('smooth', 'rectangle:0x-1'),
('thinning', 'edges'),
('distance', 'euclidean:4,10!'),
('thicken', 'unity:x5'),
('close', 'manhattan:20x25%'),
('hit_and_miss', 'chebyshev:5.0'))
for arg in args:
with Image(filename='rose:') as img:
img.morphology(*arg)
assert img.signature not in known
known.append(img.signature)
with Image(filename='rose:') as img:
with raises(TypeError):
img.morphology(method=0xDEADBEEF)
with raises(TypeError):
img.morphology(method='close',
kernel=0xDEADBEEF)
with raises(TypeError):
img.morphology(method='close',
kernel='1:0',
iterations='p')
def test_morphology_user_defined():
with Image(filename='rose:') as img:
was = img.signature
img.morphology(method='dilate',
kernel='3x3: 0.3,0.6,0.3 0.6,1.0,0.6 0.3,0.6,0.3')
assert was != img.signature
was = img.signature
img.morphology(method='dilate',
kernel='3x3: 0.3,0.6,0.3 0.6,1.0,0.6 0.3,0.6,0.3',
channel='red')
assert was != img.signature
with raises(ValueError):
img.morphology(method='dilate',
kernel='junk:0')
def test_motion_blur():
with Image(filename='rose:') as img:
was = img.signature
img.motion_blur(8, 6, 45)
result = img.signature
assert was != result
was = result
img.motion_blur(8, 6, -45, channel='blue')
assert was != img.signature
def test_negate_default():
with Image(width=100, height=100, pseudo='gradient:') as img:
was = img.signature
img.negate()
assert was != img.signature
assert img.negate(False, 'red')
with raises(ValueError):
img.negate(True, 'unknown')
def test_noise():
with Image(filename='rose:') as img:
was = img.signature
img.noise('gaussian', 1.0)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.noise('gaussian', 1.0, channel='red')
assert was != img.signature
def test_normalize():
with Image(width=100, height=100, pseudo='rose:') as img:
was = img.signature
img.normalize()
assert was != img.signature
with raises(ValueError):
img.normalize('unknown')
def test_normalize_channel():
with Image(width=100, height=100, pseudo='rose:') as img:
was = img.signature
img.normalize('red')
assert was != img.signature
def test_oil_paint():
with Image(filename='rose:') as img:
was = img.signature
img.oil_paint(5)
assert was != img.signature
def test_opaque_paint():
pink = Color('pink')
white = Color('white')
with Image(filename='WIZARD:') as img:
img.opaque_paint(target=white, fill=pink,
fuzz=0.25*img.quantum_range)
assert img[0, 0] == pink
with Image(filename='WIZARD:') as img:
img.opaque_paint(target='white', fill='pink',
fuzz=0.25*img.quantum_range, invert=True)
assert img[0, 0] == white
with Image(filename='WIZARD:') as img:
was = img.signature
img.opaque_paint(target='white', fill='pink',
fuzz=0.25*img.quantum_range, channel='red')
assert was != img.signature
def test_optimize_layers(fx_asset):
with Image(filename=str(fx_asset.joinpath('nocomments.gif'))) as img1:
with Image(img1) as img2:
img2.optimize_layers()
assert img1.signature != img2.signature
assert img1.size == img2.size
def test_optimize_transparency(fx_asset):
with Image(filename=str(fx_asset.joinpath('nocomments.gif'))) as img1:
with Image(img1) as img2:
try:
img2.optimize_transparency()
assert img1.signature != img2.signature
assert img1.size == img2.size
except AttributeError as e:
warnings.warn('MagickOptimizeImageTransparency not '
'present on system. ' + repr(e))
def test_ordered_dither():
with Image(filename='rose:') as img:
was = img.signature
img.ordered_dither('o3x3')
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.ordered_dither('o3x3', channel='red')
assert was != img.signature
def test_parse_meta_geometry():
with Image(filename='rose:') as img:
w, h, x, y = img.parse_meta_geometry('200%')
assert (w, h, x, y) == (140, 92, 0, 0)
with raises(ValueError):
img.parse_meta_geometry('junk')
def test_percent_escape():
with Image(filename='rose:') as img:
assert '3019 70x46' == img.percent_escape('%k %wx%h')
def test_polaroid(fx_asset):
# For testing polaroid method, we can't really identify if somethings
# has changed correctly.
with Image(filename='rose:') as img:
img.polaroid()
with Image(filename='rose:') as img:
img.polaroid(caption='hello')
with Image(filename='rose:') as img:
font = Font(str(fx_asset.joinpath('League_Gothic.otf')), 12,
Color('orange'), True, Color('pink'), 1)
img.polaroid(caption='hello', font=font)
with raises(TypeError):
img.polaroid(caption='hello', font='League_Gothic.otf')
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Polynomial requires ImageMagick-7.0.8.')
def test_polynomial():
with Image(filename='rose:') as img:
was = img.signature
img.polynomial(arguments=(0.5, 1.0))
assert was != img.signature
def test_posterize():
with Image(filename='rose:') as img:
was = img.signature
img.posterize(levels=16, dither='no')
assert was != img.signature
with raises(TypeError):
img.posterize(levels='16')
with raises(ValueError):
img.posterize(levels=16, dither='manhatten')
def test_quantize():
number_colors = 32
with Image(width=100, height=100, pseudo='gradient:') as img:
assert img.colors > number_colors
with Image(width=100, height=100, pseudo='gradient:') as img:
with raises(TypeError):
img.quantize(str(number_colors), 'undefined', 0, True, True)
with raises(TypeError):
img.quantize(number_colors, 0, 0, True, True)
with raises(TypeError):
img.quantize(number_colors, 'undefined', 'depth', True, True)
with raises(TypeError):
img.quantize(number_colors, 'undefined', 0, 1, True)
with raises(TypeError):
img.quantize(number_colors, 'undefined', 0, True, 1)
img.quantize(number_colors, 'undefined', 0, True, True)
assert img.colors <= number_colors
def test_random_threshold():
with Image(filename='rose:') as img:
was = img.signature
img.random_threshold(low=0.4, high=0.6)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.random_threshold(low=0.4, high=0.6, channel='red')
assert was != img.signature
def test_range_channel():
with Image(filename='rose:') as img:
minima, maxima = img.range_channel('red')
assert minima < maxima
with raises(ValueError):
img.range_channel('unknown')
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Range Threshold requires ImageMagick-7.0.8.')
def test_range_threshold():
with Image(filename='rose:') as img:
was = img.signature
img.range_threshold(20, 40, 60, 80)
assert was != img.signature
# Smoke test
img.range_threshold(20)
img.range_threshold(20, 40)
img.range_threshold(20, 40, 60)
def test_region():
with Image(filename='rose:') as src:
w, h, x, y = src.page
with src.region() as dst:
assert (w, h) == dst.size
with src.region(width=w//2, height=h//2, x=x//2, y=y//2) as dst:
assert (w//2, h//2) == dst.size
with Image(filename='rose:') as src:
with src.region(width=10, height=10, gravity='south_east') as dst:
assert (70, 46, 60, 36) == dst.page
assert (10, 10) == dst.size
with raises(ValueError):
with Image(filename='rose:') as img:
with img.region(x=10, gravity='center') as _:
pass
def test_remap():
with Image(filename='rose:') as img:
was = img.signature
with Image(filename='hald:3') as palette:
img.remap(palette)
assert was != img.signature
with raises(TypeError):
img.remap(0xDEADBEEF)
with raises(TypeError):
img.remap(img, method=0xDEADBEEF)
with raises(ValueError):
img.remap(img, method='none')
@mark.parametrize(('density', 'expected_size'), [
((72, 72), (800, 600)),
((36, 36), (400, 300)),
((144, 144), (1600, 1200)),
((None, 36), (800, 300)),
((36, None), (400, 600)),
])
def test_resample(density, expected_size, fx_asset):
"""Resample (Adjust number of pixels at the given density) the image."""
xr, yr = density
with Image(filename=str(fx_asset.joinpath('beach.jpg'))) as img:
img.units = "pixelspercentimeter"
assert img.resolution == (72, 72)
img.resample(xr, yr)
# Expect ``None`` values to match ImageMagick's default 72 resolution.
if xr is None:
xr = 72
if yr is None:
yr = 72
assert img.resolution == (xr, yr)
assert img.size == expected_size
def test_resample_errors():
"""Sampling errors."""
with Image(filename='rose:') as img:
with raises(TypeError):
img.resample(x_res='100')
with raises(TypeError):
img.resample(y_res='100')
with raises(ValueError):
img.resample(x_res=0)
with raises(ValueError):
img.resample(y_res=0)
with raises(ValueError):
img.resample(x_res=-5)
with raises(ValueError):
img.resample(y_res=-5)
@mark.parametrize(('method'), [
('resize'),
('sample'),
])
def test_resize_and_sample(method):
"""Resizes/Samples the image."""
with Image(filename='wizard:') as img:
with img.clone() as a:
assert a.size == (480, 640)
getattr(a, method)(100, 100)
assert a.size == (100, 100)
with img.clone() as b:
assert b.size == (480, 640)
getattr(b, method)(height=100)
assert b.size == (480, 100)
with img.clone() as c:
assert c.size == (480, 640)
getattr(c, method)(width=100)
assert c.size == (100, 640)
@mark.parametrize(('method'), [
('resize'),
('sample'),
])
def test_resize_and_sample_errors(method):
"""Resizing/Sampling errors."""
with Image(filename='rose:') as img:
with raises(TypeError):
getattr(img, method)(width='100')
with raises(TypeError):
getattr(img, method)(height='100')
with raises(ValueError):
getattr(img, method)(width=0)
with raises(ValueError):
getattr(img, method)(height=0)
with raises(ValueError):
getattr(img, method)(width=-5)
with raises(ValueError):
getattr(img, method)(height=-5)
@mark.slow
@mark.parametrize(('method'), [
('resize'),
('sample'),
])
def test_resize_and_sample_gif(method, tmp_path, fx_asset):
fpath = str(fx_asset.joinpath('nocomments-delay-100.gif'))
with Image(filename=fpath) as img:
assert len(img.sequence) == 46
with img.clone() as a:
assert a.size == (350, 197)
assert a.sequence[0].delay == 100
for s in a.sequence:
assert s.delay == 100
getattr(a, method)(175, 98)
a.save(filename=str(tmp_path / '175_98.gif'))
with Image(filename=str(tmp_path / '175_98.gif')) as a:
assert len(a.sequence) == 46
assert a.size == (175, 98)
for s in a.sequence:
assert s.delay == 100
with img.clone() as b:
assert b.size == (350, 197)
for s in b.sequence:
assert s.delay == 100
getattr(b, method)(height=100)
b.save(filename=str(tmp_path / '350_100.gif'))
with Image(filename=str(tmp_path / '350_100.gif')) as b:
assert len(b.sequence) == 46
assert b.size == (350, 100)
for s in b.sequence:
assert s.delay == 100
with img.clone() as c:
assert c.size == (350, 197)
for s in c.sequence:
assert s.delay == 100
getattr(c, method)(width=100)
c.save(filename=str(tmp_path / '100_197.gif'))
with Image(filename=str(tmp_path / '100_197.gif')) as c:
assert len(c.sequence) == 46
assert c.size == (100, 197)
for s in c.sequence:
assert s.delay == 100
def test_roll():
with Image(filename='rose:') as img:
was = img.signature
img.roll(x=-15, y=15)
assert was != img.signature
@mark.slow
def test_rotate(fx_asset):
"""Rotates an image."""
with Image(filename=str(fx_asset.joinpath('rotatetest.gif'))) as img:
assert 150 == img.width
assert 100 == img.height
with img.clone() as cloned:
cloned.rotate(360)
assert img.size == cloned.size
with Color('black') as black:
assert black == cloned[0, 50] == cloned[74, 50]
assert black == cloned[0, 99] == cloned[74, 99]
with Color('white') as white:
assert white == cloned[75, 50] == cloned[75, 99]
with img.clone() as cloned:
cloned.rotate(90)
assert 100 == cloned.width
assert 150 == cloned.height
with Color('black') as black:
with Color('white') as white:
for y, row in enumerate(cloned):
for x, col in enumerate(row):
if y < 75 and x < 50:
assert col == black
else:
assert col == white
with Color('red') as bg:
with img.clone() as cloned:
cloned.rotate(45, bg)
assert 176 <= cloned.width == cloned.height <= 178
assert bg == cloned[0, 0] == cloned[0, -1]
assert bg == cloned[-1, 0] == cloned[-1, -1]
with Color('black') as black:
# Until we implement antialiasing, we need to evaluate
# pixels next to corners.
assert black == cloned[5, 70]
assert black == cloned[36, 39]
assert black == cloned[85, 88]
assert black == cloned[53, 120]
with Color('red') as bg:
with img.clone() as cloned:
cloned.rotate(45, 'red')
assert 176 <= cloned.width == cloned.height <= 178
assert bg == cloned[0, 0] == cloned[0, -1]
assert bg == cloned[-1, 0] == cloned[-1, -1]
with Color('black') as black:
# Until we implement antialiasing, we need to evaluate
# pixels next to corners.
assert black == cloned[5, 70]
assert black == cloned[36, 39]
assert black == cloned[85, 88]
assert black == cloned[53, 120]
@mark.slow
def test_rotate_gif(tmp_path, fx_asset):
fpath = str(fx_asset.joinpath('nocomments-delay-100.gif'))
with Image(filename=fpath) as img:
for s in img.sequence:
assert s.delay == 100
with img.clone() as e:
assert e.size == (350, 197)
e.rotate(90)
for s in e.sequence:
assert s.delay == 100
e.save(filename=str(tmp_path / 'rotate_90.gif'))
with Image(filename=str(tmp_path / 'rotate_90.gif')) as e:
assert e.size == (197, 350)
assert len(e.sequence) == 46
for s in e.sequence:
assert s.delay == 100
def test_rotate_reset_coords():
"""Reset the coordinate frame so to the upper-left corner of
the image is (0, 0) again.
"""
with Image(filename='rose:') as img:
img.rotate(45, reset_coords=True)
img.crop(0, 0, 84, 84)
# There should be no page info from the crop, as everything was
# nulled by the rotate.
assert (0, 0, 0, 0) == img.page
@mark.skipif(MAGICK_VERSION_NUMBER < 0x688,
reason="Not supported until after ImageMagick-6.8.8")
def test_rotational_blur():
with Image(filename='rose:') as img:
was = img.signature
img.rotational_blur(45.0)
now = img.signature
assert was != now
was = now
img.rotational_blur(180, 'blue')
assert was != img.signature
def test_scale():
with Image(filename='rose:') as img:
width, height = img.size
img.scale(2, 3)
assert width*2, height*3 == img.size
def test_selective_blur():
with Image(filename='rose:') as img:
was = img.signature
img.selective_blur(8, 3, 0.1 * img.quantum_range)
assert was != img.signature
was = img.signature
img.selective_blur(8, 3, 0.1 * img.quantum_range, channel='red')
assert was != img.signature
def test_sepia_tone():
with Image(filename='rose:') as img:
was = img.signature
img.sepia_tone(threshold=0.8)
assert was != img.signature
def test_shade():
with Image(filename='rose:') as img:
was = img.signature
img.shade(gray=False, azimuth=10.0, elevation=10.0)
assert was != img.signature
with raises(TypeError):
img.shade(azimuth='hello')
with raises(TypeError):
img.shade(elevation='hello')
def test_shadow():
with Image(filename='rose:') as img:
was = img.size
img.shadow(alpha=5.0, sigma=1.25, x=10, y=10)
assert was != img.size
with raises(TypeError):
img.shadow(alpha='hello')
with raises(TypeError):
img.shadow(sigma='hello')
with raises(TypeError):
img.shadow(x=None)
with raises(TypeError):
img.shadow(y=None)
def test_sharpen():
with Image(filename='rose:') as img:
was = img.signature
img.sharpen(radius=10.0, sigma=2.0)
assert was != img.signature
with raises(TypeError):
img.sharpen(radius='hello')
with raises(TypeError):
img.sharpen(sigma='hello')
with Image(filename='rose:') as img:
was = img.signature
img.sharpen(radius=10.0, sigma=2.0, channel='red')
assert was != img.signature
def test_shave():
with Image(filename='rose:') as img:
was = img.size
img.shave(10, 10)
assert was != img.size
with raises(TypeError):
img.shave(None, 10)
with raises(TypeError):
img.shave(10, None)
def test_shear():
with Image(filename='rose:') as img:
was = img.signature
img.shear(background='green', x=10, y=10)
assert was != img.signature
def test_sigmoidal_contrast():
with Image(filename='rose:') as img:
was = img.signature
img.sigmoidal_contrast(sharpen=True,
strength=3,
midpoint=0.65 * img.quantum_range)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.sigmoidal_contrast(sharpen=True,
strength=3,
midpoint=0.65 * img.quantum_range,
channel='red')
assert was != img.signature
def test_similarity():
with Image(filename='rose:') as img:
with img.clone() as sub_img:
location, diff = img.similarity(sub_img)
assert location['top'] == 0 and location['left'] == 0
assert diff < 0.001
with raises(TypeError):
img.similarity(0xDEADBEEF)
def test_sketch():
with Image(filename='rose:') as img:
was = img.signature
img.sketch(5.0, 3.0, 45.0)
assert was != img.signature
def test_smush():
with Image(filename='rose:') as img:
width, height = img.size
with img.clone() as a:
with img.clone() as b:
a.sequence.append(b)
a.smush(False, 10)
assert a.size == (width * 2 + 10, height)
with img.clone() as a:
with img.clone() as b:
a.sequence.append(b)
a.smush(True, 0)
assert a.size == (width, height * 2)
with raises(TypeError):
img.smush(0xDEADBEEF, '0x0')
def test_solarize():
with Image(filename='rose:') as img:
was = img.signature
img.alpha_channel = 'off' # Needed for IM-7
img.solarize(0.5 * img.quantum_range)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.alpha_channel = 'off' # Needed for IM-7
img.solarize(0.5 * img.quantum_range, channel='red')
assert was != img.signature
def test_sparse_color():
with Image(width=10, height=10, background=Color('WHITE')) as img:
colors = {'#F00': (0, 0), '#00F': (9, 9)}
img.sparse_color('barycentric', colors)
assert img[0, 0] == Color('#F00')
assert img[9, 9] == Color('#00F')
with raises(TypeError):
img.sparse_color(0xDEADBEEF, colors)
with raises(TypeError):
img.sparse_color('barycentric', 0xDEADBEEF)
with raises(TypeError):
img.sparse_color('barycentric', colors, channel_mask='red')
def test_splice():
green = Color('GREEN')
with Image(filename='rose:') as img:
width, height = img.size
img.background_color = green
img.splice(10, 10, 10, 10)
assert width+10, height+10 == img.size
assert img[15, 15] == green
with Image(filename='rose:') as img:
was = img.signature
img.splice(width=10, height=10, gravity='center')
assert img.signature != was
with raises(ValueError):
with Image(filename='rose:') as img:
img.splice(width=10, height=10, x=10, gravity='center')
def test_spread():
with Image(filename='rose:') as img:
was = img.signature
img.spread(8.0)
assert was != img.signature
def test_statistic():
with Image(filename='rose:') as img:
was = img.signature
img.statistic('median', 5, 5)
assert was != img.signature
with Image(filename='rose:') as img:
was = img.signature
img.statistic('median', 5, 5, channel='red')
assert was != img.signature
def test_stegano():
with Image(filename='wizard:') as img:
was = img.signature
with Image(filename='rose:') as watermark:
img.stegano(watermark)
assert was != img.signature
with raises(TypeError):
img.stegano(0xDEADBEEF)
def test_strip(fx_asset):
"""Strips the image of all profiles and comments."""
with Image(filename=str(fx_asset.joinpath('beach.jpg'))) as img:
strio = io.BytesIO()
img.save(file=strio)
len_original = strio.tell()
strio.close()
strio = io.BytesIO()
img.strip()
img.save(file=strio)
len_stripped = strio.tell()
assert len_original > len_stripped
def test_swirl():
with Image(filename='rose:') as img:
was = img.signature
img.swirl(degree=90)
assert was != img.signature
def test_texture():
with Image(filename='rose:') as img:
was = img.signature
with Image(width=1, height=10, pseudo='gradient:') as tile:
img.texture(tile)
assert was != img.signature
with raises(TypeError):
img.texture(0xDEADBEEF)
def test_threshold():
with Image(width=100, height=100, pseudo='gradient:white-black') as img:
top = int(img.height * 0.25)
btm = int(img.height * 0.75)
img.threshold(0.5)
with img[0, top] as white:
assert white.red_int8 == white.green_int8 == white.blue_int8 == 255
with img[0, btm] as black:
assert black.red_int8 == black.green_int8 == black.blue_int8 == 0
with raises(ValueError):
img.threshold(0.5, channel='unknown')
def test_threshold_channel():
with Image(width=100, height=100, pseudo='gradient:white-black') as img:
was = img.signature
img.threshold(0.0, 'red')
img.threshold(0.5, 'green')
img.threshold(1.0, 'blue')
assert was != img.signature
def test_thumbnail():
with Image(filename='rose:') as img:
img.thumbnail(50, 20)
assert (50, 20) == img.size
with Image(filename='rose:') as img:
img.thumbnail(width=50)
assert (50, img.height) == img.size
with Image(filename='rose:') as img:
img.thumbnail(height=20)
assert (img.width, 20) == img.size
def test_tint():
with Image(filename='rose:') as img:
was = img.signature
img.tint('blue', 'blue')
assert was != img.signature
with raises(TypeError):
img.colorize(0xDEADBEEF, Color('blue'))
with raises(TypeError):
img.colorize(Color('blue'), 0xDEADBEEF)
@mark.parametrize(('args', 'kwargs', 'expected_size'), [
((), {'resize': '200%'}, (960, 1280)),
((), {'resize': '200%x100%'}, (960, 640)),
((), {'resize': '1200'}, (1200, 1600)),
((), {'resize': 'x300'}, (225, 300)),
((), {'resize': '400x600'}, (400, 533)),
((), {'resize': '1000x1200^'}, (1000, 1333)),
((), {'resize': '100x100!'}, (100, 100)),
((), {'resize': '400x500>'}, (375, 500)),
((), {'resize': '1200x3000<'}, (1200, 1600)),
((), {'resize': '120000@'}, (300, 400)),
((), {'crop': '300x300'}, (300, 300)),
((), {'crop': '300x300+100+100'}, (300, 300)),
((), {'crop': '300x300-150-150'}, (150, 150)),
(('300x300', '200%'), {}, (600, 600)),
])
def test_transform(args, kwargs, expected_size):
"""Transforms (crops and resizes with geometry strings) the image."""
with Image(filename='wizard:') as img:
assert img.size == (480, 640)
img.transform(*args, **kwargs)
assert img.size == expected_size
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason="Crop by aspect-ration requires ImageMagick-7.0.8")
def test_transform_aspect_crop():
with Image(filename='wizard:') as img:
img.transform(crop='16:9')
assert img.size == (480, 270)
def test_transform_colorspace():
with Image(filename='hald:3') as img:
with raises(TypeError):
img.transform_colorspace(0xDEADBEEF)
with raises(ValueError):
img.transform_colorspace('unknown')
img.transform_colorspace('srgb')
assert img.colorspace == 'srgb'
def test_transform_errors():
"""Tests errors raised by invalid parameters for transform."""
unichar = b'\xe2\x9a\xa0'.decode('utf-8')
with Image(filename='wizard:') as img:
with raises(TypeError):
img.transform(crop=500)
with raises(TypeError):
img.transform(resize=500)
with raises(TypeError):
img.transform(500, 500)
with raises(ValueError):
img.transform(crop=unichar)
with raises(ValueError):
img.transform(resize=unichar)
def test_transform_gif(tmp_path, fx_asset):
src = str(fx_asset / 'nocomments-delay-100.gif')
dst = str(tmp_path / 'test_transform_gif.gif')
with Image(filename=src) as img:
assert len(img.sequence) == 46
assert img.size == (350, 197)
for single in img.sequence:
assert single.delay == 100
img.transform(resize='175x98!')
assert len(img.sequence) == 46
assert img.size == (175, 98)
for single in img.sequence:
assert single.size == (175, 98)
assert single.delay == 100
img.save(filename=dst)
with Image(filename=dst) as gif:
assert len(gif.sequence) == 46
assert gif.size == (175, 98)
for single in gif.sequence:
assert single.size == (175, 98)
assert single.delay == 100
def test_transparent_color(fx_asset):
"""TransparentPaint test
.. versionchanged:: 0.5.0
Alpha channel must be enabled with ``'set'``, previously ``True``.
See docstring in :meth:`wand.image.BaseImage.alpha_channel`.
"""
with Image(filename=str(fx_asset.joinpath('rotatetest.gif'))) as img:
img.alpha_channel = 'set'
with Color('white') as white:
img.transparent_color(white, 0.0, 2, 0)
assert img[75, 50].alpha == 0
assert img[0, 50].alpha == 1.0
with Image(filename=str(fx_asset.joinpath('rotatetest.gif'))) as img:
img.alpha_channel = 'set'
img.transparent_color('white', 0.0, 2, 0)
assert img[75, 50].alpha == 0
assert img[0, 50].alpha == 1.0
def test_transparentize(fx_asset):
with Image(filename=str(fx_asset.joinpath('croptest.png'))) as im:
with Color('transparent') as transparent:
with Color('black') as black:
assert im[99, 100] == transparent
assert im[100, 100] == black
im.transparentize(0.3)
im.clamp()
assert im[99, 100].alpha_int8 == transparent.alpha_int8
with im[100, 100] as c:
assert c.red == c.green == c.blue == 0
assert 0.69 < c.alpha < 0.71
with raises(ValueError):
im.transparentize(-9)
def test_transpose():
with Image(filename='rose:') as img:
was = img.signature
img.transpose()
assert was != img.signature
def test_transverse():
with Image(filename='rose:') as img:
was = img.signature
img.transverse()
assert was != img.signature
def test_trim():
"""Remove transparent area around image."""
with Image(filename='logo:') as img:
old_x, _ = img.size
img.trim()
new_x, _ = img.size
assert new_x < old_x
def test_trim_color():
with Image(filename='wizard:') as img:
size = img.size
img.trim(color='white', fuzz=0.1*img.quantum_range)
assert img.size != size
@mark.skipif(MAGICK_VERSION_NUMBER < 0x709,
reason='Trim by percent-background requires ImagesMagick-7.0.9')
def test_trim_percent_background():
with Image(filename='wizard:') as img:
was = img.size
img.trim(fuzz=0.0, percent_background=0.5, background_color='white')
assert img.size != was
def test_trim_reset_coords():
with Image(filename='logo:') as img:
page = img.page
img.trim(reset_coords=True)
assert page != img.page
def test_unique_colors():
with Image(filename='rose:') as img:
was = img.signature
img.unique_colors()
assert was != img.signature
def test_unsharp_mask():
with Image(filename='rose:') as img:
was = img.signature
alpha = was
with img.clone() as cpy:
cpy.unsharp_mask(1.1, 1, 0.5, 0.001)
alpha = cpy.signature
assert alpha != was
with img.clone() as cpy:
cpy.unsharp_mask(1.1, 1, 0.5, 0.001, channel='red')
assert cpy.signature != alpha
def test_vignette():
with Image(filename='rose:') as img:
was = img.signature
img.vignette(radius=3, sigma=3)
assert was != img.signature
def test_watermark():
"""Adds watermark to an image."""
with Image(filename='wizard:') as img:
was = img.signature
with Image(filename='rose:') as wm:
img.watermark(wm, 0.3)
assert was != img.signature
def test_wave():
with Image(filename='rose:') as img:
was = img.size
img.wave(amplitude=img.height, wave_length=img.width/2)
assert was != img.size
with raises(TypeError):
img.wave(amplitude='img height')
with raises(TypeError):
img.wave(wave_length='img height')
with raises(TypeError):
img.wave(method=0xDEADBEEF)
@mark.skipif(MAGICK_VERSION_NUMBER < 0x708,
reason='Wavelet Denoise requires ImageMagick-7.0.8.')
def test_wavelet_denoise():
with Image(filename='rose:') as img:
was = img.signature
img.wavelet_denoise(0.2, 0.3)
assert was != img.signature
@mark.skipif(MAGICK_VERSION_NUMBER < 0x70B,
reason='White balance requires ImageMagick-7.0.11')
def test_white_balance():
with Image(filename='rose:') as img:
was = img.signature
img.white_balance()
assert was != img.signature
def test_white_threshold():
with Image(filename='rose:') as img:
was = img.signature
img.white_threshold(Color('gray(50%)'))
assert was != img.signature
assert img.white_threshold('gray(50%)')
with raises(TypeError):
img.white_threshold(0xDEADBEEF)
|