Now the dumping scripts only save the image once.
Browse files- scrape_gel.py +1 -7
- scrape_yan.py +1 -7
- utils/utils.py +21 -26
scrape_gel.py
CHANGED
@@ -5,7 +5,6 @@ import time
|
|
5 |
import json
|
6 |
import utils
|
7 |
import asyncio
|
8 |
-
import aiofiles
|
9 |
import argparse
|
10 |
import concurrent
|
11 |
from constants import *
|
@@ -107,12 +106,7 @@ async def process_link(scrape_args, scrape_state):
|
|
107 |
img_data = await img_response.read()
|
108 |
download_used_time = time.time() - download_start_time
|
109 |
|
110 |
-
|
111 |
-
await f.write(img_data)
|
112 |
-
async with aiofiles.open(metadata_path, "w", encoding="utf8") as f:
|
113 |
-
await f.write(metadata)
|
114 |
-
|
115 |
-
if not await utils.submit_validation(scrape_state.thread_pool, image_path, metadata_path, scrape_args.width, scrape_args.height, scrape_args.convert_to_avif):
|
116 |
return
|
117 |
scrape_state.scraped_image_count += 1
|
118 |
total_query_time = scrape_state.avg_query_time[0] * scrape_state.avg_query_time[1] + query_used_time
|
|
|
5 |
import json
|
6 |
import utils
|
7 |
import asyncio
|
|
|
8 |
import argparse
|
9 |
import concurrent
|
10 |
from constants import *
|
|
|
106 |
img_data = await img_response.read()
|
107 |
download_used_time = time.time() - download_start_time
|
108 |
|
109 |
+
if not await utils.submit_validation(scrape_state.thread_pool, img_data, metadata, image_path, metadata_path, scrape_args.width, scrape_args.height, scrape_args.convert_to_avif):
|
|
|
|
|
|
|
|
|
|
|
110 |
return
|
111 |
scrape_state.scraped_image_count += 1
|
112 |
total_query_time = scrape_state.avg_query_time[0] * scrape_state.avg_query_time[1] + query_used_time
|
scrape_yan.py
CHANGED
@@ -5,7 +5,6 @@ import json
|
|
5 |
import utils
|
6 |
import urllib
|
7 |
import asyncio
|
8 |
-
import aiofiles
|
9 |
import argparse
|
10 |
import concurrent
|
11 |
from constants import *
|
@@ -78,12 +77,7 @@ async def process_image_object(scrape_args, scrape_state):
|
|
78 |
img_data = await img_response.read()
|
79 |
download_used_time = time.time() - download_start_time
|
80 |
|
81 |
-
|
82 |
-
await f.write(img_data)
|
83 |
-
async with aiofiles.open(metadata_path, "w", encoding="utf8") as f:
|
84 |
-
await f.write(metadata)
|
85 |
-
|
86 |
-
if not await utils.submit_validation(scrape_state.thread_pool, image_path, metadata_path, scrape_args.width, scrape_args.height, scrape_args.convert_to_avif):
|
87 |
return
|
88 |
scrape_state.scraped_image_count += 1
|
89 |
total_download_time = scrape_state.avg_download_time[0] * scrape_state.avg_download_time[1] + download_used_time
|
|
|
5 |
import utils
|
6 |
import urllib
|
7 |
import asyncio
|
|
|
8 |
import argparse
|
9 |
import concurrent
|
10 |
from constants import *
|
|
|
77 |
img_data = await img_response.read()
|
78 |
download_used_time = time.time() - download_start_time
|
79 |
|
80 |
+
if not await utils.submit_validation(scrape_state.thread_pool, img_data, metadata, image_path, metadata_path, scrape_args.width, scrape_args.height, scrape_args.convert_to_avif):
|
|
|
|
|
|
|
|
|
|
|
81 |
return
|
82 |
scrape_state.scraped_image_count += 1
|
83 |
total_download_time = scrape_state.avg_download_time[0] * scrape_state.avg_download_time[1] + download_used_time
|
utils/utils.py
CHANGED
@@ -1,50 +1,45 @@
|
|
1 |
import os
|
|
|
2 |
import json
|
3 |
import asyncio
|
4 |
import aiohttp
|
5 |
from PIL import Image
|
6 |
|
7 |
-
def validate_image(image_path, metadata_path, width=None, height=None, convert_to_avif=False):
|
8 |
-
new_path = None
|
9 |
try:
|
10 |
-
with
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
if new_path is not None:
|
20 |
img.load()
|
21 |
-
img.save(
|
22 |
-
|
23 |
-
|
24 |
-
if new_path is not None and os.path.isfile(new_path) and os.path.realpath(new_path) != os.path.realpath(image_path):
|
25 |
-
os.remove(image_path)
|
26 |
return True
|
27 |
except Exception as e:
|
28 |
print(f"Error validating image {image_path}: {e}")
|
29 |
try:
|
30 |
os.remove(image_path)
|
|
|
|
|
31 |
except Exception as e:
|
32 |
print("Error deleting image file:", e)
|
33 |
try:
|
34 |
os.remove(metadata_path)
|
35 |
-
print(f"Deleted invalid image and metadata files: {image_path}, {metadata_path}")
|
|
|
|
|
36 |
except Exception as e:
|
37 |
print("Error deleting metadata file:", e)
|
38 |
-
try:
|
39 |
-
if new_path is not None and os.path.isfile(new_path):
|
40 |
-
os.remove(new_path)
|
41 |
-
print("Deleted invalid new image file:", new_path)
|
42 |
-
except Exception as e:
|
43 |
-
print("Error deleting new image file:", e)
|
44 |
return False
|
45 |
|
46 |
-
async def submit_validation(thread_pool, image_path, metadata_path, width=None, height=None, convert_to_avif=False):
|
47 |
-
return await asyncio.wrap_future(thread_pool.submit(validate_image, image_path, metadata_path, width, height, convert_to_avif))
|
48 |
|
49 |
def get_image_id_image_metadata_path_tuple_dict(image_dir):
|
50 |
if not os.path.isdir(image_dir):
|
|
|
1 |
import os
|
2 |
+
import io
|
3 |
import json
|
4 |
import asyncio
|
5 |
import aiohttp
|
6 |
from PIL import Image
|
7 |
|
8 |
+
def validate_image(image_data, metadata, image_path, metadata_path, width=None, height=None, convert_to_avif=False):
|
|
|
9 |
try:
|
10 |
+
with io.BytesIO(image_data) as image_filelike:
|
11 |
+
with Image.open(image_filelike) as img:
|
12 |
+
save_kwargs = {}
|
13 |
+
if isinstance(width, int) and width > 0 and isinstance(height, int) and height > 0:
|
14 |
+
img = img.resize((width, height))
|
15 |
+
if convert_to_avif:
|
16 |
+
import pillow_avif
|
17 |
+
save_kwargs["quality"] = 50
|
18 |
+
image_path = os.path.splitext(image_path)[0] + ".avif"
|
|
|
19 |
img.load()
|
20 |
+
img.save(image_path, **save_kwargs)
|
21 |
+
with open(metadata_path, "w", encoding="utf8") as metadata_file:
|
22 |
+
metadata_file.write(metadata)
|
|
|
|
|
23 |
return True
|
24 |
except Exception as e:
|
25 |
print(f"Error validating image {image_path}: {e}")
|
26 |
try:
|
27 |
os.remove(image_path)
|
28 |
+
except FileNotFoundError:
|
29 |
+
pass
|
30 |
except Exception as e:
|
31 |
print("Error deleting image file:", e)
|
32 |
try:
|
33 |
os.remove(metadata_path)
|
34 |
+
print(f"Deleted invalid image and metadata files: \"{image_path}\", \"{metadata_path}\"")
|
35 |
+
except FileNotFoundError:
|
36 |
+
pass
|
37 |
except Exception as e:
|
38 |
print("Error deleting metadata file:", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
return False
|
40 |
|
41 |
+
async def submit_validation(thread_pool, image_data, metadata, image_path, metadata_path, width=None, height=None, convert_to_avif=False):
|
42 |
+
return await asyncio.wrap_future(thread_pool.submit(validate_image, image_data, metadata, image_path, metadata_path, width, height, convert_to_avif))
|
43 |
|
44 |
def get_image_id_image_metadata_path_tuple_dict(image_dir):
|
45 |
if not os.path.isdir(image_dir):
|