使用Python实现网页中图片的批量下载和水印添加保存

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 使用Python实现网页中图片的批量下载和水印添加保存

数字时代,图片已经成为我们生活中的一部分。无论是社交媒体上的照片,还是网页中的图片元素,我们都希望能够方便地下载并进行个性化的处理。
假设你是一位设计师,你经常需要从网页上下载大量的图片素材,并为这些图片添加水印以保护你的作品。然而,手动下载和添加水印是一件繁琐的事情 ,这时就可以通过编写一个Python爬虫程序,自动化地完成这个任务,节省时间和精力。
我们的基本思路是通过发送HTTP请求获取网页内容,然后解析网页内容,提取出图片元素的URL。接下来,我们使用请求库下载这些图片,并使用Pillow库添加水印。最后,我们将处理后面的图片保存到本地。
在开始之前,我们需要准备以下工作:
1安装Python:确保您的计算机上已经安装了Python Spark语言的最新版本。
2安装所需的库:我们将使用requests库来发送HTTP请求,PIL库来处理图片。你可以使用以下命令来安装这些库:

pip install requests
pip install pillow

接下来,我们可以定义一个函数来发送HTTP请求并获取页面内容:

def fetch_page(url):
    proxies = {
        "http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
        "https": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
    }
    response = requests.get(url, proxies=proxies)
    return response.content

然后,我们可以编写一个函数来解析页面内容并提取图片元素的URL:

from bs4 import BeautifulSoup
def extract_image_urls(page_content):
    soup = BeautifulSoup(page_content, "html.parser")
    image_urls = []
    for img in soup.find_all("img"):
        image_urls.append(img["src"])
    return image_urls

接下来,我们可以编写一个函数来下载图片并添加水印:

def download_and_add_watermark(image_url):
    response = requests.get(image_url)
    image = Image.open(BytesIO(response.content))
    watermark = Image.new("RGBA", image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(watermark)
    font = ImageFont.truetype("arial.ttf", 36)
    draw.text((10, 10), "Watermark", font=font, fill=(255, 255, 255, 128))
    watermarked_image = Image.alpha_composite(image.convert("RGBA"), watermark)
    watermarked_image.save("watermarked_image.png")

实现完整代码

以下是完整的Python代码:

import requests
from bs4 import BeautifulSoup
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
def fetch_page(url):
    proxies = {
        "http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
        "https": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
    }
    response = requests.get(url, proxies=proxies)
    return response.content
def extract_image_urls(page_content):
    soup = BeautifulSoup(page_content, "html.parser")
    image_urls = []
    for img in soup.find_all("img"):
        image_urls.append(img["src"])
    return image_urls
def download_and_add_watermark(image_url):
    response = requests.get(image_url)
    image = Image.open(BytesIO(response.content))
    watermark = Image.new("RGBA", image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(watermark)
    font = ImageFont.truetype("arial.ttf", 36)
    draw.text((10, 10), "Watermark", font=font, fill=(255, 255, 255, 128))
    watermarked_image = Image.alpha_composite(image.convert("RGBA"), watermark)
    watermarked_image.save("watermarked_image.png")
def main():
    # 获取网页的页面内容
    page_content = fetch_page("https://www.58pic.com/")
    # 提取图片元素的URL
    image_urls = extract_image_urls(page_content)
    # 下载图片并添加水印
    for image_url in
相关文章
|
16天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
1月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
87 0
|
1月前
|
数据库 开发者 Python
用Python代码打造你的私人网页交互系统
用Python代码打造你的私人网页交互系统
30 1
|
1月前
|
存储 移动开发 算法
Python怎么将图片转换成base64编码
Python怎么将图片转换成base64编码
27 0
|
5天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
24 0
|
10天前
|
存储 Python
用Python实现批量下载文件——代理ip排除万难
用Python实现批量下载文件——代理ip排除万难
|
10天前
|
机器学习/深度学习 算法 自动驾驶
opencv python 图片叠加
【4月更文挑战第17天】
|
16天前
|
开发者 索引 Python
实践:如何使用python在网页的表格里抓取信息
实践:如何使用python在网页的表格里抓取信息
|
16天前
|
数据采集 JSON 网络协议
「Python系列」Python urllib库(操作网页URL对网页的内容进行抓取处理)
`urllib` 是 Python 的一个标准库,用于打开和读取 URLs。它提供了一组模块,允许你以编程方式从网络获取数据,如网页内容、文件等。
36 0
|
1月前
|
Python
如何使用Python的Requests库进行网络请求和抓取网页数据?
如何使用Python的Requests库进行网络请求和抓取网页数据?
13 0

热门文章

最新文章