python打开网络图片并缩放
来源:互联网 发布:英国买mac口红便宜吗 编辑:程序博客网 时间:2024/05/16 05:15
-
- ''
-
-
-
-
-
-
- ''
-
-
-
-
-
- import io
- from PIL import Image, ImageTk
- try:
-
- import Tkinter as tk
- from urllib2 import urlopen
- except ImportError:
-
- import tkinter as tk
- from urllib.request import urlopen
- def resize(w, h, w_box, h_box, pil_image):
- ''
-
-
-
-
-
-
- f1 = 1.0*w_box/w
- f2 = 1.0*h_box/h
- factor = min([f1, f2])
-
-
- width = int(w*factor)
- height = int(h*factor)
- return pil_image.resize((width, height), Image.ANTIALIAS)
-
- root = tk.Tk()
-
-
- w_box = 400
- h_box = 400
-
-
-
-
-
- url1 = "http://freeflowerpictures.net/image/flowers/petunia/"
- url2 = "petunia-flower.jpg"
- url = url1 + url2
- image_bytes = urlopen(url).read()
-
- data_stream = io.BytesIO(image_bytes)
-
-
-
- pil_image = Image.open(data_stream)
-
-
-
- w, h = pil_image.size
-
-
-
-
- pil_image_resized = resize(w, h, w_box, h_box, pil_image)
-
-
-
-
- wr, hr = pil_image_resized.size
-
-
-
- fname = url.split('/')[-1]
- sf = "resized {} ({}x{})".format(fname, wr, hr)
- root.title(sf)
-
-
-
- tk_image = ImageTk.PhotoImage(pil_image_resized)
-
-
-
- label = tk.Label(root, image=tk_image, width=w_box, height=h_box)
-
- label.pack(padx=5, pady=5)
- root.mainloop()
-
- ''
-
-
-
-
-
-
- ''
-
-
-
-
-
- import io
- from PIL import Image, ImageTk
- try:
-
- import Tkinter as tk
- from urllib2 import urlopen
- except ImportError:
-
- import tkinter as tk
- from urllib.request import urlopen
- def resize(w, h, w_box, h_box, pil_image):
- ''
-
-
-
-
-
-
- f1 = 1.0*w_box/w
- f2 = 1.0*h_box/h
- factor = min([f1, f2])
-
-
- width = int(w*factor)
- height = int(h*factor)
- return pil_image.resize((width, height), Image.ANTIALIAS)
-
- root = tk.Tk()
-
-
- w_box = 400
- h_box = 400
-
-
-
-
-
- url1 = "http://freeflowerpictures.net/image/flowers/petunia/"
- url2 = "petunia-flower.jpg"
- url = url1 + url2
- image_bytes = urlopen(url).read()
-
- data_stream = io.BytesIO(image_bytes)
-
-
-
- pil_image = Image.open(data_stream)
-
-
-
- w, h = pil_image.size
-
-
-
-
- pil_image_resized = resize(w, h, w_box, h_box, pil_image)
-
-
-
-
- wr, hr = pil_image_resized.size
-
-
-
- fname = url.split('/')[-1]
- sf = "resized {} ({}x{})".format(fname, wr, hr)
- root.title(sf)
-
-
-
- tk_image = ImageTk.PhotoImage(pil_image_resized)
-
-
-
- label = tk.Label(root, image=tk_image, width=w_box, height=h_box)
-
- label.pack(padx=5, pady=5)
- root.mainloop()