Chapter 17 – Manipulating Images
非计算机出身,最近一直在看《Automate the Boring Stuff with Python》,为了能够很好的理解和学习,一直坚持在做文后的练习题。但一个人学习实在是太累太难,准备将自己的做的练习题记录下来,希望得到高手的指导,来拓展个人解决问题的思路、提升代码质量。
传送门:
Chapter 12 – Working with Excel Spreadsheets
Chapter 13 – Working with PDF and Word Documents
Chapter 14 – Working with CSV Files and JSON Data
Chapter 15 – Keeping Time, Scheduling Tasks, and Launching Programs
Chapter 18 – Controlling the Keyboard and Mouse with GUI Automation
Identifying Photo Folders on the Hard Drive
I have a bad habit of transferring files from my digital camera to temporary folders somewhere on the hard drive and then forgetting about these folders. It would be nice to write a program that could scan the entire hard drive and find these leftover “photo folders.”
Write a program that goes through every folder on your hard drive and finds potential photo folders. Of course, first you’ll have to define what you consider a “photo folder” to be; let’s say that it’s any folder where more than half of the files are photos. And how do you define what files are photos?
First, a photo file must have the file extension .png or .jpg. Also, photos are large images; a photo file’s width and height must both be larger than 500 pixels. This is a safe bet, since most digital camera photos are several thousand pixels in width and height.
#! python3
# checkPhotoFolder.py
import os
from PIL import Image
for foldername, subfolders, filenames in os.walk('C:\\'):
numPhotoFiles = 0
numNonPhotoFiles = 0
numFileList = []
for filename in filenames:
# Check if the file extension isn't .png or .jpg.
if not(filename.endswith('.png') or filename.endswith('.jpg')):
numNonPhotoFiles += 1
continue
# Open image file using Pillow.
im = Image.open(filename)
imWidth, imHeight = im.size
# Check if width & height are larger than 500 pixels.
if imWidth > 500 and imHeight > 500:
numPhotoFiles += 1
else:
numNonPhotoFiles += 1
numFileList.apend(filename)
print(numFileList)
# If more than half of files were photos.
# print the absolute path of the folder path.
if numPhotoFiles > int(len(numFileList)*0.5):
print(foldername)
else:
print('Not find photo foder.')
Custom Seating Cards
Chapter 13 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 http://nostarch.com/automatestuff/, generate an image file with the guest name and some flowery decoration. A public domain flower image is available in the resources at http://nostarch.com/automatestuff/.
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.
In chapter 13,guest data as follows:
Prof. Plum
Miss Scarlet
Col. Mustard
Al Sweigart
Robocop
#! python3
# ImageForInvitations.py
import os
from PIL import Image, ImageDraw, ImageFont
# Open the guests.txt,pick up the name of the guests.
guestsTxt = open('guests.txt', 'r')
guestsNameList = guestsTxt.read().split('\n')
# Set the font for invitations.
fontFolder = 'C:\\Windows\\Fonts'
arialFont = ImageFont.truetype(os.path.join(fontFolder, 'Arial.ttf'), 32)
for guestName in guestsNameList:
# Create the images for invitations.
im = Image.new('RGB', (288, 360), 'pink')
draw = ImageDraw.Draw(im)
draw.rectangle((5, 5, 282, 354), fill = 'red')
# Add the content for the invitations.
draw.text((10, 50), 'It would be a pleasure to have the company of ', fill='purple')
draw.text((60, 80), guestName, fill='blue', font=arialFont)
draw.text((25, 130), 'at 11010 memory lane on the evening of ', fill='purple')
draw.text((80, 150), 'April 1st', fill='purple')
draw.text((80, 170), 'at 7 o\'clock', fill='purple')
# Save
im.save('invitation' + '_' + guestName + '.png')
print('Done!')