| import pyarrow.parquet as pq | |
| import pyarrow as pa | |
| from PIL import Image | |
| import io | |
| import pandas as pd | |
| def is_image_corrupt(image_dict): | |
| try: | |
| image_bytes = image_dict['bytes'] | |
| image = Image.open(io.BytesIO(image_bytes)) | |
| image.verify() | |
| return False | |
| except (IOError, SyntaxError, KeyError) as e: | |
| return True | |
| def process_parquet_file(file_path, output_file_path): | |
| table = pq.read_table(file_path) | |
| df = table.to_pandas() | |
| image_column = 'image' | |
| clean_indices = [] | |
| corrupt_indices = [] | |
| for i, image_dict in enumerate(df[image_column]): | |
| if is_image_corrupt(image_dict): | |
| corrupt_indices.append(i) | |
| else: | |
| clean_indices.append(i) | |
| clean_df = df.iloc[clean_indices] | |
| clean_table = pa.Table.from_pandas(clean_df) | |
| pq.write_table(clean_table, output_file_path) | |
| print(f"File {file_path}: {len(corrupt_indices)} corrupt images were removed.") | |
| for i in range(5): | |
| input_file_path = f'data/train-0000{i}-of-00005.parquet' | |
| output_file_path = f'data/train-0000{i}-of-00005.parquet' | |
| process_parquet_file(input_file_path, output_file_path) | |