python bulk modification of image suffixes of png to jpg

  • 2021-01-03 20:56:38
  • OfStack

Recently, I have been using faster_rcnn to train kitti data set, in which kitti data set needs to be converted into voc data set. However, I found that:

kitti images are in png format

voc2007 is in jpg format

Among them, there are more than 7000 pictures that need to be converted in batches. I found 1 code on the Internet, but there was a mistake in running, so I made some modifications:


import os
import string
dirName = "D:your path\\"   # Put a double slash at the end, or you'll get an error 
li=os.listdir(dirName)
for filename in li:
 newname = filename
 newname = newname.split(".")
 if newname[-1]=="png":
  newname[-1]="jpg"
  newname = str.join(".",newname) # Here want to use str.join
  filename = dirName+filename
  newname = dirName+newname
  os.rename(filename,newname)
  print(newname,"updated successfully")

This method can be used to convert not only image suffixes, but also other file suffixes.


Related articles: