0

I am trying to read images in a folder and save them after doing some processes.

I using following code to save my images:

   import cv2

i = 0
while i < len(bright_images):
    cv2.imwrite(f'/path/image_{i}.png', cv2.cvtColor(bright_images[i],cv2.COLOR_BGR2RGB)
    i += 1

But the problem is that when writing images all red colors in my images turn to be blue, colors change completely, it seems as if it saves based on BGR color instead of RGB.

How do I fix this issue?

FYI, I am reading images using this code:

def load_images(path):
    image_list=[]
    images= glob.glob(path)
    images = natsorted(images)

    for index in range(len(images)):
        image= cv2.cvtColor(cv2.imread(images[index]),cv2.COLOR_BGR2RGB)
        image_list.append(cv2.resize(image,(1920,1080)))

    return image_list
nikki
  • 365
  • 4
  • 20
  • 1
    Does this answer your question? [why cv2.imwrite() changes the color of pics?](https://stackoverflow.com/questions/42406338/why-cv2-imwrite-changes-the-color-of-pics) – snwflk Jun 09 '20 at 00:03
  • Well, I am not using skimage.io to read my images, I could not relate that answer – nikki Jun 09 '20 at 00:12
  • @CrisLuengo, could you please explain by editing the code? I am getting an error after doing what you said. error: "TypeError: Expected Ptr for argument 'src' " – nikki Jun 09 '20 at 01:05
  • 1
    Look, OpenCV works by default in BGR ordering. When you read the image with `cv2.imread`, you get a matrix in BGR order. When you save with `cv2.imwrite`, it expects a matrix in BGR order. But because you used `cv2.cvtColor(...,cv2.COLOR_BGR2RGB)`, you matrix is in RGB order, meaning that `cv2.imwrite` will do the wrong thing and mix up red and blue. Just leave out the call to `cv2.cvtColor`, and work in BGR, or add another call to `cv2.cvtColor` just before you save the image. – Cris Luengo Jun 09 '20 at 03:38

0 Answers0