Skip to content

Commit

Permalink
Merge pull request #29 from niteshgupta2711/master
Browse files Browse the repository at this point in the history
Resize functionality  added
  • Loading branch information
gurugaurav authored Aug 5, 2024
2 parents 900d4d6 + 0153d4d commit c6e39ec
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dataset/*
dataset
dist
image_search.egg-info
**/.vscode/*
**/.vscode/*
bing_image_downloader.egg-info
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pip install .
### Usage <br />
```python
from bing_image_downloader import downloader
downloader.download(query_string, limit=100, output_dir='dataset', adult_filter_off=True, force_replace=False, timeout=60, verbose=True)
downloader.download(query_string, limit=100, output_dir='dataset', adult_filter_off=True, force_replace=False, timeout=60,resize=(224,224) ,verbose=True)
```

`query_string` : String to be searched.<br />
Expand Down
54 changes: 45 additions & 9 deletions bing_image_downloader/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions bing_image_downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def download(query, limit=100, output_dir='dataset', adult_filter_off=True,
force_replace=False, timeout=60, filter="", verbose=True):
force_replace=False, timeout=60, filter="",resize=None, verbose=True):

# engine = 'bing'
if adult_filter_off:
Expand All @@ -34,7 +34,7 @@ def download(query, limit=100, output_dir='dataset', adult_filter_off=True,
sys.exit(1)

print("[%] Downloading Images to {}".format(str(image_dir.absolute())))
bing = Bing(query, limit, image_dir, adult, timeout, filter, verbose)
bing = Bing(query, limit, image_dir, adult, timeout, filter,resize, verbose)
bing.run()


Expand Down
Loading

0 comments on commit c6e39ec

Please sign in to comment.