Python cv2显示低质量图像。

huangapple go评论77阅读模式
英文:

Python cv2 displays low quality image

问题

以下是翻译好的部分:

我制作了一个Python脚本,它不断检查一个目录中的.png文件。如果存在.png文件,它会在一个新创建的窗口中显示该.png文件,然后删除该.png文件。我使用的是Windows 11和Python 3.11.3。

唯一的问题是Python脚本显示的图像质量较低。图像本身是高质量的。如何修改我的代码以便以高分辨率显示.png文件?

这是代码:

import os
import cv2

desktop_path = os.path.expanduser("~/Desktop")  # 获取桌面路径

while True:
    # 列出桌面目录中的所有文件
    files = os.listdir(desktop_path)

    for file in files:
        if file.lower().endswith('.png'):  # 检查文件是否是PNG图像
            file_path = os.path.join(desktop_path, file)

            # 使用OpenCV读取图像
            image = cv2.imread(file_path)

            # 检查图像是否成功加载
            if image is not None:
                # 调整图像大小为较小的尺寸
                resized_image = cv2.resize(image, (800, 600))

                # 创建一个新窗口并显示调整大小后的图像
                cv2.imshow('PNG Viewer', resized_image)
                cv2.waitKey(2000)  # 等待1秒

                # 删除PNG文件
                os.remove(file_path)

    # 在下次检查之前延迟几秒
    cv2.waitKey(2000)
英文:

I made a Python script that keeps checking a directory for .png´s. If there is a png, it displays the png in a newly created window and deletes the png afterwards. I'm on Windows 11 and I use Python 3.11.3.

The only problem is that the Python script shows the image with low quality. The image itself is of high quality. How can I change my code so the png´s are shown with high resolution?

This is the code:

import os
import cv2

desktop_path = os.path.expanduser("~/Desktop")  # Get the path to the Desktop

while True:
    # List all files in the Desktop directory
    files = os.listdir(desktop_path)

    for file in files:
        if file.lower().endswith('.png'):  # Check if the file is a PNG image
            file_path = os.path.join(desktop_path, file)

            # Read the image using OpenCV
            image = cv2.imread(file_path)

            # Check if the image was successfully loaded
            if image is not None:
                # Resize the image to a smaller size
                resized_image = cv2.resize(image, (800, 600))

                # Create a new window and display the resized image
                cv2.imshow('PNG Viewer', resized_image)
                cv2.waitKey(2000)  # Wait for 1 second

                # Delete the PNG file
                os.remove(file_path)

    # Delay for a few seconds before checking again
    cv2.waitKey(2000)

答案1

得分: 1

更改这一行代码以更改图像的大小/分辨率。现在宽度为1280像素,高度为720像素。您可以将数字更改为更大的值以获得更大的尺寸。

resized_image = cv2.resize(image, (1280, 720))

或者您可以完全删除调整大小,以便始终以原始尺寸显示。

import os
import cv2

    desktop_path = os.path.expanduser("~/Desktop")  # 获取桌面路径
    
    while True:
        # 列出桌面目录中的所有文件
        files = os.listdir(desktop_path)
    
        for file in files:
            if file.lower().endswith('.png'):  # 检查文件是否为PNG图像
                file_path = os.path.join(desktop_path, file)
    
                # 使用OpenCV读取图像
                image = cv2.imread(file_path)
    
                # 检查图像是否成功加载
                if image is not None:
                    
                    # 显示未调整大小的图像
                    cv2.imshow('PNG Viewer', image)
                    cv2.waitKey(2000)  # 等待1秒
    
                    # 删除PNG文件
                    os.remove(file_path)
    
        # 延迟几秒再次检查
        cv2.waitKey(2000)
英文:

Change this one line which changes the size/resolution of the image. 1280 pixels is the width and 720 pixels is the height now. You can change the numbers to a higher value for a bigger size.

resized_image = cv2.resize(image, (1280, 720))

Or you can remove the resize entirely so that it will always show at the original size.

import os
import cv2

    desktop_path = os.path.expanduser("~/Desktop")  # Get the path to the Desktop
    
    while True:
        # List all files in the Desktop directory
        files = os.listdir(desktop_path)
    
        for file in files:
            if file.lower().endswith('.png'):  # Check if the file is a PNG image
                file_path = os.path.join(desktop_path, file)
    
                # Read the image using OpenCV
                image = cv2.imread(file_path)
    
                # Check if the image was successfully loaded
                if image is not None:
                    
                    # Show the image without resize
                    cv2.imshow('PNG Viewer', image)
                    cv2.waitKey(2000)  # Wait for 1 second
    
                    # Delete the PNG file
                    os.remove(file_path)
    
        # Delay for a few seconds before checking again
        cv2.waitKey(2000)

答案2

得分: 0

你需要使用cv.resize()INTER_*标志。

默认为最近邻采样。

请尝试使用INTER_AREA

resized_image = cv2.resize(image, (800, 600), interpolation=cv2.INTER_AREA)

另外,请使用GUI工具包。OpenCV用于计算机视觉,不适用于显示图片。它不擅长GUI界面,但在计算机视觉方面表现出色。

英文:

You need to use cv.resize() with an INTER_* flag.

The default is nearest neighbor sampling.

Try INTER_AREA instead.

resized_image = cv2.resize(image, (800, 600), interpolation=cv2.INTER_AREA)

Also: please use a GUI toolkit. OpenCV is for computer vision, not for displaying pictures. It's not good at GUIs. It's good at computer vision.

huangapple
  • 本文由 发表于 2023年6月1日 19:46:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定