File size: 1,167 Bytes
0f9b30b |
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 |
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)
|