Search code examples
pythonpngjpegimage-conversion

Convert plenty of jpeg images to png in batch


For work, I need to convert about 200 jpeg images to png. I've tried PIL but I've no idea how to solve this problem. Any ideas?

import cv2
import glob
i=0

images = glob.glob("*.jpg")

for i in images:
    print("start")
    img = cv2.imread(images, 1)  
    cv2.imwrite(images, img, [cv2.IMWRITE_PNG_COMPRESSION, 1])
    print("end")

Solution

  • You are passing list of images to cv2.imread() and also there is an error in cv2.imwrite().

    Try this:

    import cv2
    import glob
    i=0
    
    images = glob.glob("*.jpg")
    
    for i in images:
        print("start")
        img = cv2.imread(i, 1)  
        cv2.imwrite(i.split('.')[0]+'.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 1])
        print("end")