I am using Windows 10 and Python 2.7.12, I have observed that when my folder starts with a letter f such as "folder1", "fig", "fish" it gives me an error:
[Errno 22] invalid mode ('wb') or filename:
When I rename my folder to any word starting with any letter that is not f, then it succeeds. Any reason why this happens? This is the code that I was running.
import os
import os.path
#From PIL
import Image
path2 = 'C:\Users\f'
def tiff_to_jpeg(path):
for root, dirs, files in os.walk(path):
for f in files:
a = f[:-4].replace(" ","")
b = a.replace("(","")
c = b.replace(")","")
c = c[2:]
c = '01' + c
print c
if os.path.splitext(os.path.join(root,f))[1].lower() == ".tif":
# If a jpeg is already present. Don't do anything.
if os.path.isfile(os.path.splitext(os.path.join(root, f))[0] + ".jpg"):
print "A jpeg file already exists for %s" %f
# If a jpeg is *NOT* present, create one from the tiff.
else:
outfile = os.path.join(path2,c) + ".jpg"
#outfile = os.path.splitext(os.path.join(root,f))[0] + ".jpg"
try:
im = Image.open(os.path.join(root,f))
print "Generating jpeg for %s" %f
im.thumbnail(im.size)
im.save(outfile, "JPEG", quality=100)
except Exception, e:
print e
path = 'C:\Users\f'
tiff_to_jpeg(path)