-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_imgs.py
46 lines (40 loc) · 1.38 KB
/
get_imgs.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import argparse
import urllib.request
import praw
from PIL import Image
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="get_imgs",
description="Downloads images from a subreddit",
)
parser.add_argument(
"-s", "--subreddit", type=str, help="subreddit to download from", required=True
)
parser.add_argument("-p", "--posts", type=int, default=100)
args = parser.parse_args()
reddit = praw.Reddit("bot1", user_agent="bot1 user agent")
subreddit = reddit.subreddit(args.subreddit)
# create directory
dir = f"{args.subreddit}_imgs"
try:
os.mkdir(dir)
except OSError as error:
print(error)
for submission in subreddit.top(limit=args.posts):
if submission.url.endswith(".jpg"):
# jpg == jpeg
# download image
image_name = f"{dir}/{submission.id}.jpeg"
urllib.request.urlretrieve(submission.url, image_name)
print(f"downloaded: {image_name}")
# resize image
image = Image.open(image_name)
# need to resize for best results in model training
image.resize((512, 512)).save(
f"{image_name.removesuffix('.jpeg')}_resized.jpeg"
)
os.remove(image_name)
print(f"resized: {image_name}")