title: how to convert a directory of image on png to jpg in python
body: <pre class="lang-py prettyprint-override"><code>from PIL import Image
  from os import listdir
  from os.path import splitext
  import cv2
  target_directory = r&quot;E:\pre\png&quot;
  target = '.jpg'
  jpg_folder_path = r&quot;E:\pre\jpeg&quot;
  for file in listdir(target_directory):
      filename, extension = splitext(file)
      try:
          if extension not in ['.py', target]:
              im = Image.open(filename + extension)
              #im.save((os.path.join(jpg_folder_path, im))filename + target)
              cv2.imwrite(os.path.join(jpg_folder_path , filename + target), im)
      except OSError:
          print('Cannot convert %s' % file)
  </code></pre>
  <p>OUTPUT</p>
  <pre><code>Cannot convert 000c1434d8d7.png
  Cannot convert 00a8624548a9.png
  ..
  </code></pre>