这篇文章上次修改于 311 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

事由

其实很简单,就是水群的时候聊到了,大概就是把一张这样的图给做成九宫格变种

2021-11-30T11:50:16.png
成品如下

2021-11-30T11:51:02.png

我当时觉得挺简单的,无非就是切割图片罢了,至于第一块大图再切割一次就好了.效果如上(是的,出自我手).也没什么技术含量,大概就是取原图的宽高(必须相等,否则放到QQ会产生预期之外的结果)然后除以需要分割的块数,再拿这个尺寸去进行截取生成新图片最后写入.代码如下(P站是我本地服务器,测试用):

分割

2023/6/29 补充: 后面又想起来拿来用了一次, 所以代码可以说完全重构了, 当然也更好了 (话说发现原来的把横纵坐标写反了, 但由于都是处理方图所以也没发现
用到的库
alive_progress==3.1.4
Pillow==9.5.0
Requests==2.31.0
#!/usr/bin/python3

from PIL import Image
from alive_progress import alive_bar
import re
import requests
import os
import time

ROOT = os.path.expanduser("~/py")
TIMESTAMP = str(int(time.time()))


def main(file_path: str, x: int = 3, y: int = 3) -> None:
    """
    聚合 & 分离
    """

    dir_name = f"{ROOT}/output/{TIMESTAMP}"

    try:
        image = Image.open(file_path)
        width, height = image.size
        sub_w = width // x
        sub_h = height // y

    except IOError:
        import sys

        print("Unable to load image!")
        sys.exit(1)

    images = {}  # 存放小图片信息

    if x == 3 and y == 3:  # 额外生成一张 2*2 大小的图片
        box = (0, 0, width // 3 * 2, height // 3 * 2)
        images["2x2"] = image.crop(box)

    for col in range(x):
        for row in range(y):
            box = (
                col * sub_w,
                row * sub_h,
                (col + 1) * sub_w,
                (row + 1) * sub_h,
            )
            images[f"{col}_{row}"] = image.crop(box)

    os.makedirs(dir_name)

    with alive_bar(len(images), title="Image processing") as bar:
        for idx, name in enumerate(images):
            bar.text = "\u26a1 当前处理第 %d 张, 下标: %s" % (idx, name)
            # images[name].quantize(colors=256)
            images[name].save(f"{dir_name}/{TIMESTAMP}_{name}.png", "png", quality=80)
            bar()
        bar.title = "Processing complete"


if __name__ == "__main__":
    test = [
        r"/www/alist/Wallpaper/WA/收藏[91]PID[87493799_p0]_标题[冬马和纱]画师[非式]UID[14719710]_acg17.com.jpg",
        "https://api.kazusa.cc/ygo",
        "https://kazusa.cc/favicon.png",
    ]
    img_path = input("Input image path\n")
    img_path = img_path if img_path else test[2]
    file_path = f"{ROOT}/input/{TIMESTAMP}.jpg"

    if re.compile(r"^https?://.*").match(img_path):  # 判断是本地路径还是网络地址
        response = requests.get(
            img_path,
            headers={
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
            },
            stream=True,
        )
        response_size = int(response.headers.get("content-length", 0))

        print(
            "Image size: {:.2f}M".format(response_size / (1 << 20))
            if response_size
            else "Unable to get file size"
        )

        with open(file_path, mode="wb") as img, alive_bar(
            response_size, title="Image downloading"
        ) as bar:
            for data in response.iter_content(128):
                img.write(data)
                bar(len(data))
            bar.title = "Download complete"
    else:
        os.system("cp {:s} {:s}".format(img_path, file_path))

    x, y = map(int, xy if (xy := input("Enter aspect ratio:\n").split()) else [3, 3])
    main(file_path, x, y)

    print("Finished!")

写了一些多余的东西,但不影响使用就是了.这种东西网上一大堆,我稍微修改了一下图片的命名规则(强迫症),可惜的是放到Win10中并不理想,但还是挺舒服的

2021-11-30T11:58:09.png

拼接

其实原本是还做了一个拼接, 但想了想还是删掉了.

最初是拿来对抗某个站的恶心反盗, 它把漫画图片切割后逆序排列展示了, 爬出来完全看不了.

处理方法也是类似的, 只不过是反着来.