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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for Linux/x86 (vers 7 December 2008), see www.w3.org" />
<title>RMagick 0.0.0: How to use RMagick</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<meta name="GENERATOR" content="Quanta Plus" />
<meta name="Copyright" content="Copyright (C) 2006 by Timothy P. Hunter" />
<link rel="stylesheet" type="text/css" href="css/doc.css" />
<script type="text/javascript" src="scripts/doc.js"></script>
<style type="text/css">
/*<![CDATA[*/
/* Styles local to this page. */
#index {
margin-left: 100px;
margin-right: 100px;
}
#index h4 {
position: relative;
left: -100px;
padding-left: 5px;
background-image: url(ex/images/graydient230x6.gif);
background-repeat: repeat-y;
color: black;
}
/*
* The "ilist" class identifies ImageList methods in the index.
*/
.ilist {
font-weight: bold;
}
.ilist a:link {
font-weight: bold;
text-decoration: none;
}
.ilist a:visited {
font-weight: bold;
color: #7f7fff;
}
.ilist a:hover {
text-decoration: underline;
}
.ilist a:active {
background-color: blue;
color: white;
text-decoration: underline;
}
#axes {
margin-left:100px;
margin-right:100px;
margin-bottom: 1em;
position:relative;
}
#axes div {
position:absolute;
top:0;
left: 260px;
}
#rubyname {
margin-left: auto;
margin-right:auto;
width:300px;
margin-bottom:1em;
}
#footnotes {
border-top: thin solid black;
padding-top: 1em;????
}
#footnotes h5 {
display: none;
}
.sup {
vertical-align: super;
font-size: x-small;
}
/*]]>*/
</style>
</head>
<body>
<h6 id="header">RMagick 0.0.0 User's Guide and Reference</h6>
<div class="nav">« <a href="index.html">Prev</a> | <a href="index.html">Contents</a> | <a href="imusage.html">Next</a> »</div>
<h1>How to use RMagick</h1>
<div id="toc">
<h2>Table of Contents</h2>
<ul style="margin-left: 15px; padding-top: 1em">
<li><a href="#basics">Basic concepts</a></li>
<li><a href="#reading">Reading, writing, and creating images</a></li>
<li><a href="#displaying">Displaying images</a></li>
<li><a href="#modifying">Examining and modifying images</a></li>
<li><a href="#marshaling">Marshaling images</a></li>
<li><a href="#drawing_on">Drawing on and adding text to images</a></li>
<li><a href="#more">Where to go from here</a></li>
</ul>
</div>
<h2 id="basics">Basic concepts</h2>
<p>
Let's look at the RMagick equivalent of "Hello, world". This program reads an image file named "Cheetah.jpg" and displays<span class="sup"
><a href="#display">1</a></span
>
it on your monitor
</p>
<pre class="example">
1. require "rmagick"
2. include Magick
3.
4. cat = ImageList.new("Cheetah.jpg")
5. cat.display
6. exit
</pre
>
<p class="example_cutline">
MS Windows users note: The
<span style="font-style: normal">display</span> method does not work on native MS Windows. You must use an external viewer to view the images you create.
</p>
<p>
Line 1 requires <span class="sup"><a href="#rubygems">2</a></span> the RMagick.rb file, which defines the <b>Magick</b> module. The Magick module contains
3 major classes, <b>ImageList</b>, <b>Image</b>, and <b>Draw</b>. This section - <em>Basic Concepts</em> - describes the ImageList and Image classes. The
Draw class is explained in the
<em><a href="#drawing">Drawing on and adding text to images</a></em>
section, below.
</p>
<p>
The statement on line 5 creates an <em>imagelist</em> object and initializes it by reading the <code>Cheetah.jpg</code> file in the current directory.
Line 6 sends the <code>display</code> method to the object. When you send <code>display</code> to an imagelist, it causes all the images in the imagelist
to be displayed on the default X Window screen. In this case, the <code>display</code> method makes a picture of a cheetah appear on your monitor.
</p>
<p>
Type this program in and try running it now. The
<code>Cheetah.jpg</code> file is in the <code>ex/images</code> subdirectory where you installed the RMagick documentation.
</p>
<p>
The Image and ImageList classes are closely related. An
<em>Image</em> object describes one image or one frame in an image with multiple frames. (An animated GIF or a Photoshop image with multiple layers are
examples of images with multiple frames.) You can create a image object from an image file such as a GIF, PNG, or JPEG. You can create a image from
scratch by specifying its dimensions. You can write an image to disk, display it on a screen, change its size or orientation, convert it to another
format, or otherwise modify it using one of over 100 methods.
</p>
<p>
An <em>ImageList</em> object is a <em>list</em> of images. It contains zero or more images and a <em>scene number</em>. The scene number indicates the
<em>current image</em>. The ImageList class includes methods that operate on all the images in the list. Also, with a very few exceptions, any method
defined in the Image class can be used as well. Since Image methods always operate on a single image, when an Image method is sent to an imagelist, the
ImageList class sends the method to the current image, that is, the image specified by the scene number.
</p>
<p>
The ImageList class is a subclass of the Array class, so you can use most Array methods to change the images in the imagelist. For example, you can use
the <code><<</code> method to add an image to the list.
</p>
<p>Going back to the example, let's make one modification.</p>
<pre class="example">
1. require "rmagick"
2. include Magick
3.
4. cat = ImageList.new("Cheetah.jpg")
5. smallcat = cat.minify
6. smallcat.display
7. exit
</pre
>
<p>
The difference is the statement on line 5. This statement sends the
<code>minify</code> method to the imagelist. The <code>minify</code> method is an Image method that reduces the size of an image to half its original
size. Remember, since <code>minify</code> is an Image method, the ImageList class sends <code>minify</code> to the current (and only) image. The return
value is a new image, half the size of the original.
</p>
<p>
Line 6 demonstrates the Image class's <code>display</code>
method, which displays a single image on the X Window screen.
<code>Image#display</code> makes a picture of a (in this case, small) cheetah appear on your monitor.
</p>
<p>Here's how to write the small cheetah to a file in GIF format.</p>
<pre class="example">
1. require "rmagick"
2. include Magick
3.
4. cat = ImageList.new("Cheetah.jpg")
5. smallcat = cat.minify
6. smallcat.display
7. smallcat.write("Small-Cheetah.gif")
8. exit
</pre
>
<p>
The statement on line 7 writes the image to a file. Notice that the filename extension is <code>gif</code>. When writing images, ImageMagick uses the
filename extension to determine what image format to write. In this example, the <code>Small-Cheetah.gif</code> file will be in the GIF format. Notice how
easy it is to covert an image from one format to another? (For more details, see <a href="imusage.html#formats">Image formats and filenames</a>.)
</p>
<p>
So why, in the previous example, did I create <code>cat</code> as an ImageList object containing just one image, instead of creating an Image object? No
reason, really. When you only have one image to deal with, imagelists and images are pretty much interchangeable.
</p>
<p>
<em><b>Note:</b></em> In most cases, an Image method does not modify the image to which it is sent. Instead, the method returns a new image, suitably
modified. For example, the <a href="image3.html#resize"><code>resize</code></a> method returns a new image, sized as specified. The receiver image is
unaltered. (Following the Ruby convention, when a method alters the receiver object, the method name ends with "!". For example, the
<a href="image3.html#resize_bang"><code>resize!</code></a> method resizes the receiver in place.)
</p>
<h2 id="reading">Reading, writing, and creating images</h2>
<p>
You've already seen that you can create an imagelist and initialize it by specifying the name of an image file as the argument to
<a href="ilist.html#new"><code>ImageList.new</code></a
>. In fact, <code>new</code> can take any number of file name arguments. If the file contains a single image, <code>new</code> reads the file, creates an
image, and adds it to the imagelist. If the file is a multi-frame image file, <code>new</code> adds an image for each frame or layer in the file. Lastly,
<code>new</code> changes the scene number to point to the last image in the imagelist. In the simple case, <code>new</code> reads a single image from a
file and sets the scene number to 0.
</p>
<p>
You can also create an image from scratch by calling
<a href="image1.html#new"><code>Image.new</code></a
>. This method takes 2 or 3 arguments. The first argument is the number of columns in the new image (its width). The second argument is the number of rows
(its height). If present, the 3rd argument is a <a href="struct.html#fill"><code>Fill</code></a> object. To add a "scratch" image to an imagelist, call
<a href="ilist.html#new_image"><code>ImageList#new_image</code></a
>. This method calls <code>Image.new</code>, adds the new image to the imagelist, and sets the scene number to point to the new image. Scratch images are
good for <a href="#drawing">drawing</a> on or creating images by <a href="image1.html#composite">compositing</a>.
</p>
<p>
Like many other methods in the Image and ImageList classes,
<code>Image.new</code> accepts an optional block that can be used to set additional optional parameters. If the block is present,
<code>Image.new</code> creates a <a href="info.html">parameter object</a> and yields to the block in the scope of that object. You set the parameters by
calling attribute setter methods defined in the parameter object's class. For example, you can set the background color of a new image to red with the
<code>background_color=</code> method, as shown here:
</p>
<pre class="example">
require "rmagick"
include Magick
# Create a 100x100 red image.
f = Image.new(100,100) { |options| options.background_color = "red" }
f.display
exit
</pre
>
<p>Within the parameter block you must use <code>self</code> so that Ruby knows that this statement is a method call, not an assignment to a variable.</p>
<p>
You can create an image by capturing it from the XWindow screen using
<a href="image1.html#capture"><code>Image.capture</code></a
>. This method can capture the root window, a window identified by name or ID number, or perform an interactive capture whereby you designate the desired
window by clicking it or by drawing a rectangle on the screen with your mouse.
</p>
<p>
Both the Image class and the ImageList class have
<code>write</code> methods. Both accept a single argument, the name of the file to be written.
<a href="image3.html#write"><code>Image#write</code></a> simply writes the image to a file. Like the <code>Image#read</code> method,
<code>write</code> yields to an optional block that you can use to set parameters that control how the image is written.
</p>
<p>
If an ImageList object contains only one image, then
<a href="ilist.html#write"><code>ImageList#write</code></a> is the same as <code>Image#write</code>. However, if the imagelist contains multiple images
and the file format (determined by the file name extension, as I mentioned earlier) supports multi-frame images, <code>Image#write</code> will
automatically create a multi-frame image file.
</p>
<p>
For example, the following program reads three GIF files and then uses
<code>ImageList#write</code> to combine all the images in those files (remember, each input file can contain multiple images) into one animated GIF file.
</p>
<pre class="example">
#!/usr/bin/env ruby -w
require "rmagick"
anim = ImageList.new("start.gif", "middle.gif", "finish.gif")
anim.write("animated.gif")
exit
</pre
>
<h2 id="displaying">Displaying images</h2>
<p>
RMagick defines 3 methods for displaying images and imagelists. Both the Image class and the ImageList class have a <code>display</code> method. The
<a href="image1.html#display"><code>Image#display</code></a> method displays the image on the default X Window screen. For imagelists with just one image,
<a href="ilist.html#display"><code>ImageList#display</code></a> is identical to <code>Image#display</code>. However, if the imagelist contains multiple
images, <code>ImageList#display</code> displays each of the images in turn. With both methods, right-clicking the display window will produce a menu of
other options.
</p>
<p>
The <a href="ilist.html#animate"><code>ImageList#animate</code></a> method repeatedly cycles through all the images in an imagelist, displaying each one
in turn. You can control the speed of the animation with the <a href="ilist.html#delay_eq"><code>ImageList#delay=</code></a> method.
</p>
<h2 id="modifying">Examining and modifying images</h2>
<p>
Once you've created an image or imagelist, what can you do with it? The Image and ImageList classes define over 100 methods for examining and modifying
images, both individually and in groups. Remember, unless the ImageList class defines a method with the same name, you can send any method defined in the
Image class to an instance of the ImageList class. The ImageList class sends the method to the current image and returns the result.
</p>
<p>
The methods can be classified into the following broad groups. ImageList method descriptions look like <span class="ilist">this</span>. Some of the listed
methods are not available in some releases of ImageMagick. See the method documentation for details.
</p>
<div id="index">
<h4>Utility methods</h4>
<dl>
<dt><a href="image1.html#spaceship"><=></a></dt>
<dd>Compare 2 images</dd>
<dt class="ilist"><a href="ilist.html#spaceship"><=></a></dt>
<dd class="ilist">Compare 2 imagelists</dd>
<dt><a href="image1.html#aref">[ ]</a></dt>
<dd>Reference an image property</dd>
<dt><a href="image1.html#aset">[ ]=</a></dt>
<dd>Set an image property</dd>
<dt><a href="image1.html#add_profile">add_profile</a></dt>
<dd>Add an ICC, IPTC, or generic profile</dd>
<dt><a href="image1.html#changed_q">changed?</a></dt>
<dd>Has the image been changed?</dd>
<dt><a href="image1.html#channel">channel</a></dt>
<dd>Extract a color channel from the image</dd>
<dt><a href="image1.html#compare_channel">compare_channel</a></dt>
<dd>Compare one or more channels between two images</dd>
<dt><a href="image1.html#channel_depth">channel_depth</a></dt>
<dd>Return the depth of the specified channel or channels</dd>
<dt><a href="image1.html#channel_extrema">channel_extrema</a></dt>
<dd>Return the maximum and minimum intensity values for one or more channels in the image</dd>
<dt><a href="image1.html#check_destroyed">check_destroyed</a></dt>
<dd>Raise an exception if the image has been destroyed</dd>
<dt><a href="image1.html#clone">clone</a></dt>
<dd>Return a shallow copy of the image</dd>
<dt class="ilist"><a href="ilist.html#clone">clone</a></dt>
<dd class="ilist">Return a shallow copy of the imagelist</dd>
<dt><a href="image1.html#color_histogram">color_histogram</a></dt>
<dd>Count the number of times each unique color appears in the image</dd>
<dt><a href="image1.html#combine">combine</a></dt>
<dd>Combines the grayscale value of the pixels in each image to form a new image.</dd>
<dt><a href="image1.html#copy">copy</a></dt>
<dd>Return a deep copy of the image</dd>
<dt class="ilist"><a href="ilist.html#copy">copy</a></dt>
<dd class="ilist">Return a deep copy of the imagelist</dd>
<dt><a href="image1.html#delete_profile">delete_profile</a></dt>
<dd>Delete an ICC, IPTC, or generic profile</dd>
<dt><a href="image1.html#destroy_bang">destroy!</a></dt>
<dd>Return all memory associated with the image to the system</dd>
<dt><a href="image1.html#destroyed_q">destroyed?</a></dt>
<dd>Has the image has been destroyed?</dd>
<dt><a href="image1.html#difference">difference</a></dt>
<dd>Compute the difference between two images</dd>
<dt><a href="image1.html#distortion_channel">distortion_channel</a></dt>
<dd>Compare one or more channels to a reconstructed image</dd>
<dt><a href="image1.html#dup">dup</a></dt>
<dd>Return a shallow copy of the image</dd>
<dt class="ilist"><a href="ilist.html#dup">dup</a></dt>
<dd class="ilist">Return a shallow copy of the imagelist</dd>
<dt><a href="image2.html#each_pixel">each_pixel</a></dt>
<dd>Iterate over all the pixels in the image</dd>
<dt><a href="image2.html#each_profile">each_profile</a></dt>
<dd>Iterate over all the profiles associated with the image</dd>
<dt><a href="image2.html#export_pixels">export_pixels</a></dt>
<dd>Extract pixel data from the image into an array</dd>
<dt>
<a href="image2.html#export_pixels_to_str">export_pixels_to_str</a>
</dt>
<dd>Extract pixel data from the image into a string</dd>
<dt>
<a href="image2.html#find_similar_region">find_similar_region</a>
</dt>
<dd>Search for a rectangle matching the target</dd>
<dt>
<a href="image2.html#get_exif_by_entry">get_exif_by_entry</a>,
<a href="image2.html#get_exif_by_number">get_exif_by_number</a>
</dt>
<dd>Get one or more EXIF property values for the image</dd>
<dt><a href="image2.html#get_pixels">get_pixels</a></dt>
<dd>Copy a region of pixels from the image</dd>
<dt><a href="image2.html#gray_q">gray?</a></dt>
<dd>Are all the pixels in the image gray?</dd>
<dt><a href="image2.html#histogram_q">histogram?</a></dt>
<dd>Does the image have <= 1024 unique colors??</dd>
<dt><a href="image2.html#import_pixels">import_pixels</a></dt>
<dd>Replace pixels in the image with pixel data from an array</dd>
<dt><a href="image2.html#monochrome_q">monochrome?</a></dt>
<dd>Are all the pixels in the image either black or white?</dd>
<dt><a href="image2.html#opaque_q">opaque?</a></dt>
<dd>Are all the pixels in the image opaque?</dd>
<dt><a href="image3.html#palette_q">palette?</a></dt>
<dd>Is the image a PseudoClass type image with 256 colors or less?</dd>
<dt><a href="image3.html#preview">preview</a></dt>
<dd>Show the effect of a transformation method on the image</dd>
<dt><a href="image3.html#profile_bang">profile!</a></dt>
<dd>Add or remove an ICM, IPTC, or generic profile from the image</dd>
<dt><a href="image3.html#properties">properties</a></dt>
<dd>Iterate over all the properties associated with the image</dd>
<dt><a href="image3.html#separate">separate</a></dt>
<dd>Construct a grayscale image for each channel specified</dd>
<dt><a href="image3.html#set_channel_depth">set_channel_depth</a></dt>
<dd>Set the specified channel's depth</dd>
<dt><a href="image3.html#signature">signature</a></dt>
<dd>Compute the 64-byte message signature for the image</dd>
<dt><a href="image3.html#store_pixels">store_pixels</a></dt>
<dd>Replace a region of pixels in the image</dd>
<dt><a href="image3.html#strip_bang">strip!</a></dt>
<dd>Strip the image of all comments and profiles</dd>
<dt><a href="image3.html#sync_profiles">sync_profiles</a></dt>
<dd>Synchronize the image properties with the image profiles</dd>
<dt><a href="image3.html#unique_colors">unique_colors</a></dt>
<dd>Construct an image from the unique colors</dd>
<dt><a href="image3.html#view">view</a></dt>
<dd>Access pixels by their coordinates.</dd>
</dl>
<h4>Reduce the number of colors</h4>
<dl>
<dt>
<a href="image1.html#compress_colormap_bang">compress_colormap!</a>
</dt>
<dd>Remove duplicate or unused entries in the image's colormap</dd>
<dt><a href="image2.html#ordered_dither">ordered_dither</a></dt>
<dd>Dither the image to a predefined pattern</dd>
<dt><a href="image3.html#posterize">posterize</a></dt>
<dd>Reduce the number of colors for a "poster" effect</dd>
<dt><a href="image3.html#quantize">quantize</a></dt>
<dd>Choose a fixed number of colors to represent the image</dd>
<dt class="ilist"><a href="ilist.html#quantize">quantize</a></dt>
<dd class="ilist">Choose a fixed number of colors to represent all the images in the imagelist</dd>
<dt><a href="image3.html#remap">remap</a></dt>
<dd>Change the colors in the image to the colors in a reference image.</dd>
<dt class="ilist"><a href="ilist.html#remap">remap</a></dt>
<dd class="ilist">Change the images in the imagelist to use a common color map.</dd>
</dl>
<h4>Resize</h4>
<dl>
<dt><a href="image1.html#change_geometry">change_geometry</a></dt>
<dd>Compute a new constrained size for the image</dd>
<dt><a href="image2.html#liquid_rescale">liquid_rescale</a></dt>
<dd>Rescale image with seam-carving</dd>
<dt><a href="image2.html#magnify">magnify</a></dt>
<dd>Double the size of the image</dd>
<dt><a href="image2.html#minify">minify</a></dt>
<dd>Halve the size of the image</dd>
<dt><a href="image3.html#resample">resample</a></dt>
<dd>Resample the image to the specified horizontal and vertical resolution</dd>
<dt><a href="image3.html#resize">resize</a></dt>
<dd>Resize the image using a filter</dd>
<dt><a href="image1.html#resize_to_fill">resize_to_fill</a></dt>
<dd>Resize and crop while retaining the aspect ratio</dd>
<dt><a href="image3.html#resize_to_fit">resize_to_fit</a></dt>
<dd>Resize the image retaining the aspect ratio</dd>
<dt><a href="image3.html#sample">sample</a></dt>
<dd>Resize the image using pixel sampling</dd>
<dt><a href="image3.html#scale">scale</a></dt>
<dd>Resize the image</dd>
<dt><a href="image3.html#thumbnail">thumbnail</a></dt>
<dd>Quickly create a thumbnail of the image</dd>
</dl>
<h4>Change colors or opacity</h4>
<dl>
<dt>
<a href="image1.html#color_fill_to_border">color_fill_to_border</a>
</dt>
<dd>Change the color of neighboring pixels. Stop at the image's border color.</dd>
<dt><a href="image1.html#color_floodfill">color_floodfill</a></dt>
<dd>Change the color of neighboring pixels that are the same color</dd>
<dt><a href="image1.html#colormap">colormap</a></dt>
<dd>Get or set a color in the image's colormap</dd>
<dt><a href="image1.html#color_point">color_point</a></dt>
<dd>Change the color of a single pixel in the image</dd>
<dt><a href="image1.html#color_reset_bang">color_reset!</a></dt>
<dd>Set the entire image to a single color</dd>
<dt><a href="image1.html#cycle_colormap">cycle_colormap</a></dt>
<dd>Displace the image's colormap</dd>
<dt><a href="image2.html#erase_bang">erase!</a></dt>
<dd>Set the entire image to a single color</dd>
<dt>
<a href="image2.html#matte_fill_to_border">matte_fill_to_border</a>
</dt>
<dd>Make transparent neighboring pixels. Stop at the image's border color.</dd>
<dt><a href="image2.html#matte_floodfill">matte_floodfill</a></dt>
<dd>Make transparent neighboring pixels that are the same color</dd>
<dt><a href="image2.html#matte_point">matte_point</a></dt>
<dd>Make a single pixel transparent</dd>
<dt><a href="image2.html#matte_reset_bang">matte_reset!</a></dt>
<dd>Make the entire image transparent</dd>
<dt>
<a href="image2.html#opaque">opaque</a>,
<a href="image2.html#opaque_channel">opaque_channel</a>
</dt>
<dd>Change all pixels from the specified color to a new color</dd>
<dt><a href="image3.html#pixel_color">pixel_color</a></dt>
<dd>Get or set the color of a single pixel</dd>
<dt><a href="image3.html#splice">splice</a></dt>
<dd>Splice a solid color into the image.</dd>
<dt>
<a href="image3.html#texture_fill_to_border">texture_fill_to_border</a>
</dt>
<dd>Replace neighbor pixels with pixels from a texture image. Stop at the image's border color.</dd>
<dt><a href="image3.html#texture_floodfill">texture_floodfill</a></dt>
<dd>Replace neighboring pixels that are the same color with pixels from a texture image</dd>
<dt>
<a href="image3.html#paint_transparent">paint_transparent</a>,
<a href="image3.html#transparent">transparent</a>
</dt>
<dd>Change the opacity value of pixels having the specified color</dd>
<dt><a href="image3.html#transparent_chroma">transparent_chroma</a></dt>
<dd>Change the opacity value of pixels within the specified range</dd>
</dl>
<h4>Rotate, flip, or shear</h4>
<dl>
<dt><a href="image1.html#affine_transform">affine_transform</a></dt>
<dd>Transform the image as dictated by an affine matrix</dd>
<dt><a href="image1.html#auto_orient">auto_orient</a></dt>
<dd>Rotate or flip the image using the EXIF orientation tag</dd>
<dt><a href="image1.html#deskew">deskew</a></dt>
<dd>Straighten the image</dd>
<dt><a href="image2.html#flip">flip</a></dt>
<dd>Create a vertical mirror image</dd>
<dt><a href="image2.html#flop">flop</a></dt>
<dd>Create a horizontal mirror image</dd>
<dt><a href="image3.html#rotate">rotate</a></dt>
<dd>Rotate the image by the specified angle</dd>
<dt><a href="image3.html#shear">shear</a></dt>
<dd>Shear the image along the X or Y axis, creating a parallelogram</dd>
<dt><a href="image3.html#transpose">transpose</a></dt>
<dd>Create a horizontal mirror image</dd>
<dt><a href="image3.html#transverse">transverse</a></dt>
<dd>Create a vertical mirror image</dd>
</dl>
<h4>Composite</h4>
<dl>
<dt><a href="image1.html#add_compose_mask">add_compose_mask</a></dt>
<dd>Add a mask to the background image in a composite operation</dd>
<dt class="ilist"><a href="ilist.html#average">average</a></dt>
<dd class="ilist">Average all the images in the imagelist into a single image</dd>
<dt><a href="image1.html#blend">blend</a></dt>
<dd>Blend two images together</dd>
<dt><a href="image1.html#composite">composite</a></dt>
<dd>Composite a source image onto the image</dd>
<dt><a href="image1.html#composite_affine">composite_affine</a></dt>
<dd>Composite a source image onto the image as dictated by an affine matrix</dd>
<dt class="ilist">
<a href="ilist.html#composite_layers">composite_layers</a>
</dt>
<dd class="ilist">Composite two imagelists</dd>
<dt>
<a href="image1.html#composite_mathematics">composite_mathematics</a>
</dt>
<dd>Merge a source image onto the image according to a formula.</dd>
<dt><a href="image1.html#composite_tiled">composite_tiled</a></dt>
<dd>Composite multiple copies of an image over another image</dd>
<dt>
<a href="image1.html#delete_compose_mask">delete_compose_mask</a>
</dt>
<dd>Delete a compose mask</dd>
<dt><a href="image1.html#displace">displace</a></dt>
<dd>Distort the image using a displacement map</dd>
<dt><a href="image1.html#dissolve">dissolve</a></dt>
<dd>Dissolve two images into each other</dd>
<dt><a href="image2.html#extent">extent</a></dt>
<dd>Extend or crop the image over the background color.</dd>
<dt><a href="image3.html#watermark">watermark</a></dt>
<dd>Composite a watermark onto the image</dd>
</dl>
<h4>Transform</h4>
<dl>
<dt class="ilist"><a href="ilist.html#append">append</a></dt>
<dd class="ilist">Append all the images in the imagelist into a single image</dd>
<dt><a href="image1.html#chop">chop</a></dt>
<dd>Chop a region from the image</dd>
<dt class="ilist"><a href="ilist.html#coalesce">coalesce</a></dt>
<dd class="ilist">Merge successive images in the imagelist into a new imagelist</dd>
<dt><a href="image1.html#crop">crop</a></dt>
<dd>Extract a region from the image</dd>
<dt><a href="image1.html#decipher">decipher</a></dt>
<dd>Convert enciphered pixels to plain pixels</dd>
<dt class="ilist"><a href="ilist.html#deconstruct">deconstruct</a></dt>
<dd class="ilist">Construct a new imagelist containing images that include only the changed pixels between each image and its successor</dd>
<dt><a href="image1.html#distort">distort</a></dt>
<dd>Distort the image</dd>
<dt><a href="image2.html#encipher">encipher</a></dt>
<dd>Encipher plain pixels</dd>
<dt><a href="image2.html#excerpt">excerpt</a></dt>
<dd>Excerpt a rectangle from the image</dd>
<dt class="ilist">
<a href="ilist.html#flatten_images">flatten_images</a>
</dt>
<dd class="ilist">Merge all the images in the imagelist into a single image</dd>
<dt class="ilist"><a href="ilist.html#mosaic">mosaic</a></dt>
<dd class="ilist">Inlay all the images in the imagelist into a single image</dd>
<dt class="ilist">
<a href="ilist.html#optimize_layers">optimize_layers</a>
</dt>
<dd class="ilist">Optimize or compare image layers</dd>
<dt><a href="image1.html#resize_to_fill">resize_to_fill</a></dt>
<dd>Resize and crop while retaining the aspect ratio</dd>
<dt><a href="image3.html#roll">roll</a></dt>
<dd>Offset the image</dd>
<dt><a href="image3.html#shave">shave</a></dt>
<dd>Shave regions from the edges of the image</dd>
<dt><a href="image3.html#trim">trim</a></dt>
<dd>Remove borders from the edges of the image</dd>
</dl>
<h4>Enhance</h4>
<dl>
<dt><a href="image1.html#clut_channel">clut_channel</a></dt>
<dd>Replace the channel values in the image with a lookup of its replacement value in an LUT gradient image.</dd>
<dt><a href="image1.html#contrast">contrast</a></dt>
<dd>Enhance the intensity differences between the lighter and darker elements in the image</dd>
<dt>
<a href="image1.html#contrast_stretch_channel">contrast_stretch_channel</a>
</dt>
<dd>Improve the contrast in the image by stretching the range of intensity values</dd>
<dt><a href="image1.html#despeckle">despeckle</a></dt>
<dd>Reduce the speckle noise in the image</dd>
<dt><a href="image2.html#enhance">enhance</a></dt>
<dd>Apply a digital filter that improves the quality of a noisy image</dd>
<dt>
<a href="image2.html#equalize">equalize</a>,
<a href="image2.html#equalize_channel">equalize_channel</a>
</dt>
<dd>Apply a histogram equalization to the image</dd>
<dt><a href="image2.html#function_channel">function_channel</a></dt>
<dd>Apply a function to selected channel values</dd>
<dt class="ilist"><a href="ilist.html#fx">fx</a></dt>
<dd class="ilist">Apply a mathematical expression to an image</dd>
<dt>
<a href="image1.html#auto_gamma_channel">auto_gamma_channel</a>, <a href="image2.html#gamma_correct">gamma_correct</a>,
<a href="image2.html#gamma_channel">gamma_channel</a>
</dt>
<dd>Gamma correct the image</dd>
<dt>
<a href="image1.html#auto_level_channel">auto_level_channel</a>,
<a href="image2.html#level">level,</a>
<a href="image2.html#level_channel">level_channel</a>, <a href="image2.html#level_colors">level_colors</a>,
<a href="image2.html#levelize_channel">levelize_channel</a>
</dt>
<dd>Adjust the levels of one or more channels in the image</dd>
<dt><a href="image2.html#linear_stretch">linear_stretch</a></dt>
<dd>Stretch with saturation the image contrast</dd>
<dt><a href="image2.html#median_filter">median_filter</a></dt>
<dd>Apply a digital filter that improves the quality of a noisy image</dd>
<dt><a href="image2.html#modulate">modulate</a></dt>
<dd>Change the brightness, saturation, or hue of the image</dd>
<dt>
<a href="image2.html#negate">negate</a>,
<a href="image2.html#negate_channel">negate_channel</a>
</dt>
<dd>Negate the colors of the image</dd>
<dt>
<a href="image2.html#normalize">normalize</a>,
<a href="image2.html#normalize_channel">normalize_channel</a>
</dt>
<dd>Enhance the contrast of the image</dd>
<dt><a href="image3.html#quantum_operator">quantum_operator</a></dt>
<dd>Performs an integer arithmetic operation on selected channels values</dd>
<dt><a href="image3.html#reduce_noise">reduce_noise</a></dt>
<dd>Smooth the contours of an image while still preserving edge information</dd>
<dt>
<a href="image1.html#adaptive_sharpen">adaptive_sharpen</a>, <a href="image1.html#adaptive_sharpen_channel">adaptive_sharpen_channel</a>,
<a href="image3.html#sharpen">sharpen</a>, <a href="image3.html#sharpen_channel">sharpen_channel</a>,
<a href="image3.html#unsharp_mask">unsharp_mask</a>,
<a href="image3.html#unsharp_mask_channel">unsharp_mask_channel</a>
</dt>
<dd>Sharpen the image</dd>
<dt>
<a href="image3.html#sigmoidal_contrast_channel">sigmoidal_contrast_channel</a>
</dt>
<dd>Adjusts the contrast of an image channel with a non-linear sigmoidal contrast algorithm</dd>
</dl>
<h4>Add effects</h4>
<dl>
<dt><a href="image1.html#adaptive_threshold">adaptive_threshold</a></dt>
<dd>Threshold an image whose global intensity histogram doesn't contain distinctive peaks</dd>
<dt>
<a href="image1.html#add_noise">add_noise</a>,
<a href="image1.html#add_noise_channel">add_noise_channel</a>
</dt>
<dd>Add random noise</dd>
<dt><a href="image1.html#bilevel_channel">bilevel_channel</a></dt>
<dd class="imquote">Change the value of individual image pixels based on the intensity of each pixel channel</dd>
<dt><a href="image1.html#black_threshold">black_threshold</a></dt>
<dd>Force all pixels below the threshold into black</dd>
<dt><a href="image1.html#blue_shift">blue_shift</a></dt>
<dd class="imquote">Simulate a scene at nighttime in the moonlight</dd>
<dt>
<a href="image1.html#adaptive_blur">adaptive_blur</a>, <a href="image1.html#adaptive_blur_channel">adaptive_blur_channel</a>,
<a href="image1.html#blur_image">blur_image</a>, <a href="image1.html#blur_channel">blur_channel</a>,
<a href="image2.html#gaussian_blur">gaussian_blur</a>, <a href="image2.html#gaussian_blur_channel">gaussian_blur_channel</a>,
<a href="image2.html#motion_blur">motion_blur</a>, <a href="image3.html#radial_blur">radial_blur</a>,
<a href="image3.html#radial_blur_channel">radial_blur_channel</a>,
<a href="image3.html#selective_blur_channel">selective_blur_channel</a>
</dt>
<dd>Blur the image</dd>
<dt><a href="image1.html#colorize">colorize</a></dt>
<dd>Blend the fill color with each pixel in the image</dd>
<dt>
<a href="image1.html#convolve">convolve</a>,
<a href="image1.html#convolve_channel">convolve_channel</a>
</dt>
<dd>Apply a custom convolution kernel to the image</dd>
<dt><a href="image3.html#segment">segment</a></dt>
<dd>
Segment an image by analyzing the histograms of the color components and identifying units that are homogeneous with the fuzzy c-means technique
</dd>
<dt>
<a href="image3.html#random_threshold_channel">random_threshold_channel</a>
</dt>
<dd>Change the value of individual pixels based on the intensity of each pixel compared to a random threshold.</dd>
<dt><a href="image3.html#recolor">recolor</a></dt>
<dd>translate, scale, shear, or rotate the image colors</dd>
<dt><a href="image3.html#threshold">threshold</a></dt>
<dd>Change the value of individual pixels based on the intensity of each pixel compared to threshold</dd>
<dt><a href="image3.html#white_threshold">white_threshold</a></dt>
<dd>Force all pixels above the threshold into white</dd>
</dl>
<h4>Add special effects</h4>
<dl>
<dt><a href="image1.html#charcoal">charcoal</a></dt>
<dd>Add a charcoal effect</dd>
<dt><a href="image2.html#edge">edge</a></dt>
<dd>Find edges in the image</dd>
<dt><a href="image2.html#emboss">emboss</a></dt>
<dd>Add a three-dimensional effect</dd>
<dt><a href="image2.html#implode">implode</a></dt>
<dd>Implode or explode the center pixels in the image</dd>
<dt class="ilist"><a href="ilist.html#morph">morph</a></dt>
<dd class="ilist">Transform each image in the imagelist to the next in sequence by creating intermediate images</dd>
<dt><a href="image2.html#oil_paint">oil_paint</a></dt>
<dd>Add an oil paint effect</dd>
<dt><a href="image3.html#polaroid">polaroid</a></dt>
<dd>Simulate a Polaroid® instant picture</dd>
<dt><a href="image3.html#sepiatone">sepiatone</a></dt>
<dd>Applies an effect similar to the effect achieved in a photo darkroom by sepia toning.</dd>
<dt><a href="image3.html#shade">shade</a></dt>
<dd>Shine a distant light on an image to create a three-dimensional effect</dd>
<dt><a href="image3.html#shadow">shadow</a></dt>
<dd>Add a shadow to an image</dd>
<dt><a href="image3.html#sketch">sketch</a></dt>
<dd>Simulate a pencil sketch</dd>
<dt><a href="image3.html#solarize">solarize</a></dt>
<dd>
Apply a special effect to the image, similar to the effect achieved in a photo darkroom by selectively exposing areas of photo sensitive paper to
light
</dd>
<dt><a href="image3.html#spread">spread</a></dt>
<dd>Randomly displace each pixel in a block</dd>
<dt><a href="image3.html#stegano">stegano</a></dt>
<dd>Hide a digital watermark within the image</dd>
<dt><a href="image3.html#stereo">stereo</a></dt>
<dd>Combine two images into a single image that is the composite of a left and right image of a stereo pair</dd>
<dt><a href="image3.html#swirl">swirl</a></dt>
<dd>Swirl the pixels about the center of the image</dd>
<dt><a href="image3.html#vignette">vignette</a></dt>
<dd>Soften the edges of the image to create a vignette</dd>
<dt><a href="image3.html#wave">wave</a></dt>
<dd>Add a ripple effect to the image</dd>
<dt><a href="image3.html#wet_floor">wet_floor</a></dt>
<dd>Create a "wet floor" reflection of the image</dd>
</dl>
<h4>Decorate</h4>
<dl>
<dt><a href="image1.html#border">border</a></dt>
<dd>Surround the image with a solid-colored border</dd>
<dt><a href="image2.html#frame">frame</a></dt>
<dd>Add a simulated three-dimensional border around the image</dd>
<dt><a href="image3.html#raise">raise</a></dt>
<dd>Create a simulated three-dimensional button-like effect by lightening and darkening the edges of the image</dd>
</dl>
<h4>Create thumbnail montages</h4>
<dl>
<dt class="ilist"><a href="ilist.html#montage">montage</a></dt>
<dd class="ilist">Tile image thumbnails across a canvas</dd>
</dl>
<h4>Create image blobs</h4>
<dl>
<dt><a href="image1.html#from_blob">from_blob</a></dt>
<dd>Create an image from a BLOB</dd>
<dt class="ilist"><a href="ilist.html#from_blob">from_blob</a></dt>
<dd class="ilist">Create an imagelist from one or more BLOBs</dd>
<dt><a href="image3.html#to_blob">to_blob</a></dt>
<dd>Construct a BLOB from an image</dd>
<dt class="ilist"><a href="ilist.html#to_blob">to_blob</a></dt>
<dd class="ilist">Construct a BLOB from all the images in the imagelist</dd>
</dl>
</div>
<h2 id="marshaling">Marshaling images</h2>
<p>
ImageList, Image, Draw, and Pixel objects are marshaled using custom serialization methods. Images are marshaled with ImageMagick's Binary Large OBject
functions <code>ImageToBlob</code> (for dumping) and <code>BlobToImage</code> (for loading).
</p>
<h4>Notes</h4>
<ol>
<li>Some image formats cannot be dumped. The only way to be sure it will work is to try it.</li>
<li>
Images in lossy formats, such as JPEG, will have a different signature after being reconstituted and therefore will not compare equal (using
<a href="image1.html#spaceship"><=></a>) to the original image.
</li>
<li>Image metadata may be changed by marshaling.</li>
</ol>
<h2 id="drawing_on">Drawing on and adding text to images</h2>
<p>
The <strong>Draw</strong> class is the third major class in the Magick module. This class defines two kinds of methods,
<a href="#drawing">drawing</a> methods and <a href="#annotation">annotation</a> methods.
</p>
<h3 id="drawing">Drawing</h3>
<p>
ImageMagick supports a set of 2D drawing commands that are very similar to the commands and elements defined by the W3C's
<a href="https://www.w3.org/TR/SVG11/">Scalable Vector Graphics (SVG) 1.1 Specification</a>. In RMagick, each command (called a <em>primitive</em>) is
implemented as a method in the <code>Draw</code> class. To draw on an image, simply
</p>
<ol>
<li>Create an instance of the <code>Draw</code> class.</li>
<li>Call one or more primitive methods with the appropriate arguments.</li>
<li>
Call the <a href="draw.html#draw"><code>draw</code></a> method.
</li>
</ol>
<p>
The primitive methods do <em>not</em> draw anything directly. When you call a primitive method, you are simply adding the primitive and its arguments to a
list of primitives stored in the <code>Draw</code> object. To "execute" the primitive list, call <code>draw</code>. Drawing the primitives does not
destroy them. You can draw on another image by calling <code>draw</code> again, specifying a different image as the "canvas." Of course you can also draw
on an image with multiple <code>Draw</code> objects, too. The canvas can be any image or imagelist, created by reading an image file or from scratch using
<code>ImageList#new_image</code> or <code>Image.new</code>. (If you pass an imagelist object to <code>draw</code>, it draws on the current image.)
</p>
<div id="axes">
<a href="javascript:popup('axes.rb.html')"><img src="ex/axes.gif" alt="Drawing coordinate system" title="Click to see the example script" /></a>
<div>
<p>
Here's an illustration of the default drawing coordinate system. The origin is in the top left corner. The x axis extends to the right. The y axis
extends downward. The units are pixels. 0° is at 3 o'clock and rotation is clockwise. The units of rotation are usually degrees.<sup
><a href="#degrees">3</a></sup
>
</p>
<p>
You can change the default coordinate system by specifying a
<em>scaling</em>, <em>rotation</em>, or <em>translation</em> transformation.
</p>
<p>
<em>(Click the image to see the Ruby program that created it.)</em>
</p>
</div>
</div>
<p>
RMagick's primitive methods include methods for drawing points, lines, Bezier curves, shapes such as ellipses and rectangles, and text. Shapes and lines
have a fill color and a stroke color. Shapes are filled with the fill color unless the fill opacity is 0. Similarly, shapes are stroked with the stroke
color unless the stroke opacity is 0. Text is considered a shape and is stroked and filled. Other rendering properties you can set include the stroke
width, antialiasing, stroke patterns, and fill patterns.
</p>
<p>As an example, here's the section of the Ruby program that created the circle in the center of the above image.</p>
<pre class="example">
1. !# /usr/local/bin/ruby -w
2. require "rmagick"
3.
4. canvas = Magick::ImageList.new
5. canvas.new_image(250, 250, Magick::HatchFill.new('white', 'gray90'))
6.
7. circle = Magick::Draw.new
8. circle.stroke('tomato')
9. circle.fill_opacity(0)
10. circle.stroke_opacity(0.75)
11. circle.stroke_width(6)
12. circle.stroke_linecap('round')
13. circle.stroke_linejoin('round')
14. circle.ellipse(canvas.rows/2,canvas.columns/2, 80, 80, 0, 315)
15. circle.polyline(180,70, 173,78, 190,78, 191,62)
16. circle.draw(canvas)
</pre
>
<p>
The statements on lines 4 and 5 create the drawing canvas with a single 250x250 image. The <code>HatchFill</code> object fills the image with light-gray
lines 10 pixels apart. The statement on line 7 creates a Draw object. The method calls on lines 8-15 construct a list of primitives that are "executed" by
the <code>draw</code> method call on line 16.
</p>
<p>
The <code>stroke</code> method sets the stroke color, as seen on line 8. Normally, shapes are filled (opacity = 1.0), but the call to
<code>fill_opacity</code> on line 9 sets the opacity to 0, so the background will show through the circle. Similarly, the stroke lines are normally
opaque, but the tomato-colored stroke line in this example is made slightly transparent by the call to <code>stroke_opacity</code> on line 10. The method
calls on lines 11 through 13 set the stroke width and specify the appearance of the line ends and corners.
</p>
<p>
The <code>ellipse</code> method call on line 14 describes an circle in the center of the canvas with a radius of 80 pixels. The ellipse occupies 315°
of a circle, starting at 0° (that is, 3 o'clock). The <code>polyline</code> call on line 15 adds the arrowhead to the circle. The arguments (always an
even number) are the x- and y-coordinates of the points the line passes through.
</p>
<p>Finally, the <code>draw</code> method on line 16 identifies the canvas to be drawn on and executes the stored primitives.</p>
<h3 id="annotation">Annotation</h3>
<p>
The <a href="draw.html#annotate"><code>annotate</code></a>
method draws text on an image. In its simplest form,
<code>annotate</code> requires only arguments that describe where to draw the text and the text string.
</p>
<p>
Most of the time, you'll want to specify text properties such as the font, its size, font styles such as italic, font weights such as bold, the fill and
stroke color, etc. The Draw class defines
<a href="draw.html">attribute writers</a> for this purpose. You can set the desired text properties by calling the attribute writers before calling
<code>annotate</code>, or you can call them in an image block associated with the <code>annotate</code> call.
</p>
<p>The following example shows how to use <code>annotate</code> to produce this image.</p>
<div id="rubyname">
<a href="javascript:popup('rubyname.rb.html')"><img src="ex/rubyname.gif" alt="annotate example" title="Click to see the example script" /></a>
</div>
<pre class="example">
1. #! /usr/local/bin/ruby -w
2. require "rmagick"
3.
4. # Demonstrate the annotate method
5.
6. Text = 'RMagick'
7.
8. granite = Magick::ImageList.new('granite:')
9. canvas = Magick::ImageList.new
10. canvas.new_image(300, 100, Magick::TextureFill.new(granite))
11.
12. text = Magick::Draw.new
13. text.font_family = 'helvetica'
14. text.pointsize = 52
15. text.gravity = Magick::CenterGravity
16.
17. text.annotate(canvas, 0,0,2,2, Text) { |options|
18. options.fill = 'gray83'
19. }
20.
21. text.annotate(canvas, 0,0,-1.5,-1.5, Text) { |options|
22. options.fill = 'gray40'
23. }
24.
25. text.annotate(canvas, 0,0,0,0, Text) { |options|
26. options.fill = 'darkred'
27. }
28.
29. canvas.write('rubyname.gif')
30. exit
</pre
>
<p>
This program uses three calls to <code>annotate</code> to produce the "etched" appearance. All three calls have some parameters in common but the fill
color and location are different.
</p>
<p>
First, the statements in lines 8-10 create the background. See
<a href="struct.html#fill"><code>Fill classes</code></a> for information about the <code>TextureFill</code> class. The "granite:" image format is one of
ImageMagick's built-in image formats. See <a href="imusage.html#builtin_formats">"Built-in image formats"</a> for more information. The statement on line
12 creates the Draw object that does the annotation. The next 3 lines set the values of the attributes that are common to all 3
<code>annotate</code> calls.
</p>
<p>
The first <code>annotate</code> argument is the image on which the text will be drawn. Arguments 2-5, <code>width</code>, <code>height</code>,
<code>x</code>, and <code>y</code>, describe a rectangle about which the text is drawn. This rectangle, combined with the value of <code>gravity</code>,
define the position of the text. When the <code>gravity</code> value is <a href="constants.html#GravityType"><code>CenterGravity</code></a> the values of
<code>width</code> and <code>height</code> are unused.
</p>
<p>
The first call to <code>annotate</code>, on lines 17-19, draws the text 2 pixels to the right and down from the center. The
<code>options.fill = 'gray83'</code> statement sets the text color to light gray. The second call to <code>annotate</code>, on lines 21-22, draws dark
gray text 1.5 pixels to the left and up from the center. The last call, on lines 25-27, draws the text a third time, in dark red, exactly in the center of
the image.
</p>
<h2 id="more">Where to go from here</h2>
<p>
The next section,
<a href="imusage.html">"ImageMagick Conventions,"</a> describes some conventions that you need to know, such as how ImageMagick determines the graphic
format of an image file, etc. The ImageMagick (www.imagemagick.org) web site (from which much of the information in these pages has been taken) offers a
lot of detail about ImageMagick. While this web sites doesn't describe RMagick, you can often use the documentation to learn more about a RMagick method
by reading about the Magick API the method calls. (In the Reference section of this document, most of the method descriptions include the name of the
Magick API that the method calls.) Check out the example programs. Almost every one of the methods is demonstrated in one of the examples.
</p>
<p>Good luck!</p>
<div>
<h5 id="footnotes">Footnotes</h5>
<p id="display">
<span class="sup">1</span>The <code>display</code> and <code>animate</code> methods do not work on MS Windows. You will have to write the image to a
file and view it with a separate image viewer.
</p>
<p id="rubygems">
<span class="sup">2</span>If you installed RMagick using <a href="https://rubygems.org/">Rubygems</a> you must set up the RubyGems environment before
using RMagick. You can do one of
</p>
<ol>
<li>add the <code>require 'rubygems'</code> statement to your program</li>
<li>use the -rubygems command line option</li>
<li>add <code>rubygems</code> to the RUBYOPT environment variable</li>
</ol>
<p id="degrees">
<span class="sup">3</span>The rotation attributes <code>rx</code> and <code>ry</code> in the
<a href="struct.html#AffineMatrix"><code>AffineMatrix</code></a> class use radians instead of degrees.
</p>
</div>
<p class="spacer"></p>
<div class="nav">
« <a href="index.html">Prev</a> | <a href="index.html">Contents</a> | <a href="imusage.html">Next</a>
»
</div>
</body>
</html>
|