image
chromatic.image
ansi2img(arr, /, font=uf.VGA437, font_size=16, *, fg_default=(170, 170, 170), bg_default=(0, 0, 0))
Render an ANSI array as an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
color_chain or 2D array-like
|
2D ANSI string array. |
required |
font
|
FontArgType
|
Font to render the ANSI strings with. |
VGA437
|
font_size
|
int
|
Font size in pixels. |
16
|
fg_default
|
tuple[int, int, int] | tuple[int, int, int, int]
|
Default foreground color of rendered text. |
(170, 170, 170)
|
bg_default
|
tuple[int, int, int] | tuple[int, int, int, int]
|
Default background color of rendered text, and the fill color of the base canvas. |
(0, 0, 0)
|
Returns:
| Name | Type | Description |
|---|---|---|
ansi_img |
Image
|
The rendered ANSI array as an |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input ANSI array is empty. |
See Also
img2ansi : Create an ANSI array from an input image, font, and character set.
Source code in chromatic/image/_array.py
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 | |
ansi_quantize(img, ansi_type)
Color-quantize an RGB array into ANSI 4-bit or 8-bit color space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
RGBArray
|
Input image in RGB format. |
required |
ansi_type
|
type[ansicolor4Bit | ansicolor8Bit]
|
ANSI color format to map the quantized image to. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
quantized |
RGBArray
|
The image with RGB values transformed into ANSI color space. |
Source code in chromatic/image/_array.py
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 | |
ansify(img, /, font=uf.VGA437, font_size=16, *, factor=200, char_set=None, sort_glyphs=True, ansi_type=None, equalize=False, fg=(170, 170, 170), bg=(0, 0, 0))
Source code in chromatic/image/_array.py
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 | |
ascii2img(s, /, font=uf.VGA437, font_size=16, *, fg=(0, 0, 0), bg=(255, 255, 255))
Render a literal string as an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The ASCII string to convert into an image. |
required |
font
|
FontArgType
|
Font to use for rendering the ASCII characters. |
VGA437
|
font_size
|
int
|
Font size in pixels for the rendered ASCII characters. |
16
|
fg
|
tuple[int, int, int]
|
Foreground (text) color. |
(0, 0, 0)
|
bg
|
tuple[int, int, int]
|
Background color. |
(255, 255, 255)
|
Returns:
| Name | Type | Description |
|---|---|---|
ascii_img |
Image
|
A |
See Also
img2ascii : Convert an image into an ASCII string.
Source code in chromatic/image/_array.py
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 | |
contrast_stretch(img, percentile=(2, 98))
Rescale the intensities of an RGB image using linear contrast stretching.
Balances contrast across both lightness and color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
RGBArray
|
|
required |
percentile
|
tuple[int, int]
|
|
(2, 98)
|
Returns:
| Name | Type | Description |
|---|---|---|
eq_img |
RGBArray
|
|
See Also
equalize_white_point
Source code in chromatic/image/_array.py
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 | |
equalize_white_point(img)
Apply histogram equalization to the L-channel (lightness) in LAB color space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
RGBArray
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
eq_img |
RGBArray
|
|
See Also
contrast_stretch
Source code in chromatic/image/_array.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
get_font_key(font)
Obtain a unique tuple pair from a FreeTypeFont object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
font
|
FreeTypeFont
|
The FreeTypeFont object from which to derive a key. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
A tuple containing the font family and font name. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the font key cannot be generated due to missing fields. |
Source code in chromatic/image/_array.py
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 | |
get_font_object(font, *, retpath=False)
cached
get_font_object(font: _tp.FontArgType, *, retpath: tp.Literal[False] = False) -> ImageFont.FreeTypeFont
get_font_object(font: _tp.FontArgType, *, retpath: tp.Literal[True]) -> str
get_font_object(font: _tp.FontArgType, *, retpath: bool) -> ImageFont.FreeTypeFont | str
Return a FreeTypeFont object or its filepath.
The result is cached to prevent FreeType from consuming excessive resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
font
|
FontArgType
|
FreeTypeFont, UserFont, or string. |
required |
retpath
|
bool
|
Return filepath instead of FreeTypeFont object |
False
|
Returns:
| Type | Description |
|---|---|
FreeTypeFont or str
|
FreeTypeFont object, or filepath (if |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input type is unsupported. |
Source code in chromatic/image/_array.py
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 | |
img2ansi(img, /, font=uf.VGA437, factor=200, char_set=None, sort_glyphs=True, ansi_type=None, equalize=False, bg=None, *, outarray=False)
img2ansi(img: str | os.PathLike[str] | _tp.RGBImageLike, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., ansi_type: tp.Optional[core.AnsiColorParam] = ..., equalize: bool | tp.Literal['white_point'] = ..., bg: tp.Optional[_tp.Int3Tuple | str] = ..., *, outarray: tp.Literal[False] = False) -> core.color_chain | list[core.color_chain]
img2ansi(img: _tp.RGBArray, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., ansi_type: tp.Optional[core.AnsiColorParam] = ..., equalize: bool | tp.Literal['white_point'] = ..., bg: tp.Optional[_tp.Int3Tuple | str] = ..., *, outarray: tp.Literal[True]) -> _tp.ShapedNDArray[tuple[int, int], np.void]
img2ansi(img: _tp.RGBArray3d, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., ansi_type: tp.Optional[core.AnsiColorParam] = ..., equalize: bool | tp.Literal['white_point'] = ..., bg: tp.Optional[_tp.Int3Tuple | str] = ..., *, outarray: tp.Literal[True]) -> _tp.ShapedNDArray[tuple[int, int, int], np.void]
img2ansi(img: str | os.PathLike[str] | Image.Image, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., ansi_type: tp.Optional[core.AnsiColorParam] = ..., equalize: bool | tp.Literal['white_point'] = ..., bg: tp.Optional[_tp.Int3Tuple | str] = ..., *, outarray: tp.Literal[True]) -> tp.Union[_tp.ShapedNDArray[tuple[int, int], np.void], _tp.ShapedNDArray[tuple[int, int, int], np.void]]
Convert an image to an ANSI array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
str | PathLike[str] | RGBImageLike
|
Base image or path to image being convert into ANSI. |
required |
font
|
FontArgType
|
Font to use for glyph comparisons and representation. |
VGA437
|
factor
|
int
|
Length of each line in characters per line in |
200
|
char_set
|
str
|
The literal string or sequence of strings to use for greyscale interpolation and visualization. If None (default), the character set will be determined based on the 'font' parameter. |
None
|
sort_glyphs
|
(True, False, -1)
|
Specifies to sort Glyph bitmasks obtained from 'font' are compared when sorting the string.
|
True
|
ansi_type
|
AnsiColorParam
|
ANSI color format to map the RGB values to. Can be 4-bit, 8-bit, or 24-bit ANSI color space. If 4-bit or 8-bit, the RGB array will be color-quantized into ANSI color space; if 24-bit, uses the RGB colors of the image; if |
None
|
equalize
|
(True, False, white_point)
|
Apply contrast equalization to the input image. If True, performs contrast stretching; if 'white_point', applies white-point equalization. |
True
|
bg
|
sequence of ints or RGBArray
|
Background color |
None
|
outarray
|
bool
|
If True, an ndarray is returned instead of a color_chain object. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
ansi_array |
`color_chain` or ``ndarray[tuple[int, int], dtype[void]]``
|
The ANSI-converted image. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
ansi2img : Render an ANSI array as an image. img2ascii : Used to obtain the base ASCII characters.
Source code in chromatic/image/_array.py
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 | |
img2ascii(img, /, font=uf.VGA437, factor=200, char_set=None, sort_glyphs=True, *, outarray=False)
img2ascii(img: str | os.PathLike[str] | _tp.RGBImageLike, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., *, outarray: tp.Literal[False] = False) -> str | list[str]
img2ascii(img: _tp.RGBArray, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., *, outarray: tp.Literal[True]) -> _tp.ShapedNDArray[tuple[int, int], np.str_]
img2ascii(img: _tp.RGBArray3d, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., *, outarray: tp.Literal[True]) -> _tp.ShapedNDArray[tuple[int, int, int], np.str_]
img2ascii(img: str | os.PathLike[str] | Image.Image, /, font: _tp.FontArgType = ..., factor: int = ..., char_set: tp.Optional[str] = ..., sort_glyphs: bool | tp.Literal[-1] = ..., *, outarray: tp.Literal[True]) -> tp.Union[_tp.ShapedNDArray[tuple[int, int], np.str_], _tp.ShapedNDArray[tuple[int, int, int], np.str_]]
Convert an image to a multiline ASCII string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
str | PathLike[str] | RGBImageLike
|
Base image being converted to ASCII. |
required |
font
|
FontArgType
|
Font to use for glyph comparisons and representation. |
VGA437
|
factor
|
int
|
Length of each line in characters per line in |
200
|
char_set
|
Iterable[str]
|
Characters to be mapped to greyscale values of 'img'. |
None
|
sort_glyphs
|
(True, False, -1)
|
Specifies to sort Glyph bitmasks obtained from 'font' are compared when sorting the string.
|
True
|
outarray
|
bool
|
If True, returns the raw chararray. Otherwise, returns a string or list of strings. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
output_str |
str
|
Characters from |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
See Also
ascii2img : Render an ASCII string as an image.
Source code in chromatic/image/_array.py
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 | |
otsu_mask(img)
Source code in chromatic/image/_array.py
1392 1393 1394 1395 1396 1397 1398 1399 | |
read_ans(buf, /, fallback=None)
Source code in chromatic/image/_array.py
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 | |
render_ans(buf, /, fallback=None, font=None, font_size=16, *, bg_default=(0, 0, 0))
Return an image render of an ANS file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Literal ANSI text. |
required |
fallback
|
tuple[int, int]
|
Defaults to |
None
|
font
|
FontArgType
|
Font to draw the image. Overrides SAUCE record if present. |
None
|
font_size
|
int
|
Font size in pixels. |
16
|
bg_default
|
tuple[int, int, int] or tuple[int, int, int, int]
|
Background color to use as a fallback when ANSI SGR has none. |
(0, 0, 0)
|
Source code in chromatic/image/_array.py
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 | |
render_font_char(c, /, font, size=(24, 24), fill=(255, 255, 255))
Render a one-character string as an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
str
|
Character to be rendered. |
required |
font
|
FontArgType
|
Font to use for rendering. |
required |
size
|
tuple[int, int]
|
Size of the bounding box to use for the output image, in pixels. |
(24, 24)
|
fill
|
tuple[int, int, int]
|
The color to fill the character. |
(255, 255, 255)
|
Returns:
| Name | Type | Description |
|---|---|---|
Image |
The character rendered in the given font. |
Raises:
| Type | Description |
|---|---|
ValueError : If the input string is longer than a single character.
|
|
Source code in chromatic/image/_array.py
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 | |
render_font_str(s, /, font)
Render a string as an image using the specified font.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string to render. |
required |
font
|
FontArgType
|
The font to use for rendering. |
required |
Returns:
| Type | Description |
|---|---|
ImageType
|
An image of the rendered string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string is empty. |
Source code in chromatic/image/_array.py
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 | |
reshape_ansi(s, /, shape, flags=0)
Return the string padded for a grid with dims shape.
The output string represents a terminal render after stateful transitions have been applied.
Cursor codes and '\r' are consumed and resolved to character emplacements,
and null character cells are translated to whitespace (0x20).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
|
required |
shape
|
tuple[int, int]
|
Shape of the output as (width, height). Must be 2D. |
required |
flags
|
int
|
Additonal flags for state transitions. See |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
str
|
Reshaped string with ANSI escape state transitions applied. |
Source code in chromatic/image/_array.py
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 | |
scale_saturation(img, alpha=None)
Source code in chromatic/image/_array.py
345 346 347 348 349 350 351 | |
shuffle_char_set(chars)
Flatten chars into a list and return the randomly shuffled string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chars
|
Iterable[str]
|
Iterable of characters (or strings, which will be flattened). |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in chromatic/image/_array.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
ascii_printable()
Source code in chromatic/image/_curses.py
40 41 | |
backtrans_cp437(x, /, keys=None)
Translate cp437 graphical chars back into control chars
Source code in chromatic/image/_curses.py
26 27 28 29 30 31 32 | |
cp437_printable()
Return a string containing all graphical characters in code page 437
Source code in chromatic/image/_curses.py
35 36 37 | |
translate_cp437(x, /, ignore=())
Translate control chars (0x1-0x1F, 0x7F) into cp437 graphical chars
Source code in chromatic/image/_curses.py
20 21 22 23 | |
get_glyph_masks(font, /, char_set=None, *, dist_transform=False)
get_glyph_masks(font: _tp.FontArgType, /, char_set: abc.Sequence[str] | None = ...) -> dict[str, _tp.GlyphArray[np.uint8]]
get_glyph_masks(font: _tp.FontArgType, /, char_set: abc.Sequence[str] | None = ..., *, dist_transform: L[False]) -> dict[str, _tp.GlyphArray[np.uint8]]
get_glyph_masks(font: _tp.FontArgType, /, char_set: abc.Sequence[str] | None = ..., *, dist_transform: L[True]) -> dict[str, _tp.GlyphArray[np.float64]]
Source code in chromatic/image/_glyph.py
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 | |
sort_glyphs(s, /, font, reverse=False)
Source code in chromatic/image/_glyph.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
ttf_extract_codepoints(fp, /, **kwargs)
Source code in chromatic/image/_glyph.py
89 90 91 92 93 94 95 | |