Search code examples
pandasdataframerenameglob

Rename images according to their labels and id in dataframe


I have a csv file as follow :

id                                       ocr    raw_value   manual_raw_value
00219b14-37d1-42b2-95e8-65fe2a94b7a5    ABBYY   6,35        6,35
402048fd-868d-446a-8468-07a57f5386bf    ABBYY   11,68        11,68
33405269-c273-4c13-83d4-a973c42b72eb    ABBYY   VOTRE        VOTRE
9cf97fc4-d79b-4125-85d2-7dabc056caa3    ABBYY   questions.  questions.
6f63010b-2ccc-4e7a-bfe6-aab1dfc65ea3    ABBYY   nb              nb
a76d4f54-5036-4212-ab5a-921724c05910    ABBYY   tes             les
4f6c38e9-6500-4172-a472-ba8532db05d2    ABBYY   à                à

the first column is the id of the images. the images are stored in a directory called images as follow :

00219b14-37d1-42b2-95e8-65fe2a94b7a5.png
402048fd-868d-446a-8468-07a57f5386bf.png
33405269-c273-4c13-83d4-a973c42b72eb.png
9cf97fc4-d79b-4125-85d2-7dabc056caa3.png
6f63010b-2ccc-4e7a-bfe6-aab1dfc65ea3.png
a76d4f54-5036-4212-ab5a-921724c05910.png
4f6c38e9-6500-4172-a472-ba8532db05d2.png

l would like to rename these images with their manual_raw_values which means for instance

00219b14-37d1-42b2-95e8-65fe2a94b7a5.png

becomes

6,35.png

to do so what l've done until now is the following :

df = pd.read_csv('/home/images/words.csv',sep=',')
df = df.astype(str)
path='/home/images/'
images_name = glob.glob("*.png")
set_img = set([x.rsplit('.', 1)[0] for x in images_name])
for img in set_img:
    label=df.loc[df.id==img, 'manual_raw_value'].item()
    # what to do next to rename the images with respect to their label and id

Solution

  • os.rename(img+'.png',label+'.png')