How to Use Tkinter to display network images in python

Source: Internet
Author: User

How to Use Tkinter to display network images in python

 How to Use Tkinter to display network images in python

This example describes how to use Tkinter to display network images in python. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

'''Tk_image_view_url_io.py

Display an image from a URL using Tkinter, PIL and data_stream

Tested with Python27 and Python33 by vegaseat 01mar2013

'''

Import io

# Allows for image formats other than gif

From PIL import Image, ImageTk

Try:

# Python2

Import Tkinter as tk

From urllib2 import urlopen

Failed t ImportError:

# Python3

Import tkinter as tk

From urllib. request import urlopen

Root = tk. Tk ()

# Find yourself a picture on an internet web page you like

# (Right click on the picture, under properties copy the address)

# Url = "http://www.google.com/intl/en/images/logo.gif"

# Or use image previusly downloaded to tinypic.com

# Url = "http://i48.tinypic.com/w6sjn6.jpg"

Url = "http://i50.tinypic.com/34g8vo5.jpg"

Image_bytes = urlopen (url). read ()

# Internal data file

Data_stream = io. BytesIO (image_bytes)

# Open as a PIL image object

Pil_image = Image. open (data_stream)

# Optionally show image info

# Get the size of the image

W, h = pil_image.size

# Split off image file name

Fname = url. split ('/') [-1]

Sf = "{} ({} x {})". format (fname, w, h)

Root. title (sf)

# Convert PIL image object to Tkinter PhotoImage object

Tk_image = ImageTk. PhotoImage (pil_image)

# Put the image on a typical widget

Label = tk. Label (root, image = tk_image, bg = 'brown ')

Label. pack (padx = 5, pady = 5)

Root. mainloop ()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.