AutoWallpaperEnhancer脚本

脚本名称

AutoWallpaperEnhancer

脚本介绍

AutoWallpaperEnhancer 是一个全自动的 Python 脚本,旨在从指定的网页(如壁纸网站)批量爬取高分辨率图片,并通过 Adobe Photoshop 进行自动化图像增强处理。该脚本结合了 Web 爬虫和 Photoshop 脚本自动化功能,能够批量下载图片并提升其分辨率,使其适合用作高质量壁纸。

主要功能:

  1. 批量图片爬取:从目标网页自动爬取壁纸图片,并保存到本地指定文件夹。
  2. 自动图像增强:通过调用 Photoshop 的“图像大小”功能,利用“保持细节 2.0”算法,自动将爬取到的图片进行放大和增强。
  3. 全自动处理:处理完成后,脚本会自动保存处理后的图片,并自动退出 Photoshop,避免任何人为干预。
  4. 后台运行:Photoshop 可以在后台运行,不干扰用户的其他操作。

使用场景:

  • 适用于需要批量下载和增强壁纸图片的用户。
  • 可应用于需要将低分辨率图片放大为高分辨率的场景,确保图片质量最大化。

依赖:

  • Python 3.x
  • RequestsBeautifulSoup4:用于网页爬虫。
  • win32com.client:用于与 Photoshop 进行 COM 通信。
  • Adobe Photoshop:处理图片的核心工具(建议使用最新版)。

如何使用:

  1. 配置与依赖

    • 安装 Python 并确保已安装所需的 Python 库。
    • 确保系统中已安装 Adobe Photoshop,并配置好 COM 接口。
  2. 运行脚本

    • 将脚本下载到本地,并根据需要修改脚本中的配置(如网页 URL、爬取页数等)。
    • 运行脚本,脚本会自动爬取图片并调用 Photoshop 进行处理。
  3. 查看结果

    • 处理后的高分辨率图片会保存在指定的本地文件夹中。

注意事项:

  • Photoshop 版本:此脚本需要适配 Photoshop 2023 或更高版本。如遇到兼容性问题,建议更新 Photoshop。
  • 图像质量:虽然脚本自动提高图片分辨率,但实际效果仍然取决于原始图片的质量。
  • 批量处理:对于大量图片的处理,建议确保系统性能,以避免 Photoshop 处理过载。

AutoWallpaperEnhancer 是一个功能强大的工具,专为需要高质量壁纸的用户设计,简化了下载和图像增强的流程,提供了一个轻松获取高分辨率壁纸的解决方案。

Python脚本

import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import hashlib
import win32com.client

# 基础 URL
base_url = 'https://haowallpaper.com/?page='

# 要爬取的页数范围
start_page = 1
end_page = 2  # 例如爬取第 1 到第 5 页

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# 保存图片的目录
save_dir = 'high_res_wallpapers'
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 记录已经下载的图片 URL
downloaded_urls = set()


# 过滤掉无用的图像链接(如默认背景图等)
def is_relevant_image(url):
    placeholder_patterns = [
        '/_nuxt/defaultBag.',
        'placeholder',
        'default',
        'no_image',
        '404',
        'error'
    ]
    return not any(pattern in url for pattern in placeholder_patterns)


# 获取高分辨率图片的 URL
def get_high_res_image_url(img_tag):
    srcset = img_tag.get('srcset')
    if srcset:
        srcset_urls = [url.split(' ')[0] for url in srcset.split(',')]
        if srcset_urls:
            return srcset_urls[-1]
    return img_tag.get('src')


# 使用 Photoshop 提高图片分辨率
def enhance_image_with_photoshop(img_path):
    # 确保路径是绝对路径
    img_path = os.path.abspath(img_path)

    # 检查文件是否存在
    if not os.path.exists(img_path):
        print(f'文件不存在: {img_path}')
        return

    try:
        # 启动 Photoshop 应用
        app = win32com.client.Dispatch("Photoshop.Application")
        script_path = os.path.abspath('photoshop_script.jsx')

        # 打开图像
        doc = app.Open(img_path)

        # 设置 Photoshop 前台运行
        app.Visible = True

        # 执行 JSX 脚本
        app.DoJavaScriptFile(script_path)

        print(f'图片已通过 Photoshop 处理并保存到: {img_path}')
    except Exception as e:
        print(f'Photoshop 处理出错: {e}')


# 遍历每一页
for page in range(start_page, end_page + 1):
    url = base_url + str(page)
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        wallpaper_containers = soup.find_all('div', class_='card')
        for container in wallpaper_containers:
            link_tag = container.find('a', href=True)
            if link_tag:
                detail_page_url = urljoin('https://haowallpaper.com', link_tag['href'])
                detail_response = requests.get(detail_page_url, headers=headers)
                if detail_response.status_code == 200:
                    detail_soup = BeautifulSoup(detail_response.text, 'html.parser')
                    img_tags = detail_soup.find_all('img')
                    for img_tag in img_tags:
                        high_res_img_url = get_high_res_image_url(img_tag)
                        if not high_res_img_url:
                            continue
                        high_res_img_url = urljoin('https://haowallpaper.com', high_res_img_url)
                        if not is_relevant_image(high_res_img_url):
                            print(f'跳过无关图片: {high_res_img_url}')
                            continue
                        if high_res_img_url in downloaded_urls:
                            print(f'跳过已下载的图片: {high_res_img_url}')
                            continue
                        try:
                            img_response = requests.get(high_res_img_url, headers=headers)
                            if img_response.status_code == 200:
                                img_url_hash = hashlib.md5(high_res_img_url.encode('utf-8')).hexdigest()
                                img_name = f"{img_url_hash}.jpg"
                                img_path = os.path.join(save_dir, img_name)
                                with open(img_path, 'wb') as f:
                                    f.write(img_response.content)
                                print(f'已下载高分辨率图片: {img_name}')
                                downloaded_urls.add(high_res_img_url)
                                enhance_image_with_photoshop(img_path)
                            else:
                                print(f'下载失败: {high_res_img_url}, 状态码: {img_response.status_code}')
                        except requests.RequestException as e:
                            print(f'下载图片 {high_res_img_url} 时发生错误: {e}')
                else:
                    print(f'获取详细页面失败: {detail_page_url}, 状态码: {detail_response.status_code}')
    else:
        print(f'获取页面 {page} 失败。状态码: {response.status_code}')

photoshop_script.jsx脚本

// 获取当前打开的文档
var doc = app.activeDocument;

// 调整分辨率
var newResolution = 300;
doc.resizeImage(undefined, undefined, newResolution, ResampleMethod.BICUBIC);

// 保存增强后的图像
var saveOptions = new JPEGSaveOptions();
saveOptions.quality = 12;

// 构造输出文件路径
var outputFile = new File(doc.path + "/enhanced_" + doc.name);
doc.saveAs(outputFile, saveOptions, true);

// 关闭文档,不保存原文件的更改
doc.close(SaveOptions.DONOTSAVECHANGES);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值