forked from hassan-sd/civitai-image-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civitai-image.py
50 lines (39 loc) · 1.48 KB
/
civitai-image.py
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
45
46
47
48
49
50
import requests
from PIL import Image
from io import BytesIO
import json
import os
from tqdm import tqdm
# Credit to Hassan-sd
# Replace with your API key
api_key = "xxxxxxxxx"
# API endpoint
url = "https://civitai.com/api/v1/images"
headers = {"Authorization": f"Bearer {api_key}"}
# Make API request
response = requests.get(url, headers=headers)
response_data = response.json()
# Filter images with stats.heartCount greater than 10 and a non-empty meta.prompt
filtered_images = [image for image in response_data['items'] if image['stats']['heartCount'] > 10 and image['meta'] is not None and 'prompt' in image['meta'] and image['meta']['prompt']]
# Download and save filtered images and metadata
total_saved = 0
for image in tqdm(filtered_images, desc="Saving images and metadata", unit="image"):
image_id = image['id']
image_url = image['url']
image_meta = image['meta']
# Download image
image_response = requests.get(image_url)
img = Image.open(BytesIO(image_response.content))
# Convert image to RGB if necessary
if img.mode == 'RGBA':
img = img.convert('RGB')
# Save image
img_filename = f"{image_id}.jpg"
img.save(img_filename)
# Save meta.prompt as a text file
meta_prompt = image_meta['prompt']
meta_filename = f"{image_id}.txt"
with open(meta_filename, "w") as meta_file:
meta_file.write(meta_prompt)
total_saved += 1
print(f"Downloaded and saved {total_saved}/{len(filtered_images)} images and metadata files.")