1

I have a folder that contains 2000 TIF images and I want to convert them to jpg images. I wrote two codes and both work well until they convert 370 images and then they raise an error

Here is my first code :

DestPath='/media/jack/Elements/ToJPG95/'
from PIL import Image
import os
def change(path, row):
  filename1=path+row
  filename=row.split('.')[0] + '.jpg'
  im = Image.open(filename1)
  img= im.convert('RGB')
  Dest=os.path.join(DestPath,filename)
  img.save(Dest, format='JPEG',quality=95)


import csv
sourcePath='/media/jack/Elements/TifImages/'
with open("TIFFnames.csv") as f:
    filtered = (line.replace('\n', '') for line in f)
    reader = csv.reader(filtered)
    for row in filtered:
       change(sourcePath , row)

and here is my second code which I ran in inside the folder that has the images :

from PIL import Image  # Python Image Library - Image Processing
import glob
DestPath='/media/jack/Elements/ToJPG95/'
print(glob.glob("*.TIF"))
for file in glob.glob("*.TIF"):
    im = Image.open(file)
    rgb_im = im.convert('RGB')
    rgb_im.save(DestPath+file.replace("TIF", "jpg"), quality=95)
# based on SO Answer: https://stackoverflow.com/a/43258974/5086335

they convert up to 370 images and then give an error Here is the error I am getting :

Traceback (most recent call last):
  File "conmg.py", line 7, in <module>
    rgb_im = im.convert('RGB')
  File "/home/jack/.local/lib/python3.6/site-packages/PIL/Image.py", line 873, in convert
    self.load()
  File "/home/jack/.local/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1070, in load
    return self._load_libtiff()
  File "/home/jack/.local/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1182, in _load_libtiff
    raise OSError(err)
OSError: -2

I have tried imagemagick mentioned in the solution Here

but this is what I am getting when I click enter to run the command:

jack@jack-dell:/media/jack/Elements/TifImages$ for f in *.tif; do  echo "Converting $f"; convert "$f"  "$(basename "$f" .tif).jpg"
> 
> 
> 
> 

As you can see, it does nothing I think the codes work well but for some reason they fail after converting 370 images I am running on a 6 TB external hard drive. Can any one tell me please whats wrong ?

Sea
  • 25
  • 5

1 Answers1

2

As @fmw42 says, you likely have a problem with the 370th file (corrupt, or some ill-supported TIFF variant). You bash code will convert all the files that can be read, it doesn't work because you are missing a closing done:

 for f in *.tif; do  echo "Converting $f"; convert "$f"  "$(basename "$f" .tif).jpg" ; done

Your Python would also convert all the readable files if you use try/except to catch errors and continue with the next file:

for file in glob.glob("*.TIF"):
    try: 
        im = Image.open(file)
        rgb_im = im.convert('RGB')
        rgb_im.save(DestPath+file.replace("TIF", "jpg"), quality=95)
    except:
        print('File not converted:',file)
xenoid
  • 8,396
  • 3
  • 23
  • 49
  • The Python code worked well Thanks a lot. The bash script did not work Here is the error : convert-im6.q16: no images defined `*.jpg' @ error/convert.c/ConvertImageCommand/3258. – Sea Apr 07 '20 at 09:03
  • But the Python works. it printed the file that caused the problem and continued working. you and @fmw42 saved my life. Thank you both – Sea Apr 07 '20 at 09:06
  • Upvote is fine, but please consider accepting the answer (checkmark below the votes) so that it shows as having a satisfactory answer. – xenoid Apr 07 '20 at 09:15