Skip to content

Commit 274deb3

Browse files
authored
Merge pull request #16 from KTS-o7/dependencyResolve
Bugfix: Removed imagehdr as its deprecated. Used Filetype. Bumped Ver…
2 parents 406c6f7 + 9b8a450 commit 274deb3

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ This project is licensed under the terms of the MIT license.
111111
### Contact
112112

113113
If you have any questions or feedback, please contact us at [email](mailto:shentharkrishnatejaswi@gmail.com).
114+
115+
### Changelog
116+
117+
- 1.1.3:
118+
- Fixed issue with invalid image types. Deleted imghdr and used filetype to check image types.

better_bing_image_downloader/bing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from pathlib import Path
22
import urllib.request
33
import urllib
4-
import imghdr
54
import posixpath
65
import re
76
import logging
87
from tqdm import tqdm
8+
import filetype
99

1010
'''
1111
@@ -123,8 +123,8 @@ def save_image(self, link, file_path) -> None:
123123
try:
124124
request = urllib.request.Request(link, None, self.headers)
125125
image = urllib.request.urlopen(request, timeout=self.timeout).read()
126-
if not imghdr.what(None, image):
127-
126+
kind = filetype.guess(image)
127+
if kind is None or not kind.mime.startswith('image/'):
128128
logging.error('Invalid image, not saving %s', link)
129129
raise ValueError('Invalid image, not saving %s' % link)
130130
with open(str(file_path), 'wb') as f:

better_bing_image_downloader/helperdownload.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from __future__ import print_function
66

77
import shutil
8-
import imghdr
98
import os
109
import concurrent.futures
1110
import requests
1211
import socket
12+
import filetype
1313

1414
headers = {
1515
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
@@ -39,16 +39,15 @@ def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, p
3939
with open(file_path, 'wb') as f:
4040
f.write(response.content)
4141
response.close()
42-
file_type = imghdr.what(file_path)
43-
# if file_type is not None:
44-
if file_type in ["jpg", "jpeg", "png", "bmp", "webp"]:
45-
new_file_name = "{}.{}".format(file_name, file_type)
42+
kind = filetype.guess(file_path)
43+
if kind and kind.extension in ["jpg", "jpeg", "png", "bmp", "webp"]:
44+
new_file_name = "{}.{}".format(file_name, kind.extension)
4645
new_file_path = os.path.join(dst_dir, new_file_name)
4746
shutil.move(file_path, new_file_path)
4847
print("## OK: {} {}".format(new_file_name, image_url))
4948
else:
5049
os.remove(file_path)
51-
print("## Err: TYPE({}) {}".format(file_type, image_url))
50+
print("## Err: Invalid image type {}".format(image_url))
5251
break
5352
except Exception as e:
5453
if try_times < 3:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ charset-normalizer==3.3.2
55
chromedriver-autoinstaller==0.6.4
66
cryptography==42.0.2
77
docutils==0.20.1
8+
filetype==1.2.0
89
h11==0.14.0
910
idna==3.6
1011
importlib-metadata==6.0.0

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="better_bing_image_downloader",
8-
version="1.1.2",
8+
version="1.1.3",
99
author="Krishnatejaswi S",
1010
author_email="shentharkrishnatejaswi@gmail.com",
1111
description="This package is built on top of bing-image-downloader by gaurav singh",

0 commit comments

Comments
 (0)