2024年6月6日 星期四

自動化下載網路上圖片加上浮水印

程式:

 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
import requests
from PIL import Image

urls = ["https://optimise2.assets-servd.host/maniacal-finch/production/animals/amur-tiger-01-01.jpg?w=1200&h=1200&auto=compress%2Cformat&fit=crop&dm=1658935145&s=704a64cbbe164087202ba90883b4b588",
       "https://t3.ftcdn.net/jpg/03/83/46/48/360_F_383464809_VAyaM0bON9NZT1UCPXghp8GhHx56QKqm.jpg",
       "https://images.ctfassets.net/rt5zmd3ipxai/2TtovnO1qnGJyPwtyuVOdU/8ddc745eca71320d2ea1d05679f81cb2/NVA-tiger.jpg?fit=fill&fm=webp&h=578&w=1070&q=72,%20https://images.ctfassets.net/rt5zmd3ipxai/2TtovnO1qnGJyPwtyuVOdU/8ddc745eca71320d2ea1d05679f81cb2/NVA-tiger.jpg?fit=fill&fm=webp&h=1156&w=2140&q=72"]
paths = ["LOGO1.png", "LOGO2.png", "LOGO3.png"]
watermark_im = Image.open("watermark.png")
for index, url in enumerate(urls):
    response = requests.get(url)
    if response.status_code == 200:
        with open(paths[index], 'wb') as fp:
            for chunk in response:
                fp.write(chunk)
        print("LOGO", index+1, "圖檔已經下載")
        im = Image.open(paths[index])
        im = im.resize((600,400))
        im.show()
        x = int(im.width - watermark_im.width)
        y = int(im.height - watermark_im.height)
        im.paste(watermark_im,(x, y),watermark_im)
        im.save(paths[index])
        im.show()
    else:
        print("錯誤! HTTP請求失敗...")

執行結果:
LOGO 1 圖檔已經下載
LOGO 2 圖檔已經下載
LOGO 3 圖檔已經下載



沒有留言:

張貼留言