Create script.py
Browse files
script.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
def get_image_data(folder_path):
|
| 6 |
+
image_data = []
|
| 7 |
+
|
| 8 |
+
# Iterate through all files in the folder
|
| 9 |
+
for filename in os.listdir(folder_path):
|
| 10 |
+
file_path = os.path.join(folder_path, filename)
|
| 11 |
+
|
| 12 |
+
# Check if the file is an image
|
| 13 |
+
if os.path.isfile(file_path) and filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
| 14 |
+
try:
|
| 15 |
+
with Image.open(file_path) as img:
|
| 16 |
+
width, _ = img.size
|
| 17 |
+
image_data.append({'file_name': filename, 'image_width': width})
|
| 18 |
+
except IOError:
|
| 19 |
+
print(f"Error opening {filename}. Skipping this file.")
|
| 20 |
+
|
| 21 |
+
return image_data
|
| 22 |
+
|
| 23 |
+
# Specify the folder path
|
| 24 |
+
folder_path = "/tmp/data/hugging_face_images"
|
| 25 |
+
|
| 26 |
+
# Get image data
|
| 27 |
+
image_data = get_image_data(folder_path)
|
| 28 |
+
|
| 29 |
+
# Create DataFrame
|
| 30 |
+
df = pd.DataFrame(image_data)
|
| 31 |
+
|
| 32 |
+
df.to_csv("solution.csv", index=False)
|