-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
51 lines (44 loc) · 1.69 KB
/
test.py
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
from PIL import Image
from PIL.ExifTags import TAGS
import os, sys
import datetime as dt
import json
import pandas as pd
for dirname, subdirs, images in os.walk("downloads\hashtag\instagood"):
metadata = []
for inImg in images:
## convert image types
f, e = os.path.splitext(inImg)
outImg = f + ".jpg"
if inImg != outImg:
try:
os.rename(os.path.join(dirname,inImg),os.path.join(dirname,outImg))
except OSError:
print("cannot convert", inImg)
filemeta = {}
node = {}
# img = Image.open(os.path.join(dirname,outfile))
filepath = os.path.join(dirname,outImg)
node['created_at_timestamp'] = os.path.getctime(filepath)
node['modified_at_timestamp'] = os.path.getmtime(filepath)
# node['byte_size'] = os.path.getsize(filepath)
filemeta['node'] = node
filemeta['_mediaPath'] = os.path.join(dirname, os.path.basename(inImg))
metadata.append(filemeta)
data = json.dumps(metadata)
f = open(r'output.json','w')
f.write(data)
# read from json file and add new info
with open(os.path.join(os.getcwd(), "output.json"), 'r', encoding='utf8') as read_file:
data = json.load(read_file)
for item in data:
path = os.path.join(os.getcwd(),item['_mediaPath'])
img = Image.open(path)
width = img.size[0]
height = img.size[1]
item['node']['dimensions'] = {'width': width, 'height': height}
data = json.dumps(data)
f = open(r'output.json','w')
f.write(data)
# timestamp = data[0]['shortcode_media']['taken_at_timestamp']
# print(dt.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))