mesh-survey-paper/anonymize_images.py
2026-02-05 17:58:33 +01:00

99 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""
Script to anonymize survey images by overlaying a black bar at the bottom.
The black bar is 50% of image width and 25% of image height, horizontally centered.
"""
from PIL import Image, ImageDraw
import os
import sys
def anonymize_image(input_path, output_path, img_id, internal=False):
"""
Add a black bar to the bottom of an image.
Bar dimensions: 50% width, 25% height, centered horizontally at bottom.
"""
# Open the image
img = Image.open(input_path)
width, height = img.size
# Calculate bar dimensions
bar_width = int(width * 0.8)
if internal:
bar_height = height * (0.45 if img_id in ['01', '02', '15', '24', '27'] else 0.35)
else:
bar_height = height * (0.4 if img_id in ['31', '32'] else 0.28)
bar_height = int(bar_height)
# Calculate bar position (centered horizontally, at bottom)
bar_x = (width - bar_width) // 2
bar_y = height - bar_height
# Create a drawing context
draw = ImageDraw.Draw(img)
# Draw black rectangle
draw.rectangle(
[(bar_x, bar_y), (bar_x + bar_width, height)],
fill='black'
)
# Save the anonymized image
img.save(output_path, quality=95)
print(f"Processed: {input_path} -> {output_path}")
def main():
# List of image IDs to process for external survey images
image_ids = [
'02', '03', '04', '05', '06',
'08', '09', '10', '11', '12',
'13', '14', '15', '16', '17',
'18', '19', '20', '21', '22',
'23', '24', '25', '27', '28',
'29', '30', '31', '32'
]
# Internal survey images to process (with their number prefixes)
# Excluding H30, H31, H32 as requested
internal_images = [
('01', '09'), ('02', '20'), ('03', '11'), ('04', '03'), ('05', '10'),
('06', '08'), ('08', '24'), ('09', '13'), ('10', '23'), ('11', '17'),
('12', '19'), ('13', '02'), ('14', '00'), ('14', '01'), ('15', '04'),
('16', '05'), ('17', '22'), ('18', '21'), ('19', '26'), ('20', '12'),
('21', '15'), ('22', '16'), ('23', '07'), ('24', '06'), ('25', '25'),
('27', '18'), ('28', '14')
]
# Directory containing images
figures_dir = 'figures'
# Process external survey images
print("Processing external survey images...")
for img_id in image_ids:
input_filename = os.path.join(figures_dir, f"survey_diag_S{img_id}.jpg")
output_filename = os.path.join(figures_dir, f"survey_diag_S{img_id}_anon.jpg")
if os.path.exists(input_filename):
try:
anonymize_image(input_filename, output_filename, img_id, internal=False)
except Exception as e:
print(f"Error processing {input_filename}: {e}", file=sys.stderr)
else:
print(f"Warning: {input_filename} not found", file=sys.stderr)
# Process internal survey images
print("\nProcessing internal survey images...")
for specimen_id, number in internal_images:
input_filename = os.path.join(figures_dir, f"survey_internal_{number}_S{specimen_id}.jpg")
output_filename = os.path.join(figures_dir, f"survey_internal_{number}_S{specimen_id}_anon.jpg")
if os.path.exists(input_filename):
try:
anonymize_image(input_filename, output_filename, specimen_id, internal=True)
except Exception as e:
print(f"Error processing {input_filename}: {e}", file=sys.stderr)
else:
print(f"Warning: {input_filename} not found", file=sys.stderr)
if __name__ == "__main__":
main()