0
**The error says**
  "cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file 'image-playground/.DS_Store'

The above error constantly shows up when i run this code, please help with the solution

**while running the code**
import sys
import os
from PIL import Image

image_folder = sys.argv[1]
output_folder = sys.argv[2]

if not os.path.exists(output_folder):
    os.makedirs(output_folder)

for filename in os.listdir(image_folder):
    img= Image.open(f'{image_folder}{filename}')
    img.save(f'{output_folder}{filename}', 'png')
    print ('all done!')
Rishabh Jain
  • 15
  • 1
  • 4
  • Please provide a simple, self-contained example. [This post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) may be helpful. – Limey Jun 01 '20 at 09:45

1 Answers1

0

The error is because you are trying to open a non-image file with Image.open().

If your goal is to move the file from one folder to another then I would suggest you use

os.rename(SourceFileName,TargetFileName)

If not, consider filtering your input file before calling Image.open to specific extensions that you would like to access, say like:-

ext=['jpg','png','gif']
for filename in os.listdir(image_folder):
    if filename[-3:] in ext:
        img= Image.open(f'{image_folder}{filename}')
        img.save(f'{output_folder}{filename}', 'png')
    print ('all done!')

Hope that helps

Sherin Jayanand
  • 204
  • 2
  • 9