Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1aa822f

Browse files
Merge pull request avinashkranjan#1134 from Ayushjain2205/image-search-display
Image viewer and download (web scraped image)
2 parents d66a196 + 19050d0 commit 1aa822f

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

‎Image-scrape-download/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Image viewer and downloader
2+
Running this Script would allow the user to search for any image by entering a search query, the image will then be scraped and displayed in the GUI, and providing an option to download the image as well
3+
4+
## Setup instructions
5+
In order to run this script, you need to have Python and pip installed on your system. After you're done installing Python and pip, run the following command from your terminal to install the requirements from the same folder (directory) of the project.
6+
```
7+
pip install -r requirements.txt
8+
```
9+
After satisfying all the requirements for the project, Open the terminal in the project folder and run
10+
```
11+
python image.py
12+
```
13+
or
14+
```
15+
python3 image.py
16+
```
17+
depending upon the python version. Make sure that you are running the command from the same virtual environment in which the required modules are installed.
18+
19+
## Output
20+
21+
The user can enter the search query in the input box and then the image will be displayed as shown in the screenshot below
22+
23+
![Image viewer and downloader](https://i.postimg.cc/HnxwB2sx/img.png)
24+
25+
## Author
26+
[Ayush Jain](https://github.com/Ayushjain2205)

‎Image-scrape-download/image.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Import Module
2+
from tkinter import *
3+
from tkhtmlview import HTMLLabel
4+
from tkinter import ttk
5+
from tkinter import font as tkFont
6+
import requests
7+
from bs4 import BeautifulSoup
8+
import urllib3
9+
import shutil
10+
11+
# Function to save image to the users file system
12+
def saveImage():
13+
file_name = search_box.get().replace(" ","")
14+
image_file = getImage()
15+
http = urllib3.PoolManager()
16+
with open(file_name, 'wb') as out:
17+
r = http.request('GET', image_file, preload_content=False)
18+
shutil.copyfileobj(r, out)
19+
20+
# Function to scrape image source from bing
21+
def getImage():
22+
search = search_box.get()
23+
url = "https://www.bing.com/images/search?q={}".format(search.replace(' ','?'))
24+
page = requests.get(url)
25+
26+
# Start scraping resultant html data
27+
soup = BeautifulSoup(page.content, 'html.parser')
28+
images_div = soup.find('div',{'id':'mmComponent_images_2'})
29+
images_ul = images_div.find('ul')
30+
31+
image = images_ul.find("li")
32+
link = image.find('img').get('src')
33+
34+
return link
35+
36+
# Function to show image in the GUI
37+
def showImage():
38+
link = getImage()
39+
str_value = '<img src="{}">'.format(link)
40+
my_label.set_html(str_value)
41+
42+
43+
# Create tkinter Object
44+
root = Tk()
45+
46+
# Set Geomerty of window
47+
root.geometry("400x500")
48+
root.title("Image viewer and downloader")
49+
50+
# Set styles
51+
style = ttk.Style()
52+
style.theme_use('alt')
53+
style.map('my.TButton', background=[('active','white')])
54+
style.configure('my.TButton', font=('Helvetica', 16, 'bold'))
55+
style.configure('my.TButton', background='white')
56+
style.configure('my.TButton', foreground='orange')
57+
style.configure('my.TFrame', background='white')
58+
59+
# Add labels and buttons
60+
my_label = HTMLLabel(root)
61+
62+
search_box = Entry(root, font=("Helvetica 15"), bd = 2, width=60)
63+
search_box.pack(side = TOP, pady=5, padx=15, ipadx=5)
64+
65+
search_btn = ttk.Button(text="Scrape Image!",command=showImage,style='my.TButton')
66+
search_btn.pack(side=TOP)
67+
68+
save_btn = ttk.Button(text="Download Image!",command=saveImage,style='my.TButton')
69+
save_btn.pack(side=TOP)
70+
71+
my_label.pack(pady=20, padx=20)
72+
# Execute Tkinter
73+
root.mainloop()

‎Image-scrape-download/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests
2+
beautifulsoup4
3+
urllib3

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /