Python uses PIL to zoom the network picture and save the method

Source: Internet
Author: User
This example describes how Python uses PIL to scale a network picture and save it. Share to everyone for your reference. The implementation method is as follows:

' Tk_image_view_url_io_resize.pydisplay an image from a URL using Tkinter, PIL and data_streamalso resize the web image To fit a certain size display widgetretaining it aspect Ratiopil facilitates resizing and allows file formats other then Giftested with Python27 and Python33 by Vegaseat 18mar2013 "" Import iofrom PIL import Image, imagetktry: # Python2 Impor T Tkinter as TK from urllib2 import urlopenexcept importerror: # Python3 import Tkinter as TK from Urllib.request Impo RT Urlopendef Resize (W, H, W_box, H_box, pil_image): ' Resize a Pil_image object so it would fit into a box of size w_  Box times H_box, but retain aspect ratio "' F1 = 1.0*w_box/w # 1.0 Forces Float Division in Python2 f2 = 1.0*h_box/h factor = min ([f1, F2]) #print (F1, F2, factor) # test # Use best down-sizing filter width = Int (w*factor) height = int ( H*factor) return Pil_image.resize ((width, height), image.antialias) root = Tk. Tk () # Size of image display box you wantw_box = 400h_box = 350# Find yourself a picture in the Internet Web page you like# (right click on the picture, under Properties copy the address) # a L Arger (x) picture from the internet# URL name is a long, so split iturl1 = "HTTP://FREEFLOWERPICTURES.NET/IMAGE/FL owers/petunia/"URL2 =" petunia-flower.jpg "url = url1 + url2image_bytes = Urlopen (URL). read () # internal Data filedata_stre am = io. Bytesio (image_bytes) # Open as a PIL image objectpil_image = Image.open (data_stream) # Get the size of the Imagew, h = pil_i mage.size# Resize the image so it retains its aspect ration# but fits into the specified display boxpil_image_resized = RE Size (W, H, W_box, H_box, Pil_image) # Optionally show resized image info ... # Get the size of the resized imagewr, hr = PiL _image_resized.size# split off image file Namefname = url.split ('/') [ -1]SF = ' resized {} ({}x{}) '. Format (fname, WR, HR) Roo T.title (SF) # convert PIL Image object to Tkinter photoimage objecttk_image = Imagetk.photoimage (pil_image_resized) # put th e image on a WIDGET the size of the specified display Boxlabel = tk. Label (Root, Image=tk_image, Width=w_box, Height=h_box) label.pack (padx=5, pady=5) Root.mainloop ()

Hopefully this article will help you with Python programming.

  • Related Article

    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.