• opencv 打开中文路径图报错


    img = cv.imread(中文图, 1)

    [ WARN:0@6.414] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_(‘D:/download/中文.png’): can’t open/read file: check file path/integrity

    转换编码无法解决

    file_path = path.encode(‘utf-8’).decode(‘utf-8’) # 确保路径是UTF-8编码

    标准格式打开无法解决

    path = os.path.normpath(path)

    正确方式,更换打开中文图方法

    中文图

    完整代码

    import time
    
    import cv2 as cv
    import os
    import tkinter as tk
    from tkinter import filedialog
    from tkinter import messagebox
    
    import numpy as np
    from PIL import ImageTk, Image
    
    
    def open_image_dialog():
        global file_path
        file_path = filedialog.askopenfilename(title="选择图片", filetypes=[("图片文件", "*.jpg;*.jpeg;*.png;*.bmp")])
        if file_path:
            print(file_path)
            display_image()
    def convert_to_gray():
        pass
    def display_image():
        
        # img = cv.imread(file_path , 1)  # 路径或名字有中文时,无法读取图片
        img = cv.imdecode(np.fromfile(file_path, dtype=np.uint8), cv.IMREAD_COLOR)
    
        rgb_image = cv.cvtColor(img, cv.COLOR_BGR2RGB)
        # 将 NumPy 数组转换为 PIL Image,并调整大小
        img = Image.fromarray(rgb_image)
    
        img = img.resize((300, 300), Image.LANCZOS)
        # 将 PIL Image 转换为 PhotoImage
        photo = ImageTk.PhotoImage(img)
        # 在 tkinter 的 Label 上显示 PhotoImage
        image_label.config(image=photo)
        image_label.image = photo  # 保持对 PhotoImage 的引用
    
        pass
    
    # 创建 tkinter 窗口
    root = tk.Tk()
    root.title("图片转灰度工具")
    
    # 创建一个 Frame 用于放置按钮
    button_frame = tk.Frame(root)
    button_frame.pack(pady=10)
    
    # 创建一个 Label 用于显示图片信息
    image_label = tk.Label(root, text="图片展位", font=("Helvetica", 12))
    image_label.pack(pady=20)
    # 创建一个 Label 用于显示图片标题
    image_title_label = tk.Label(root, text="图片path", font=("Helvetica", 12))
    image_title_label.pack(pady=5)
    
    
    # 创建一个 Button 用于选择图片,并放置到 Frame 中
    choose_button = tk.Button(button_frame, text="选择图片", command=open_image_dialog)
    choose_button.pack(side=tk.LEFT, padx=5)
    
    # 创建一个 Button 用于转换图片,并放置到 Frame 中
    convert_button = tk.Button(button_frame, text="转换为灰度", command=convert_to_gray)
    convert_button.pack(side=tk.LEFT, padx=5)
    
    # 创建一个 Button 用于取消操作,并放置到 Frame 中
    cancel_button = tk.Button(button_frame, text="取消", command=root.destroy)
    cancel_button.pack(side=tk.LEFT, padx=5)
    
    # 运行 tkinter 主循环
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    | 在这里插入图片描述 | 在这里插入图片描述 |

  • 相关阅读:
    aasist-bladedisc 音频反欺骗算法模型
    查看docker run 时启动命令
    PacBio全长扩增子测序发现酵母益生菌可提高黑山羊免疫力
    Elasticsearch
    [模块]ES6与cjs的混合开发
    Java_多态
    插入排序改进 将交换变成赋值语句 优点适用于近乎有序的序列
    模板初阶学习
    Python威布尔分布
    REDIS三主三从集群搭建笔记(redis版本5.0.8)
  • 原文地址:https://blog.csdn.net/flyingsir_zw/article/details/137959231