我正在尝试使用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
指定的文件夹中写入新图像
所有的路径我都检查了多次,都是正确的,我不知道我做错了什么。
有人能帮帮我吗?
1条答案
按热度按时间r6hnlfcb1#
您应该使用
argparse
来定义所需的参数及其默认值。在此脚本中,您可以将一个文件夹中的所有图像转换为png,通过使用以下命令运行它: