Книга: Automate the Boring Stuff with Python: Practical Programming for Total Beginners
Назад: 16. Sending Email and Text Messages
Дальше: 18. Controlling the Keyboard and Mouse with GUI Automation

lists a selection of standard color names and their values.

.

demonstrates how this coordinate system works.

Many of Pillow’s functions and methods take a box tuple argument. This means Pillow is expecting a tuple of four integer coordinates that represent a rectangular region in an image. The four integers are, in order, as follows:

.

is the image that will be used for all the interactive shell examples in this chapter. You can download it from .

Once you have the image file Zophie.png in your current working directory, you’ll be ready to load the image of Zophie into Python, like so:

.

.

. You can achieve this effect with just a couple for loops. Continue the interactive shell example by entering the following:

. To see our nested loops working, we print left and top. After the pasting is complete, we save the modified catCopyTwo to tiled.png.

.

. On OS X, transparent pixels are used for the gaps instead.

The rotate() method has an optional expand keyword argument that can be set to True to enlarge the dimensions of the image to fit the entire rotated new image. For example, enter the following into the interactive shell:

). The second call rotates the image 6 degrees with expand set to True and saves it to rotate6_expanded.png (see the image on the right of ).

.

. You can call getpixel() on some coordinates to confirm that the color at any given pixel is what you expect. Finally, save the image to putPixel.png.

is the logo you want to add to the bottom-right corner of each image: a black cat icon with a white border, with the rest of the image transparent.

shows how to calculate the pasting position. The left coordinate for where to paste the logo will be the image width minus the logo width; the top coordinate for where to paste the logo will be the image height minus the logo height.

. Remember that the paste() method will not paste the transparency pixels if you do not pass the logoIm for the third argument as well. This program can automatically resize and “logo-ify” hundreds of images in just a couple minutes.

.

.

.

. Modify resizeAndAddLogo.py so that the image must be at least twice the width and height of the logo image before the logo is pasted. Other wise, it should skip adding the logo.

included a practice project to create custom invitations from a list of guests in a plaintext file. As an additional project, use the pillow module to create images for custom seating cards for your guests. For each of the guests listed in the guests.txt file from the resources at , generate an image file with the guest name and some flowery decoration. A public domain flower image is available in the resources at .

To ensure that each seating card is the same size, add a black rectangle on the edges of the invitation image so that when the image is printed out, there will be a guideline for cutting. The PNG files that Pillow produces are set to 72 pixels per inch, so a 4×5-inch card would require a 288×360-pixel image.

© RuTLib.com 2015-2018