Skip to content

Commit

Permalink
merged the resize folder with bing module
Browse files Browse the repository at this point in the history
  • Loading branch information
niteshgupta2711 committed May 3, 2022
1 parent 118776f commit 0153d4d
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 27 deletions.
25 changes: 21 additions & 4 deletions bing_image_downloader/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@
import imghdr
import posixpath
import re
from resize.resize import resize
from resize.resize import image_to_byte_array
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='',resize=None, verbose=True):
Expand All @@ -28,7 +45,7 @@ def __init__(self, query, limit, output_dir, adult, timeout, filter='',resize=No
self.limit = limit
assert type(timeout) == int, "timeout must be integer"
self.timeout = timeout
assert type(resize)==tuple, "resize must be a tuple(height,width)"
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'}
Expand Down
Loading

0 comments on commit 0153d4d

Please sign in to comment.