Skip to content

Instantly share code, notes, and snippets.

@rishimukherjee
Created July 28, 2012 09:11
Show Gist options
  • Save rishimukherjee/3192587 to your computer and use it in GitHub Desktop.
Save rishimukherjee/3192587 to your computer and use it in GitHub Desktop.
ImageSave
path = '/home/rishi/Documents/'
fname = 'MyImg'
#fname = ''
if path:
import glob, re, os, tempfile
if fname:
if os.path.exists(path):
if path[-1]!='/': path+='/'
gpath = path + fname + "*.png"
current_images = glob.glob(gpath)
nums = [0]
for img in current_images:
i = os.path.splitext(img)[0].split('/')[-1][-4:]
try:
num = re.findall('[0-9]+$', i)[0]
nums.append(int(num))
except IndexError:
pass
nums.sort()
new_num = nums[-1]+1
file_suffix = '%04d.png'%(new_num)
filename = tempfile.NamedTemporaryFile(prefix=fname, suffix=file_suffix, dir=path)
else:
print "wrong path"
else:
if os.path.exists(path):
import glob, re, os, tempfile
if path[-1]!='/': path+='/'
gpath = path + "Image*.png"
current_images = glob.glob(gpath)
nums = [0]
for img in current_images:
i = os.path.splitext(img)[0].split('/')[-1][-4:]
try:
num = re.findall('[0-9]+$', i)[0]
nums.append(int(num))
except IndexError:
pass
nums.sort()
new_num = nums[-1]+1
file_suffix = '%04d.png'%(new_num)
filename = tempfile.NamedTemporaryFile(prefix='Image', suffix=file_suffix, dir=path)
else:
print "wrong path"
else:
print "Path is not optional"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment