分类: 学习笔记

  • 【Python】一个适配本网站性能的压缩图片脚本

    由于网站的服务器与带宽性能有限,因此上传的图片被严格限制了边长和大小,使用脚本可以有效对想要上传的文件进行批处理,节省时间。

    参考代码:

    实现对jpg、png、webp等文件的压缩,限制最长边2560,大小2m以下,随机文件名且保留可能含有的EXIF信息。

    暂不支持中文路径文件夹。

    import os
    import random
    import string
    from PIL import Image
    import tkinter as tk
    from tkinterdnd2 import TkinterDnD, DND_FILES
    
    # 生成随机文件名
    def generate_random_filename(length=10):
        """生成指定长度的随机文件名(字母和数字)"""
        return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
    
    def compress_image(input_path, max_size=2560, max_file_size=2 * 1024 * 1024, quality=85):
        """对图片进行压缩和尺寸调整,保留EXIF信息"""
        img = Image.open(input_path)
        exif_data = img.info.get("exif")  # 获取EXIF数据
        width, height = img.size
        file_size = os.path.getsize(input_path)
        
        # 生成随机文件名前缀
        random_prefix = generate_random_filename(10)  # 10 位随机字符前缀
    
        # 获取文件扩展名
        file_extension = os.path.splitext(input_path)[1].lower()
    
        # 如果图片是RGBA格式,将其转换为RGB格式,只对JPEG格式需要转换
        if img.mode == 'RGBA' and file_extension not in ['.jpeg', '.jpg']:
            output_path = os.path.join(os.path.dirname(input_path), f"{random_prefix}_已压缩{file_extension}")
            if exif_data:
                img.save(output_path, quality=quality, optimize=True, exif=exif_data)
            else:
                img.save(output_path, quality=quality, optimize=True)
        else:
            # 如果图片不需要压缩,直接保存为“无需压缩”版本
            if max(width, height) <= max_size and file_size <= max_file_size:
                output_path = os.path.join(os.path.dirname(input_path), f"{random_prefix}_无需压缩{file_extension}")
                if exif_data:
                    img.save(output_path, exif=exif_data)
                else:
                    img.save(output_path)
            else:
                # 如果需要调整大小
                if max(width, height) > max_size:
                    scaling_factor = max_size / float(max(width, height))
                    new_size = (int(width * scaling_factor), int(height * scaling_factor))
                    img = img.resize(new_size, Image.LANCZOS)
                
                # 保存为JPEG格式,质量为85
                output_path = os.path.join(os.path.dirname(input_path), f"{random_prefix}_已压缩.jpg")
                if exif_data:
                    img.save(output_path, quality=quality, optimize=True, exif=exif_data)
                else:
                    img.save(output_path, quality=quality, optimize=True)
                
                # 如果文件过大,继续降低质量,直到符合要求
                while os.path.getsize(output_path) > max_file_size and quality > 10:
                    quality -= 10
                    if exif_data:
                        img.save(output_path, quality=quality, optimize=True, exif=exif_data)
                    else:
                        img.save(output_path, quality=quality, optimize=True)
        
        return output_path
    
    # 处理拖动的文件
    def on_drop(event):
        file_paths = event.data.split()
        process_images(file_paths)
    
    # 批量处理图片
    def process_images(file_paths):
        processed_files = []
        for file_path in file_paths:
            if is_image_file(file_path):
                processed_file = compress_image(file_path)
                processed_files.append(processed_file)
        print(f"处理完成的文件: {processed_files}")
    
    # 判断文件是否为图片
    def is_image_file(file_path):
        try:
            img = Image.open(file_path)
            return True
        except IOError:
            return False
    
    # 创建GUI界面
    root = TkinterDnD.Tk()
    root.title("图片压缩工具")
    root.geometry("600x400")
    
    label = tk.Label(root, text="将图片拖到这里", padx=20, pady=20)
    label.pack(padx=20, pady=20)
    
    # 绑定拖拽事件
    root.drop_target_register(DND_FILES)
    root.dnd_bind('<<Drop>>', on_drop)
    
    # 运行主循环
    root.mainloop()
    
  • 【Python】调用微信OCR来对输入的图片进行文字识别

    参考资料:

    1、三年磨一剑——微信OCR图片文字提取-腾讯云开发者社区-腾讯云

    2、可供独立使用且最小依赖的微信 OCR 功能包 – 吾爱破解 – 52pojie.cn

    3、GitHub – kanadeblisst00/wechat_ocr: 使用Python调用微信本地ocr服务

    4、[原创]Python调用微信OCR识别文字和坐标-编程技术-看雪-安全社区|安全招聘|kanxue.com

    示例代码:

    这段简易代码实现创建一个GUI,输入一个图片,将图片中的文字进行OCR,按段落生成到一个TXT中。

    import os
    import json
    import time
    import tkinter as tk
    from tkinter import filedialog, messagebox
    from wechat_ocr.ocr_manager import OcrManager, OCR_MAX_TASK_ID
    
    
    def ocr_result_callback(img_path: str, results: dict):
        save_text_to_txt(results, img_path)
    
    
    def save_text_to_txt(ocr_results, original_image_path):
        if 'ocrResult' in ocr_results and isinstance(ocr_results['ocrResult'], list):
            all_text = ""
            for result in ocr_results['ocrResult']:
                text = result.get('text', '')
                if text:
                    all_text += text + "\n"
    
            base_name, ext = os.path.splitext(original_image_path)
            txt_path = f"{base_name}_ocr_result.txt"
            with open(txt_path, 'w', encoding='utf-8') as f:
                f.write(all_text)
            print(f"已保存OCR结果到: {txt_path}")
        else:
            print("OCR结果不符合预期格式。")
    
    
    def select_image():
        file_path = filedialog.askopenfilename(
            title="选择图片",
            filetypes=(("Image files", "*.png;*.jpg;*.jpeg;*.bmp;*.tiff"), ("All files", "*.*"))
        )
        if not file_path:
            return
    
        wechat_ocr_dir = r"C:\Users\YourID\AppData\Roaming\Tencent\WeChat\XPlugin\Plugins\WeChatOCR79\extracted\WeChatOCR.exe"
        wechat_dir = r"C:\Program Files\Tencent\WeChat\[3.9.12.17]"
        ocr_manager = OcrManager(wechat_dir)
        ocr_manager.SetExePath(wechat_ocr_dir)
        ocr_manager.SetUsrLibDir(wechat_dir)
        ocr_manager.SetOcrResultCallback(ocr_result_callback)
        ocr_manager.StartWeChatOCR()
    
        ocr_manager.DoOCRTask(file_path)
    
        while ocr_manager.m_task_id.qsize() > 0 or ocr_manager.IsOcrRunning():
            time.sleep(0.5)
    
        ocr_manager.KillWeChatOCR()
        messagebox.showinfo("完成", "文字提取并保存完成!")
    
    
    def main():
        root = tk.Tk()
        root.title("OCR文字提取到txt工具")
        root.geometry("300x100")
    
        btn_select = tk.Button(root, text="选择图片", command=select_image)
        btn_select.pack(pady=20)
    
        root.mainloop()
    
    
    if __name__ == "__main__":
        main()
    

  • 【Python】本地运行的,以缩略图搜原图脚本

    像我这种不太注重整理的人,在想找一张原图时候往往很抓狂,因为文件夹太多了!

    因此今天问ChatGPT“协调”了一段Python代码,可以有效的在本地用jpg缩略图来搜索原jpg图。

    代码主要用到PIL库,本来想用OpenCV但是实在是搞不定中文路径问题,本着能用就行的原则,因此只能使用PIL,代码如下👇。