-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from niteshgupta2711/master
Resize functionality added
- Loading branch information
Showing
5 changed files
with
437 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ dataset/* | |
dataset | ||
dist | ||
image_search.egg-info | ||
**/.vscode/* | ||
**/.vscode/* | ||
bing_image_downloader.egg-info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,27 +4,49 @@ | |
import imghdr | ||
import posixpath | ||
import re | ||
from PIL import Image | ||
from io import BytesIO | ||
import io | ||
|
||
|
||
''' | ||
Python api to download image form Bing. | ||
Author: Guru Prasad ([email protected]) | ||
''' | ||
def image_to_byte_array(image: Image) -> bytes: | ||
imgByteArr = io.BytesIO() | ||
image.save(imgByteArr, format="PNG") | ||
imgByteArr = imgByteArr.getvalue() | ||
return imgByteArr | ||
|
||
|
||
def resize(url,size: tuple): | ||
|
||
response = urllib.request.urlopen(url) | ||
img = Image.open(BytesIO(response.read())) | ||
img=img.resize(size=size,resample=Image.LANCZOS) | ||
#kl=image_to_byte_array(img) | ||
# with open('pn.png','wb') as f: | ||
# f.write(kl) | ||
return img | ||
|
||
class Bing: | ||
def __init__(self, query, limit, output_dir, adult, timeout, filter='', verbose=True): | ||
def __init__(self, query, limit, output_dir, adult, timeout, filter='',resize=None, verbose=True): | ||
self.download_count = 0 | ||
self.query = query | ||
self.output_dir = output_dir | ||
self.adult = adult | ||
self.filter = filter | ||
self.verbose = verbose | ||
self.seen = set() | ||
|
||
|
||
assert type(limit) == int, "limit must be integer" | ||
self.limit = limit | ||
assert type(timeout) == int, "timeout must be integer" | ||
self.timeout = timeout | ||
assert (type(resize)==tuple) or (resize is None), "resize must be a tuple(height,width)" | ||
self.resize=resize | ||
|
||
# self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0'} | ||
self.page_counter = 0 | ||
|
@@ -54,16 +76,30 @@ def get_filter(self, shorthand): | |
|
||
|
||
def save_image(self, link, file_path): | ||
request = urllib.request.Request(link, None, self.headers) | ||
image = urllib.request.urlopen(request, timeout=self.timeout).read() | ||
if not imghdr.what(None, image): | ||
print('[Error]Invalid image, not saving {}\n'.format(link)) | ||
raise ValueError('Invalid image, not saving {}\n'.format(link)) | ||
with open(str(file_path), 'wb') as f: | ||
f.write(image) | ||
if not self.resize: | ||
|
||
|
||
request = urllib.request.Request(link, None, self.headers) | ||
image = urllib.request.urlopen(request, timeout=self.timeout).read() | ||
if not imghdr.what(None, image): | ||
print('[Error]Invalid image, not saving {}\n'.format(link)) | ||
raise ValueError('Invalid image, not saving {}\n'.format(link)) | ||
with open(str(file_path), 'wb') as f: | ||
f.write(image) | ||
elif self.resize: | ||
request = urllib.request.Request(link, None, self.headers) | ||
|
||
img=resize(request,size=self.resize) | ||
image=image_to_byte_array(img) | ||
# if not imghdr.what(None, image): | ||
# print('[Error]Invalid image, not saving {}\n'.format(link)) | ||
# raise ValueError('Invalid image, not saving {}\n'.format(link)) | ||
with open(str(file_path), 'wb') as f: | ||
f.write(image) | ||
|
||
|
||
|
||
def download_image(self, link): | ||
|
||
self.download_count += 1 | ||
# Get the image link | ||
try: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.