Search code examples
pythontensorflowimage-processingcomputer-visionpng

regarding transforming PNG into JPG


I have a set of PNG files which are of shape (64,64,4), and want to feed it to a tensorflow model which was designed for JPG files, which has shape (64,64,3). I plan to transform PNG into JPG, is this a good approach, what's the best way to do it?


Solution

  • One way transforming it would be using opencv, not sure about the side-effects, you might get a little amount of color shifting. This will take all the png's in the current folder and save them as jpgs

    from glob import glob                                                           
    import cv2 
    pngs = glob('./*.png')
    
    for png in pngs:
        img = cv2.imread(png)
        cv2.imwrite(png[:-3] + 'jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])