《Python編程快速上手—讓繁瑣工作自動(dòng)化》第17章實(shí)踐項(xiàng)目答案

17.3 項(xiàng)目:添加徽標(biāo)

17.7.1 實(shí)踐項(xiàng)目:擴(kuò)展和修正本章項(xiàng)目的程序

項(xiàng)目要求:

  1. 調(diào)整數(shù)千張圖片的大小,并在每張圖片的角上增加一個(gè)小徽標(biāo)水印
  2. 圖像必須至少是徽標(biāo)的兩倍的寬度和高度,然后才粘貼徽標(biāo)。否則,它應(yīng)該跳過添加徽標(biāo)。
#! python3
# put watermark into many .jpg or .png
import os

# to make a watermark pic without background color
from PIL import Image
f = Image.open('wmk.png')
w,h = f.size
for x in range(w):
    for y in range(h):
        if f.getpixel((x,y)) == (255,255,255,255):
            f.putpixel((x,y),(0,0,0,0))
f.save('wmk.png')

# in a 300x300 square, and adds catlogo.png to the lower-right corner.
SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'wmk.png'
logoIm = Image.open(LOGO_FILENAME)
logoWidth, logoHeight = logoIm.size

# TODO: Loop over all files in the working directory.
for filename in os.listdir('.'):
    if not (filename.lower().endswith('.jpg')
            or filename.lower().endswith('.png')
            or filename.lower().endswith('.gif')
            or filename.lower().endswith('.bmp')) or (filename == LOGO_FILENAME):
        continue
    print(filename)
    im = Image.open(filename)
    width, height = im.size

# TODO: Check if image needs to be resized.
    if width < 2 * logoWidth or height < 2 * logoHeight:
        continue
    elif width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
# TODO: Calculate the new width and height to resize to.
        if width > height:
            height = int((SQUARE_FIT_SIZE / width) * height)
            width = SQUARE_FIT_SIZE
        else:
            width = int((SQUARE_FIT_SIZE / height) * width)
            height = SQUARE_FIT_SIZE
# TODO: Resize the image.
        print('resizing the pic')
        im = im.resize((width, height))
# TODO: Add the logo.
    print('adding watermark into %s...' % filename)
    im.paste(logoIm,(width - logoWidth, height - logoHeight),logoIm)
# TODO: Save changes.
    im.save("added_" + filename)

17.7.2 實(shí)踐項(xiàng)目:在硬盤上識別照片文件夾

項(xiàng)目要求:編寫一個(gè)程序,遍歷硬盤上的每個(gè)文件夾,找到可能的照片文件夾。當(dāng)然,首先你必須定義什么是“照片文件夾”。假定就是超過半數(shù)文件是照片的任何文件夾。你如何定義什么文件是照片?首先,照片文件必須具有文件擴(kuò)展名.png 或.jpg。此外,照片是很大的圖像。照片文件的寬度和高度都必須大于 500 像素。

#! python3
# check pic dir

import os
from PIL import Image

for root, folders, files in os.walk('.'):
    numPhoteFiles = 0
    numNonPhotoFiles = 0
    for file in files:
        if not(file.lower().endswith('.jpg') or file.lower().endswith('.png')):
            numNonPhotoFiles += 1
            continue

        im = Image.open(os.path.join(root,file))
        width, height = im.size
        if width < 500 and height < 500:
            numNonPhotoFiles += 1
        else:
            numPhoteFiles += 1
            
        if numPhoteFiles >= numNonPhotoFiles:
            print(root)

17.7.3 實(shí)踐項(xiàng)目:定制的座位卡

項(xiàng)目要求:使用 Pillow 模塊,為客人創(chuàng)建定制的座位卡圖像。從 http://nostarch.com/automatestuff/下載資源文件guests.txt,對于其中列出的客人,生成帶有客人名字和一些鮮花裝飾的圖像文件。

#! python3
# make card for different guests

import os
from PIL import Image, ImageDraw

os.chdir('card_dir')
with open('guests.txt') as f:
    for guest in f.readlines():
        im = Image.open('card_mo.png')
        draw = ImageDraw.Draw(im)
        draw.text((50,50),guest.strip().title(),fill='blue')
        card_name = 'card_to_%s.png' % guest.strip()
        im.save(card_name)

        print('card-making work is done!')

環(huán)境:python3

想做這個(gè)系列文章,就是因?yàn)楫?dāng)時(shí)看這本書時(shí),想看看網(wǎng)上有沒更優(yōu)美的解決,但是略難找到。所以就把自己的項(xiàng)目練習(xí)放在了一個(gè)txt文件中,現(xiàn)在把練習(xí)代碼放到這里,有不足之處希望大家能給出指導(dǎo)意見及相互交流、提升。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容