10 examples of 'download image from url python' in Python

Every line of 'download image from url python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
80def downloadImage(url):
81 if not url:
82 return None
83
84 try:
85 img, _ = urllib.request.urlretrieve(url)
86 except Exception:
87 return None
88 ext = imghdr.what(img)
89 res = img + "." + ext
90 os.rename(img, res)
91
92 # Images smaller than 4 KB have a problem, and Twitter will complain
93 if os.path.getsize(res) < 4096:
94 os.remove(res)
95 return None
96
97 return res
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

71def download_image(url):
72 name = random.randrange(1,100000)
73 fullname = '/tmp/' + str(name) + ".png"
74 urllib.request.urlretrieve(url, fullname)
75 return fullname
19def download_image(url, savepath):
20 response = requests.get(url, headers=headers, stream=True)
21 with open(savepath, 'wb') as out_file:
22 shutil.copyfileobj(response.raw, out_file)
89def download_image(image_url, path='./'):
90 if not image_url:
91 return None
92 image_path = create_file_name(path)
93 urllib.request.urlretrieve(image_url, image_path)
94 # did download succeed?
95 if os.path.isfile(image_path):
96 return image_path
97 else:
98 return None
184def download_image(self, img_url):
185 """ Downloads a single image.
186
187 Downloads img_url using self.page_url as base.
188 Also, raises the appropriate exception if required.
189 """
190 img_request = None
191 try:
192 img_request = requests.request(
193 'get', img_url, stream=True, proxies=self.proxies)
194 if img_request.status_code != 200:
195 raise ImageDownloadError(img_request.status_code)
196 except:
197 raise ImageDownloadError()
198
199 if img_url[-3:] == "svg" or (int(img_request.headers['content-length']) > self.min_filesize and\
200 int(img_request.headers['content-length']) < self.max_filesize):
201 img_content = img_request.content
202 with open(os.path.join(self.download_path, img_url.split('/')[-1]), 'wb') as f:
203 byte_image = bytes(img_content)
204 f.write(byte_image)
205 else:
206 raise ImageSizeError(img_request.headers['content-length'])
207 return True
14def download(self, url):
15 response = self.session.get(url)
16 return response.content
29def download_image(self, image_url, output, timeout=30):
30 if os.path.exists(output) and os.path.getsize(output) != 0:
31 warnings.warn('图片已存在, pass', output)
32 return output
33 response = self.session.get(image_url, timeout=timeout)
34 if response.status_code == 200:
35 with open(output, 'wb') as f:
36 f.write(response.content)
37 return output
96def download(self, image_id):
97 """Download an image from the FarmBot Web App API."""
98 image_filename = self.plant_db.get_image(image_id)
99 if image_filename is None:
100 raise IOError("Image download failed.")
101 self.load(image_filename)
102 os.remove(image_filename)
9def downloadPhotoFromLink(link):
10
11 paramaters = urllib.urlencode({'link': link})
12 photo = urllib.urlopen("http://instagram.jonathanlking.com/engine/link?%s" % paramaters)
13 return photo
10def download(pid, image_list, base_url, save_dir, image_size=(512, 512)):
11 colors = ['red', 'green', 'blue', 'yellow']
12 for i in tqdm(image_list, postfix=pid):
13 img_id = i.split('_', 1)
14 for color in colors:
15 img_path = img_id[0] + '/' + img_id[1] + '_' + color + '.jpg'
16 img_name = i + '_' + color + '.png'
17 img_url = base_url + img_path
18
19 # Get the raw response from the url
20 r = requests.get(img_url, allow_redirects=True, stream=True)
21 r.raw.decode_content = True
22
23 # Use PIL to resize the image and to convert it to L
24 # (8-bit pixels, black and white)
25 im = Image.open(r.raw)
26 im = im.resize(image_size, Image.LANCZOS).convert('L')
27 im.save(os.path.join(save_dir, img_name), 'PNG')

Related snippets