-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_images_metadata.py
More file actions
72 lines (58 loc) · 1.8 KB
/
detect_images_metadata.py
File metadata and controls
72 lines (58 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'''
This script opens images in a folder and writes metadata in csv file.
Author: Maria Rosario SEBASTIAN
Date:20/06/2022
'''
from PIL import Image
import csv, glob
import pandas as pd
def get_metadata(fn):
'''Opens an image, retrieves the image data, and puts the data into a dictionary'''
i = Image.open(fn)
fname=i.filename.replace("\\","/")
ret = {
"Filename": fname.split("/")[-1],
"Size": i.size,
"Height": i.height,
"Width": i.width,
"Format": i.format,
"Mode": i.mode
}
return ret
def create_image_metadata(resultspath, csvpath):
'''
Writes metadata to csvpath on images from resultspath
'''
# Define path to images and grab all image filenames
images = glob.glob(resultspath + '/*')
#image data in each row
rows=[]
#header
fieldnames=''
for img in images:
info_dict = get_metadata(img)
fieldnames = info_dict.keys()
rows.append(info_dict)
with open(csvpath, 'w', encoding='UTF8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
def write_image_metadata(metadatapath, resultspath, csvpath):
'''
Reads image scraped metadata, transfers uploaded image metadata into different file.
'''
df = pd.read_csv(metadatapath)
print('****************',df)
#add header
new_meta= pd.DataFrame(list(df.columns.values)).transpose()
# Define path to images and grab all image filenames
images = glob.glob(resultspath + '/*')
#image data in each row
rows=[]
for img in images:
filename=img.replace("\\","/")
entry = df.loc[df['filename'] == filename.split("/")[-1]]
rows.append(entry)
new_meta = pd.concat(rows)
print(new_meta)
new_meta.to_csv(csvpath)