标签: 小工具

  • 【HTML】输入Pixiv ID跳转对应画师主页

    Pixiv的应用内部居然没这个功能,不科学,非常基础的功能。

    收录进了小工具页面。👉点击使用小工具

    <body>
        <h1>Pixiv用户跳转</h1>
        <input id="input-id" type="text" placeholder="请输入数字ID" oninput="validateInput()" />
        <div>
            <button onclick="generateLink()">生成链接</button>
            <button onclick="openLink()">跳转</button>
        </div>
        <div id="output"></div>
    
        <script>
            function validateInput() {
                const input = document.getElementById('input-id');
                input.value = input.value.replace(/\D/g, ''); // 只保留数字
            }
    
            function generateLink() {
                const input = document.getElementById('input-id').value.trim();
                const output = document.getElementById('output');
                if (input === '') {
                    output.innerHTML = '<p style="color: red;">请输入有效的数字ID!</p>';
                    return;
                }
                const link = `https://www.pixiv.net/member.php?id=${input}`;
                output.innerHTML = `<a href="${link}" target="_blank">点击跳转到 Pixiv 用户页面</a>`;
            }
    
            function openLink() {
                const input = document.getElementById('input-id').value.trim();
                if (input === '') {
                    alert('您还没有输入ID~');
                    return;
                }
                const link = `https://www.pixiv.net/member.php?id=${input}`;
                window.open(link, '_blank');
            }
        </script>
    </body>
  • 【Node.js】批量导出微信联系人到Excel表格(基于Wechaty)

    用到的库:

    wechaty: 一个用于开发微信聊天机器人的框架。

    qrcode-terminal: 用于生成终端二维码的库。

    xlsx: 用于处理 Excel 文件的库。

    fspath: Node.js 的内置文件系统模块,用于文件操作。

    file-box: 用于处理文件(如图片)的库。

    uuid: 用于生成唯一标识符(UUID)的库。

    工作原理:

    基于wechaty的api,获取相应信息并批量导出到excel表格中,头像文件夹单独放置。可以在WPS中依靠UUID生成的唯一ID来快速批量嵌入头像。

    可惜标签功能不在Wechaty的功能中,也没法导出手机号等更有价值的信息,目前能导出的信息不多,图一乐。

    const { WechatyBuilder } = require('wechaty');
    const qrcode = require('qrcode-terminal');
    const xlsx = require('xlsx');
    const fs = require('fs');
    const path = require('path');
    const { FileBox } = require('file-box');
    const { v4: uuidv4 } = require('uuid');  // 引入 UUID 库
    
    const outputDir = './avatars/';  // 存储头像的文件夹
    
    // 确保头像目录存在
    if (!fs.existsSync(outputDir)) {
      fs.mkdirSync(outputDir);
    }
    
    class WeChaty {
      bot = null;
    
      constructor() {
        this.bot = WechatyBuilder.build();
        this.bot.on('scan', code => {
          qrcode.generate(code, { small: true });
        });
        this.bot.on('login', user => {
          console.log(`登录成功,欢迎 ${user}`);
          this.waitForContacts();  // 登录后开始等待联系人同步
        });
      }
    
      // 随机延时函数,返回一个 Promise,用于模拟短时延时
      delay(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
      }
    
      // 每10秒检查一次联系人数量,最多检查10次
      async waitForContacts() {
        let previousCount = 0;
        let currentCount = 0;
        let attempts = 0;
    
        while (attempts < 10) {
          const allContacts = await this.bot.Contact.findAll();  // 获取所有联系人
          currentCount = allContacts.length;
    
          if (currentCount === previousCount) {
            console.log('联系人数量没有变化,开始导出');
            break;  // 如果联系人数量没有变化,则认为同步完成
          }
    
          console.log(`当前联系人数量: ${currentCount}, 尝试次数: ${attempts + 1}`);
          previousCount = currentCount;
          attempts++;
    
          await this.delay(10000);  // 每次等待10秒再检查
        }
    
        if (attempts >= 10) {
          console.log('尝试多次后联系人数量没有变化,开始导出');
        }
    
        // 导出联系人
        this.getAllFriendContacts();
      }
    
      // 获取所有好友联系人
      async getAllFriendContacts() {
        const allContacts = await this.bot.Contact.findAll();  // 获取所有联系人
    
        // 只筛选出好友联系人
        const friendContacts = allContacts.filter(contact => 
          contact.friend() && contact.type() === this.bot.Contact.Type.Individual
        );
    
        console.log(`总共获取了 ${friendContacts.length} 个好友联系人`);
    
        if (friendContacts.length > 0) {
          await this.exportContacts(friendContacts);  // 导出好友联系人
        }
      }
    
      // 获取并保存联系人头像
      async saveAvatar(contact) {
        try {
          const avatarFile = await contact.avatar();
          if (avatarFile) {
            const cleanName = contact.name().replace(/[\/\\:*?"<>|]/g, '_');  // 清理非法字符
            const uniqueName = `${cleanName}_${uuidv4()}`;  // 使用清理后的昵称 + UUID 作为文件名
            const avatarPath = path.join(outputDir, uniqueName + '.jpg');  // 存储路径
    
            await avatarFile.toFile(avatarPath, true);  // 保存头像文件
            return uniqueName;  // 返回不带后缀的文件名
          }
        } catch (error) {
          console.log(`获取 ${contact.name()} 头像失败`);
        }
        return '';  // 如果获取失败,返回空字符串
      }
    
      // 导出联系人信息
      async exportContacts(allContacts) {
        try {
          const contactList = [];
    
          const avatarPromises = allContacts.map(async (contact) => {
            // 获取头像文件名
            const avatarFileName = await this.saveAvatar(contact);  
    
            // 只导出有头像的联系人,且即使没有昵称也会保留
            return {
              昵称: contact.name().trim() || '无昵称',  // 如果没有昵称,则使用 '无昵称'
              备注: (await contact.alias())?.trim() || '',  // 获取备注信息
              性别: contact.gender() === this.bot.Contact.Gender.Male ? '男' : 
                    contact.gender() === this.bot.Contact.Gender.Female ? '女' : '未知',  // 性别
              省份: contact.province()?.trim() || '',  // 获取省份
              城市: contact.city()?.trim() || '',  // 获取城市
              文件名: avatarFileName,  // 保存文件名(不带后缀)
            };
          });
    
          // 等待所有头像保存完毕
          const contactData = await Promise.all(avatarPromises);
    
          // 使用 xlsx 库导出 Excel 表格
          const ws = xlsx.utils.json_to_sheet(filteredContactData);  // 将联系人信息转换为 Excel 表格
          const wb = xlsx.utils.book_new();  // 创建一个新的 Excel 工作簿
          xlsx.utils.book_append_sheet(wb, ws, '联系人');  // 将联系人数据添加到工作簿
    
          // 将工作簿保存为 Excel 文件
          xlsx.writeFile(wb, 'contacts_with_details.xlsx');
          console.log('好友联系人信息已成功导出到 contacts_with_details.xlsx');
        } catch (error) {
          console.error('导出联系人信息失败:', error);
        }
      }
    
      run() {
        this.bot.start();
      }
    }
    
    new WeChaty().run();
    
  • 【Python】对彩色LOGO进行批量反白处理

    为了制作一些高大上的风格化 PPT,有时我们需要很多客户的反白色LOGO,以符合当下的一些设计潮流。

    目前常用的做法是在 PPT中对图片本身进行亮度调整,可以理解为一键过曝,但是这对于一些本身就含有白色的图片不适用,也无法处理JPG的图片,更没法快速将反白的图片进行批量保存,以便存储成库,在其他场景继续使用。

    因此使用脚本可以防止原来的白色部分混成一团,预先对原LOGO白色区域进行透明化,然后对其他颜色区域反白。

    这个脚本目前适用于我的工作环境,包含一些问题,例如如果原来的图标包含白色文字,这样会将其透明化,因此还需按照使用情况进行调整。

    后续考虑加入对JPG进行处理的过程,原理上是对白色部分预先透明度处理,然后后续步骤基本一致,不过使用JPG作为LOGO的客户较少,该功能并不急迫。若要实现该功能,可能需要使用OCR对文字部分预先识别处理,流程上麻烦不少,不过由于wechat-ocr的强大功能,应该也可以稳定呈现,wechat-ocr此前有过一些实践,效果出众,推荐大家使用。

    此外脚本尚未测试灰色部分是否会有问题,目前感觉应该会有问题,若使用中有其他问题会随时更新。

    import tkinter as tk
    from tkinter import filedialog
    from PIL import Image, ImageTk
    import random
    import string
    class ImageProcessor(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title("Logo Image Processor")
            self.geometry("600x600")
            self.image_path = None
            self.images = []  # 用于存储多个图像
            self.image_label = tk.Label(self)
            self.image_label.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
            
            # 设置拖放区域
            self.drop_area = tk.Label(self, text="拖动PNG图片到此区域", relief="solid", width=30, height=4)
            self.drop_area.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
            self.drop_area.bind("<Enter>", self.on_drag_enter)
            self.drop_area.bind("<Leave>", self.on_drag_leave)
            self.drop_area.bind("<ButtonRelease-1>", self.on_drop)
            # 添加处理按钮
            self.process_button = tk.Button(self, text="处理图片并保存", command=self.process_images)
            self.process_button.pack(pady=10)
        def on_drag_enter(self, event):
            self.drop_area.config(bg="lightblue")
        def on_drag_leave(self, event):
            self.drop_area.config(bg="white")
        def on_drop(self, event):
            file_paths = filedialog.askopenfilenames(filetypes=[("PNG files", "*.png")])
            if file_paths:
                self.load_images(file_paths)
        def load_images(self, paths):
            self.images = []  # 清空当前图像列表
            for path in paths:
                image = Image.open(path).convert("RGBA")  # 确保加载为RGBA格式以处理透明度
                self.images.append((path, image))  # 存储图像及其路径
            if self.images:
                self.display_image(self.images[0][1])  # 显示第一张图片
        def display_image(self, image):
            image_tk = ImageTk.PhotoImage(image)
            self.image_label.config(image=image_tk)
            self.image_label.image = image_tk
        def process_images(self):
            if self.images:
                for original_path, image in self.images:
                    # 获取图像的每个像素
                    pixels = image.load()
                    width, height = image.size
                    
                    for x in range(width):
                        for y in range(height):
                            r, g, b, a = pixels[x, y]
                            
                            # 将白色部分透明化
                            if r == 255 and g == 255 and b == 255:
                                pixels[x, y] = (255, 255, 255, 0)  # 将白色变为透明
                            elif a != 0:  # 如果是非透明区域
                                # 将所有非透明区域变为纯白色
                                pixels[x, y] = (255, 255, 255, a)  # 变为白色,保持原透明度
                    
                    # 生成随机字符并保存处理后的图像
                    random_suffix = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
                    output_path = f"processed_logo_{random_suffix}.png"
                    image.save(output_path)
                    print(f"处理后的LOGO图片已保存为 {output_path}")
                    
                # 更新显示处理后的图片(显示第一张图像)
                self.display_image(self.images[0][1])
    if __name__ == "__main__":
        app = ImageProcessor()
        app.mainloop()
    

    此外,还可以对JPG进行处理:

    注意保证输入图片的分辨率,其平滑操作对分辨率会有一定的损失。

    import tkinter as tk
    from tkinter import filedialog, messagebox
    from PIL import Image, ImageFilter
    import random
    import string
    import os
    class ImageProcessor(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title("Logo Image Processor")
            self.geometry("400x200")  # 设置主窗口大小
            self.image_path = None
            self.images = []  # 用于存储多个图像
            self.processed_images = []  # 用于存储处理后图像路径
            # 设置拖放区域
            self.drop_area = tk.Label(self, text="拖动PNG或JPG图片到此区域", relief="solid", width=30, height=4)
            self.drop_area.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
            self.drop_area.bind("<Enter>", self.on_drag_enter)
            self.drop_area.bind("<Leave>", self.on_drag_leave)
            self.drop_area.bind("<ButtonRelease-1>", self.on_drop)
            # 添加处理按钮
            self.process_button = tk.Button(self, text="处理图片并保存", command=self.process_images)
            self.process_button.pack(pady=10)
            # 添加选择输出路径按钮
            self.output_dir = None
            self.select_output_button = tk.Button(self, text="选择保存路径", command=self.select_output_dir)
            self.select_output_button.pack(pady=5)
        def on_drag_enter(self, event):
            self.drop_area.config(bg="lightblue")
        def on_drag_leave(self, event):
            self.drop_area.config(bg="white")
        def on_drop(self, event):
            file_paths = filedialog.askopenfilenames(filetypes=[("Image files", "*.png *.jpg *.jpeg")])
            if file_paths:
                self.load_images(file_paths)
        def load_images(self, paths):
            self.images = []  # 清空当前图像列表
            for path in paths:
                try:
                    image = Image.open(path)
                    # 将JPG图像转换为支持透明度的RGBA格式
                    if image.mode != "RGBA":
                        image = image.convert("RGBA")
                    self.images.append((path, image))  # 存储图像及其路径
                except Exception as e:
                    print(f"无法加载图像 {path}: {e}")
                    messagebox.showerror("错误", f"无法加载图像 {path}")
                    
        def select_output_dir(self):
            self.output_dir = filedialog.askdirectory()
            if self.output_dir:
                print(f"选择的输出目录是: {self.output_dir}")
            
        def process_images(self):
            if not self.images:
                messagebox.showwarning("警告", "请先加载图片")
                return
            
            if not self.output_dir:
                messagebox.showwarning("警告", "请先选择保存路径")
                return
            for original_path, image in self.images:
                pixels = image.load()
                width, height = image.size
                
                for x in range(width):
                    for y in range(height):
                        r, g, b, a = pixels[x, y]
                        
                        # 将接近白色的区域透明化,设置阈值范围 (240, 240, 240) 到 (255, 255, 255)
                        if r >= 240 and g >= 240 and b >= 240:
                            pixels[x, y] = (255, 255, 255, 0)  # 将接近白色的部分变为透明
                        elif a != 0:  # 如果是非透明区域
                            # 将所有非透明区域变为纯白色
                            pixels[x, y] = (255, 255, 255, a)  # 变为白色,保持原透明度
                # 对图像进行边缘平滑处理,减少杂色
                image = image.filter(ImageFilter.GaussianBlur(radius=2))
                
                # 生成随机字符并保存处理后的图像
                random_suffix = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
                output_path = os.path.join(self.output_dir, f"processed_logo_{random_suffix}.png")
                image.save(output_path)
                print(f"处理后的LOGO图片已保存为 {output_path}")
                self.processed_images.append(output_path)
    if __name__ == "__main__":
        app = ImageProcessor()
        app.mainloop()