cdw1p

download-image-by-link.py

Jul 13th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import urllib.request
  2. import cv2
  3. import numpy as np
  4. import os
  5.  
  6. def store_raw_images():
  7.     neg_images_link = '//image-net.org/api/text/imagenet.synset.geturls?wnid=n00523513'  
  8.     neg_image_urls = urllib.request.urlopen(neg_images_link).read().decode()
  9.     pic_num = 1
  10.    
  11.     if not os.path.exists('neg'):
  12.         os.makedirs('neg')
  13.        
  14.     for i in neg_image_urls.split('\n'):
  15.         try:
  16.             print(i)
  17.             urllib.request.urlretrieve(i, "neg/"+str(pic_num)+".jpg")
  18.             img = cv2.imread("neg/"+str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
  19.             # should be larger than samples / pos pic (so we can place our image on it)
  20.             resized_image = cv2.resize(img, (100, 100))
  21.             cv2.imwrite("neg/"+str(pic_num)+".jpg",resized_image)
  22.             pic_num += 1
  23.            
  24.         except Exception as e:
  25.             print(str(e))  
  26.  
  27. store_raw_images()
Add Comment
Please, Sign In to add comment