본문 바로가기
Python

[python] download images from pexels

by 센세이 2023. 5. 16.
728x90
반응형

https://www.pexels.com/

 

After logging in on the pexels site and requesting image & video api, you can get a key. 

I will proceed with the assumption that there is an api key.

 

pip install pexels-api-py

First, install pip install pexels-api-py.

import requests
import os
from pexelsapi.pexels import Pexels
 
pexel = Pexels('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') #api_key
search_photos = pexel.search_photos(query=f'{keyword}', orientation='', size='', color='', locale='', page=1, per_page=2)
print(search_photos.get('photos')[0].get('src'))
res = requests.get(search_photos.get('photos')[0].get('src').get('original'))
print(res.content)
folder_path = os.path.join(os.path.expanduser("~"), "Desktop", "pexelFolder")
if res.ok:
    file_name = 'test_download.jpeg'
    file_path = os.path.join(folder_path, file_name)
    with open(file_path, "wb") as f:
        f.write(res.content)
        print(f"{file_name} file download success.")

That's it. It's that simple.

 

pexel-api-key-image-download
pexel-api-key-image-download

 

After executing the above code, you can see that the image is saved in the location you specified.

Then see you next time~

 

728x90
반응형

댓글