-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.py
32 lines (22 loc) · 871 Bytes
/
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
from pexels_api import API
import requests
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Access the secret key from the environment variable
pixel_api=os.getenv("PIXELS_API")
pexels_api = API(pixel_api)
def get_image(topic):
if not os.path.exists('images'):
os.makedirs('images')
photos = pexels_api.search(topic)
for index, photo in enumerate(photos['photos'], start=1):
image_url = photo['src']['large']
image_id = photo['id']
image_filename = f'images/{index}.jpg' # Save the images in the "images" directory
response = requests.get(image_url)
response.raise_for_status()
with open(image_filename, 'wb') as file:
file.write(response.content)
print(f"Image saved: {image_filename}")