LeoZhangzaolin commited on
Commit
e012aec
1 Parent(s): 7f97f62

DataCleaning and ImageTransfering.py

Browse files
Files changed (1) hide show
  1. Project _1.py +136 -0
Project _1.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### delete unuseful columns
2
+ import csv
3
+
4
+ file_path = '/Users/leozhangzaolin/Downloads/Graptolite specimens.csv'
5
+
6
+ with open(file_path, newline='', encoding='utf-8') as file:
7
+ reader = csv.reader(file)
8
+ data = list(reader)
9
+
10
+ columns_to_delete = [
11
+ "species ID", "Phylum", "Class", "Order", "revised species name",
12
+ "total number of specimens", "specimens Serial No", "显微镜照片数量", "SLR photo No",
13
+ "相机照片数量", "跑数据照片总数", "备注", "age from", "age to", "collection No", "Microscrope photo No"
14
+ ]
15
+
16
+ header = data[0]
17
+ indices_to_delete = [header.index(column) for column in columns_to_delete if column in header]
18
+
19
+ indices_to_delete.sort(reverse=True)
20
+
21
+ for row in data:
22
+ for index in indices_to_delete:
23
+ del row[index]
24
+
25
+ new_file_path = '/Users/leozhangzaolin/desktop/New_GS.csv'
26
+
27
+ with open(new_file_path, mode='w', newline='', encoding='utf-8') as file:
28
+ writer = csv.writer(file)
29
+ writer.writerows(data)
30
+
31
+
32
+ #### merge columns
33
+
34
+ file_path1 = '/Users/leozhangzaolin/desktop/New_GS.csv'
35
+
36
+ with open(file_path1, newline='', encoding='utf-8') as file1:
37
+ reader1 = csv.reader(file1)
38
+ data1 = list(reader1)
39
+
40
+ header1 = data1[0]
41
+ family_index1 = header1.index('Family') if 'Family' in header1 else None
42
+ subfamily_index1 = header1.index('Subfamily') if 'Subfamily' in header1 else None
43
+ locality_index1 = header1.index('Locality') if 'Locality' in header1 else None
44
+ longitude_index1 = header1.index('Longitude') if 'Longitude' in header1 else None
45
+ latitude_index1 = header1.index('Latitude') if 'Latitude' in header1 else None
46
+ horizon_index1 = header1.index('Horizon') if 'Horizon' in header1 else None
47
+
48
+ for row1 in data1[1:]:
49
+ family1 = row1[family_index1] if family_index1 is not None else ''
50
+ subfamily1 = row1[subfamily_index1] if subfamily_index1 is not None else 'no subfamily'
51
+ row1[family_index1] = f"{family1} ({subfamily1})" if subfamily_index1 is not None else family1
52
+
53
+ locality1 = row1[locality_index1] if locality_index1 is not None else ''
54
+ longitude1 = row1[longitude_index1] if longitude_index1 is not None else ''
55
+ latitude1 = row1[latitude_index1] if latitude_index1 is not None else ''
56
+ horizon1 = row1[horizon_index1] if horizon_index1 is not None else ''
57
+ row1[locality_index1] = f"{locality1} ({longitude1}, {latitude1}, {horizon1})"
58
+
59
+ header1[family_index1] = 'Family (Subfamily)'
60
+ header1[locality_index1] = 'Locality (Longitude, Latitude, Horizon)'
61
+
62
+ indices_to_delete1 = sorted([subfamily_index1, longitude_index1, latitude_index1, horizon_index1], reverse=True)
63
+ for index1 in indices_to_delete1:
64
+ if index1 is not None:
65
+ for row1 in data1:
66
+ del row1[index1]
67
+
68
+ new_file_path1 = '/Users/leozhangzaolin/desktop/New_GS_1.csv'
69
+
70
+ with open(new_file_path1, mode='w', newline='', encoding='utf-8') as file1:
71
+ writer1 = csv.writer(file1)
72
+ writer1.writerows(data1)
73
+
74
+ #### read images
75
+ import os
76
+ import numpy as np
77
+ from PIL import Image
78
+ import pandas as pd
79
+
80
+ # Paths
81
+ csv_file_path = '/Users/leozhangzaolin/desktop/New_GS_1.csv'
82
+ image_dir_paths = ['/Users/leozhangzaolin/Desktop/project 1/graptolite specimens with scale 1',
83
+ '/Users/leozhangzaolin/Desktop/project 1/graptolite specimens with scale 2']
84
+ npy_save_dir = '/Users/leozhangzaolin/Desktop/arrays'
85
+
86
+ # Normalize file extensions in the image directories
87
+ def normalize_file_extensions(dir_path):
88
+ for filename in os.listdir(dir_path):
89
+ if filename.lower().endswith('.jpg') and not filename.endswith('.jpg'):
90
+ base, ext = os.path.splitext(filename)
91
+ new_filename = base + '.jpg'
92
+ os.rename(os.path.join(dir_path, filename),
93
+ os.path.join(dir_path, new_filename))
94
+
95
+ for path in image_dir_paths:
96
+ normalize_file_extensions(path)
97
+
98
+ # Load the CSV file
99
+ df = pd.read_csv(csv_file_path)
100
+
101
+ # Function to process and resize the image
102
+ def process_image(image_name, save_dir, max_size=(1024, 1024)):
103
+ image_base_name = os.path.splitext(image_name)[0]
104
+ image_paths = [os.path.join(dir_path, image_base_name + suffix)
105
+ for dir_path in image_dir_paths
106
+ for suffix in ['_S.jpg', '_S.JPG']]
107
+
108
+ image_path = next((path for path in image_paths if os.path.exists(path)), None)
109
+
110
+ if image_path is None:
111
+ return None
112
+
113
+ with Image.open(image_path) as img:
114
+ # Resize the image using LANCZOS filter (replacement for ANTIALIAS)
115
+ img.thumbnail(max_size, Image.Resampling.LANCZOS)
116
+ image_array = np.array(img)
117
+ npy_filename = image_base_name + '_S.npy'
118
+ npy_path = os.path.join(save_dir, npy_filename)
119
+ np.save(npy_path, image_array)
120
+ return npy_path
121
+
122
+ # Create a directory to save the numpy arrays
123
+ os.makedirs(npy_save_dir, exist_ok=True)
124
+
125
+ # Process each image and update the 'image file name' column
126
+ df['image file name'] = df['image file name'].apply(lambda x: process_image(x, npy_save_dir))
127
+
128
+ # Remove rows where the image was not found
129
+ df = df.dropna(subset=['image file name'])
130
+
131
+ # Rename the 'image file name' column to 'image'
132
+ df.rename(columns={'image file name': 'image'}, inplace=True)
133
+
134
+ # Save the updated DataFrame to a new CSV file
135
+ updated_csv_path = '/Users/leozhangzaolin/Desktop/New_GS_2_updated_images.csv'
136
+ df.to_csv(updated_csv_path, index=False)