Python Forum
Renaming explorer files in order?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Renaming explorer files in order?
#1
Hi everyone. I'm pretty new at Python, and am struggling with something. As you can see from the code attached (line 59 - 77 is what I'm refering to in this post), I am trying to re-name all the files in a particular folder to fit a pattern for a project I'm making. Before running the code, the images are all named "testimage(i), i being the number in the folder. The problem I have is that while this code does run, and does rename them the way I want, the order in which it is going through them is weird. It seems to do the first one called "testimage(1)", followed by "testimage(10)", through to 19, then it just starts getting like every tenth number first. I'm really confused why this is happening, but essentially I want it to just go through the images in the order they appear in the explorer so that it does "testimage(1), testimage(2)" etc all the way through. Any suggestions would be really great. Thanks!

from random import shuffle
import os
import glob
import sys
import cv2
import numpy as py
import matplotlib.pyplot as plt
#import skimage.io as io
import tensorflow as tf
import time

def _int64_feature(value): # takes value and wraps it a int64 list
	return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
	return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def load_image(addr):
	#read an image and resize to (244, 244)
 	#cv2 load images as BGR, convert it to RGB
 	img = cv2.imread(addr)
 	if img is None:
 		return None
 	img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_CUBIC)
 	img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 	return img

def createDataRecord(out_filename, addrs, labels):
	# open the TFRecords file
	writer = tf.python_io.TFRecordWriter(out_filename)
	for i in range(len(addrs)):
 		# print how many images are saved every 1000 images
 		if not i % 1000:
 			print('Train data: {}/{}'.format(i, len(addrs)))
 			sys.stdout.flush()
 		# Load the image
 		img = load_image(addrs[i])

 		label = labels[i]

 		if img is None:
 			continue


 		# Create a feature
 		feature = {
 			'image_raw': _bytes_feature(img.tostring()),
 			'label': _int64_feature(label)
 		}
 		# Create an example protocol buffer
 		example = tf.train.Example(features=tf.train.Features(feature=feature))

 		# Serialize to string and write on the file
 		writer.write(example.SerializeToString())
	writer.close()
	sys.stdout.flush()

train_path = 'E:/Documents/Year 3/Advanced Technology/Task3/MapScraper/MapsImageScraper/IMAGES_SORTED/*/*.png'

width = 38 	#dimensions of single split map area in tiles
height = 18

imgPath = 'E:/Documents/Year 3/Advanced Technology/Task3/MapScraper/MapsImageScraper/IMAGES_TEST_MAP/images/'
h = 1
w = 1

for filename in os.listdir(imgPath): 	#finds the files in the path

    dst = str(h)  + "_" + str(w) + ".png" 	#sets rename for map positions
    src = imgPath + filename
    dst = imgPath + dst 	#sets the output images to the same location

    os.rename(src, dst) 
    time.sleep(1)
    w += 1
    if w > width:
        w = 0
        h += 1

# read addresses and labels from the folders
addrs = glob.glob(train_path)
labels = [0 if 'BUILDING' in addr 	#numbers to represent each label type
 		 else 1 if 'FOREST' in addr
 		 else 2 if 'GRASS' in addr
 		 else 3 if 'HOUSE' in addr
 		 else 4 if 'RIVER' in addr
 		 else 5 if 'ROAD' in addr
 		 else 6 if 'SAND' in addr
 		 else 7 if 'WATER' in addr
 		 else 8 for addr in addrs]

# to shuffle data
c = list(zip(addrs, labels))
shuffle(c)
addrs, labels = zip(*c)

# Divide the data into 60% train, 20% validation, and 20% test
train_addrs = addrs[0:int(0.6*len(addrs))]
train_labels = labels[0:int(0.6*len(labels))]
val_addrs = addrs[int(0.6*len(addrs)):int(0.8*len(addrs))]
val_labels = labels[int(0.6*len(addrs)):int(0.8*len(addrs))]
test_addrs = addrs[int(0.8*len(addrs)):]
test_labels = labels[int(0.8*len(labels)):]

createDataRecord('train.tfrecords', train_addrs, train_labels)
createDataRecord('val.tfrecords', val_addrs, val_labels)
createDataRecord('test.tfrecords', test_addrs, test_labels)
Reply
#2
(Mar-02-2019, 09:05 PM)stroudie2 Wrote: Before running the code, the images are all named "testimage(i), i being the number in the folder. The problem I have is that while this code does run, and does rename them the way I want, the order in which it is going through them is weird. It seems to do the first one called "testimage(1)", followed by "testimage(10)", through to 19, then it just starts getting like every tenth number first. I'm really confused why this is happening
That is the normal alphabetical order. You need to make all number equal length by add 0s to the left, e.g.
001, 002...010, 011... and so on
That is assuming you have 999 images, if more make them 4 digit number etc. - you get the idea
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Mar-02-2019, 10:02 PM)buran Wrote:
(Mar-02-2019, 09:05 PM)stroudie2 Wrote: Before running the code, the images are all named "testimage(i), i being the number in the folder. The problem I have is that while this code does run, and does rename them the way I want, the order in which it is going through them is weird. It seems to do the first one called "testimage(1)", followed by "testimage(10)", through to 19, then it just starts getting like every tenth number first. I'm really confused why this is happening
That is the normal alphabetical order. You need to make all number equal length by add 0s to the left, e.g.
001, 002...010, 011... and so on
That is assuming you have 999 images, if more make them 4 digit number etc. - you get the idea

Thanks, that is one way. I think what I might try doing instead, since I know how many images there will always be (the width * height), I think I'm gonna create a filename string which gets each file and applies the new name to each one before carrying onto the next one, all in a count loop up to the max number of images. I think that's the best way to do it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Explorer in spyder driesdep 1 201 Apr-02-2024, 06:50 AM
Last Post: paul18fr
Information automatic document renaming lisa_d 2 338 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 735 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,512 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  File explorer DPaul 9 2,118 Aug-05-2022, 06:00 AM
Last Post: DPaul
  Functions to consider for file renaming and moving around directories cubangt 2 1,746 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,792 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Python with win32com and EXIF renaming files. Amrcodes 4 3,657 Apr-03-2021, 08:51 PM
Last Post: DeaD_EyE
  Flask files running order TomasAm 0 1,453 Nov-12-2020, 08:22 AM
Last Post: TomasAm
  Odd behavior when starting Explorer.exe malonn 2 2,058 Sep-14-2020, 07:45 PM
Last Post: malonn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020