opencv 无法使用cv2.write写入图像

2fjabf4q  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(188)

我正在尝试使用opencv将图像从.jpg转换为.png格式。我编写了以下代码。

if __name__ == '__main__':
    
    logging.info(r'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
    logging.info('Starting to make a .lst file')

    
    # png_to_png = glob.globoos.path.join(args.jpg2png, '*.png')
    images_list = glob.glob(os.path.join(args.image, "*.jpg"))
    logging.info(f'images_list example: {images_list[3]}')
    # mask_list = glob.glob(os.path.join(args.masks, "*.png"))

    # Changing rgb images from jpg to png images and saving it in a new folder.
    # Check if the folder for storing the converted png images exists or not. If not, create a folder
    # if not os.path.exists(args.rgb2png):
        # os.mkdir(args.rgb2png)

    for index, img in enumerate(images_list):
        old_img_name = img.split('/')[-1]
        img_name = old_img_name.split('.')[0]
        logging.info('The following info is regarding image being converted from jpg to png')
        logging.info(f'img: {img}')
        logging.info(f'img_name: {img_name}')
        new_name = img_name + '.png'
        logging.info(f'new_name: {new_name}')
        logging.info(f'final image path: {os.path.join(args.rgb2png, new_name)}')
        png_path = os.path.join(args.rgb2png, new_name) 
        
        
        color_rgb = cv2.imread(img)
        cv2.imwrite(png_path, color_rgb)

此代码将运行没有任何错误。但是,它不是在args.rgb2png指定的文件夹中写入新图像
所有的路径我都检查了多次,都是正确的,我不知道我做错了什么。
有人能帮帮我吗?

r6hnlfcb

r6hnlfcb1#

您应该使用argparse来定义所需的参数及其默认值。

import os
import cv2
import glob
import argparse
import logging

logging.basicConfig(level=logging.INFO)

def convert_image(img_path, output_dir):
    old_img_name = os.path.basename(img_path)
    img_name, _ = os.path.splitext(old_img_name)
    new_name = img_name + '.png'
    png_path = os.path.join(output_dir, new_name)

    color_rgb = cv2.imread(img_path)
    cv2.imwrite(png_path, color_rgb)
    return png_path

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--image', help='Path to the folder containing .jpg images', required=True)
    parser.add_argument('--rgb2png', help='Path to the folder where .png images will be saved', required=True)
    args = parser.parse_args()

    logging.info('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
    logging.info('Starting to convert .jpg images to .png format')

    images_list = glob.glob(os.path.join(args.image, "*.jpg"))
    logging.info(f'images_list example: {images_list[0]}')

    # Create the folder for storing the converted png images if it doesn't exist
    os.makedirs(args.rgb2png, exist_ok=True)

    # Convert all images using list comprehension
    converted_images = [convert_image(img, args.rgb2png) for img in images_list]

    logging.info(f'Converted images: {converted_images}')

在此脚本中,您可以将一个文件夹中的所有图像转换为png,通过使用以下命令运行它:

python your_script.py --image <path_to_images> --rgb2png <path_to_save_png>

相关问题