cannot write images using cv2.write
Question:
I am trying to convert images from .jpg to .png format using opencv. I have written the following code.
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)
This code will run without any errors. But, it is not writing new images in folder specified by args.rgb2png
I have checked all the paths multiple times. They are all correct. I do not know what am I doing wrong.
Can someone please help me with this?
Answers:
You should use argparse
to define the required arguments and their default values
You can try this approach
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}')
In this script you can convert all images in one folder to png, run it by using this command:
python your_script.py --image <path_to_images> --rgb2png <path_to_save_png>
I am trying to convert images from .jpg to .png format using opencv. I have written the following code.
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)
This code will run without any errors. But, it is not writing new images in folder specified by args.rgb2png
I have checked all the paths multiple times. They are all correct. I do not know what am I doing wrong.
Can someone please help me with this?
You should use argparse
to define the required arguments and their default values
You can try this approach
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}')
In this script you can convert all images in one folder to png, run it by using this command:
python your_script.py --image <path_to_images> --rgb2png <path_to_save_png>