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 ?