Skip to content

Instantly share code, notes, and snippets.

@qingswu
Last active October 26, 2022 14:05
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save qingswu/1a58c9d66dfc0a6aaac45528bbe01b82 to your computer and use it in GitHub Desktop.
Save qingswu/1a58c9d66dfc0a6aaac45528bbe01b82 to your computer and use it in GitHub Desktop.
Convert all png images in current folder to jpg using OpenCV cv2
#!/usr/bin/env python
from glob import glob
import cv2
pngs = glob('./*.png')
for j in pngs:
img = cv2.imread(j)
cv2.imwrite(j[:-3] + 'jpg', img)
@NoahDragon
Copy link

using glob('./**/*.png', recursive=True) for all sub folders.

Copy link

ghost commented Jun 27, 2020

Hi @qingswu, do you know what changes can be made in this algorithm if we want to save all the jpgs ina different folder? Could you let me know.

@atulyaatul1999
Copy link

@ghost you just have to give the folder name in cv.imwrite before the file name .
e.g. 'jpg_images/'+j[:-3] + 'jpg'

@ACMOIDRE
Copy link

I found same strange bug and using this script. I try to convert 4k image png(sizes almost 40mb) to jpg.
color image but Normal map image works fine.

@ACMOIDRE
Copy link

Capture
Capture2
Capture3

this are strange effect after running scripts

@ACMOIDRE
Copy link

ACMOIDRE commented Oct 14, 2020

I think first image conversion has error other are fine atleast to my eye vision.
if any one notices difference in below images too , plz kindly report by commenting.
thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment