Skip to content

Instantly share code, notes, and snippets.

@xeptore
Created February 25, 2019 07:52
Show Gist options
  • Save xeptore/74952a6684388865536c168920a1b3ae to your computer and use it in GitHub Desktop.
Save xeptore/74952a6684388865536c168920a1b3ae to your computer and use it in GitHub Desktop.
fake random image downloader using requests and Faker modules
from faker import Faker
import requests
import os
fake = Faker()
text = fake.text(1000)
image_urls = [
"".join((
"https://via.placeholder.com/250x200.jpg/",
fake.safe_hex_color()[1:],
"/",
fake.safe_hex_color()[1:],
"?text=",
text[i * 10: (i + 1) * 10]
))
for i in range(20)
]
dir_path = os.path.dirname(os.path.realpath(__file__))
dest_dir = os.path.join(dir_path, "public", "imagessss")
os.mkdir(dest_dir)
for i, url in enumerate(image_urls):
r = requests.get(url)
file_name = "{0:02d}".format(i + 1)
path = os.path.join(dest_dir, "{file_name}.{ext}".format(file_name=file_name, ext="jpg"))
with open(path, "wb") as f:
f.write(r.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment