|
|
|
|
|
|
|
|
|
|
|
import csv |
|
import os |
|
import cv2 |
|
import pandas as pd |
|
|
|
|
|
initial_csv_file_path = '/Users/leozhangzaolin/Desktop/Graptolite specimens.csv' |
|
image_dir_paths = ['/Users/leozhangzaolin/Desktop/project 1/graptolite specimens with scale 1', |
|
'/Users/leozhangzaolin/Desktop/project 1/graptolite specimens with scale 2'] |
|
output_image_dir = '/Users/leozhangzaolin/Desktop/project 1/output_images' |
|
target_size = (256, 256) |
|
|
|
|
|
os.makedirs(output_image_dir, exist_ok=True) |
|
|
|
|
|
with open(initial_csv_file_path, newline='', encoding='utf-8') as file: |
|
reader = csv.reader(file) |
|
data = list(reader) |
|
|
|
header = data[0] |
|
|
|
|
|
family_index = header.index('Family') if 'Family' in header else None |
|
subfamily_index = header.index('Subfamily') if 'Subfamily' in header else None |
|
locality_index = header.index('Locality') if 'Locality' in header else None |
|
longitude_index = header.index('Longitude') if 'Longitude' in header else None |
|
latitude_index = header.index('Latitude') if 'Latitude' in header else None |
|
horizon_index = header.index('Horizon') if 'Horizon' in header else None |
|
|
|
|
|
for row in data[1:]: |
|
|
|
if family_index is not None and subfamily_index is not None: |
|
family = row[family_index] |
|
subfamily = row[subfamily_index] if row[subfamily_index] else 'no subfamily' |
|
row[family_index] = f"{family} ({subfamily})" |
|
|
|
if locality_index is not None and all([longitude_index, latitude_index, horizon_index]): |
|
locality = row[locality_index] |
|
longitude = row[longitude_index] |
|
latitude = row[latitude_index] |
|
horizon = row[horizon_index] |
|
row[locality_index] = f"{locality} ({longitude}, {latitude}, {horizon})" |
|
|
|
|
|
header[family_index] = 'Family (Subfamily)' |
|
header[locality_index] = 'Locality (Longitude, Latitude, Horizon)' |
|
indices_to_delete = [header.index(column) for column in columns_to_delete if column in header] |
|
merged_indices = [subfamily_index, longitude_index, latitude_index, horizon_index] |
|
indices_to_delete.extend(merged_indices) |
|
indices_to_delete = list(set(indices_to_delete)) |
|
indices_to_delete.sort(reverse=True) |
|
header = [col for i, col in enumerate(header) if i not in indices_to_delete] |
|
|
|
for row in data[1:]: |
|
for index in indices_to_delete: |
|
del row[index] |
|
|
|
|
|
df = pd.DataFrame(data[1:], columns=header) |
|
|
|
|
|
def process_and_save_image(image_name, max_size=target_size): |
|
image_base_name = os.path.splitext(image_name)[0] |
|
image_paths = [os.path.join(dir_path, image_base_name + suffix) |
|
for dir_path in image_dir_paths |
|
for suffix in ['_S.jpg', '_S.JPG']] |
|
|
|
image_path = next((path for path in image_paths if os.path.exists(path)), None) |
|
|
|
if image_path is None: |
|
return None |
|
|
|
|
|
img = cv2.imread(image_path, cv2.IMREAD_COLOR) |
|
img = cv2.resize(img, max_size, interpolation=cv2.INTER_AREA) |
|
|
|
|
|
output_path = os.path.join(output_image_dir, image_base_name + '.jpg') |
|
cv2.imwrite(output_path, img) |
|
|
|
return output_path |
|
|
|
|
|
df['image file name'] = df['image file name'].apply(process_and_save_image) |
|
df = df.dropna(subset=['image file name']) |
|
|
|
|
|
df.rename(columns={'image file name': 'image'}, inplace=True) |
|
|
|
|
|
final_csv_path = '/Users/leozhangzaolin/Desktop/Final_GS_with_Images5.csv' |
|
df.to_csv(final_csv_path, index=False) |
|
|
|
|
|
def update_csv_with_github_links(csv_file_path, github_repo_url, branch_name): |
|
updated_rows = [] |
|
|
|
with open(csv_file_path, mode='r') as file: |
|
reader = csv.DictReader(file) |
|
for row in reader: |
|
image_name = row['image'].split('/')[-1] |
|
row['image'] = f"{github_repo_url}/{branch_name}/{image_name}" |
|
updated_rows.append(row) |
|
|
|
|
|
with open(csv_file_path, mode='w', newline='') as file: |
|
writer = csv.DictWriter(file, fieldnames=reader.fieldnames) |
|
writer.writeheader() |
|
writer.writerows(updated_rows) |
|
|
|
csv_file = '/Users/leozhangzaolin/Desktop/Final_GS_with_Images5.csv' |
|
github_repo_url = 'https://raw.githubusercontent.com/LeoZhangzaolin/photos' |
|
branch_name = 'main' |
|
update_csv_with_github_links(csv_file, github_repo_url, branch_name) |
|
|
|
|