python打开网络图片并缩放

来源:互联网 发布:英国买mac口红便宜吗 编辑:程序博客网 时间:2024/05/16 05:15


[python] view plain copy
  1. # -*- coding:utf-8 -*-  
  2. ''''' tk_image_view_url_io_resize.py 
  3. display an image from a URL using Tkinter, PIL and data_stream 
  4. also resize the web image to fit a certain size display widget 
  5. retaining its aspect ratio 
  6. Pil facilitates resizing and allows file formats other then gif 
  7. tested with Python27 and Python33 by vegaseat 18mar2013 
  8. '''  
  9. ''''' tk_image_view_url_io_resize.py 
  10. 用Tkinter, PIL和data_stream从一个url地址加载图片, 
  11. 能保持比例缩放适应显示大小, Pil便于缩放和读取各种图像格式, 
  12. 在Python27和Python33上都测试过了 by vegaseat 18mar2013 
  13. '''  
  14.   
  15. import io  
  16. from PIL import Image, ImageTk  
  17. try:  
  18.   # Python2  
  19.   import Tkinter as tk  
  20.   from urllib2 import urlopen  
  21. except ImportError:  
  22.   # Python3  
  23.   import tkinter as tk  
  24.   from urllib.request import urlopen  
  25. def resize(w, h, w_box, h_box, pil_image):  
  26.   ''''' 
  27.   resize a pil_image object so it will fit into 
  28.   a box of size w_box times h_box, but retain aspect ratio 
  29.   对一个pil_image对象进行缩放,让它在一个矩形框内,还能保持比例 
  30.   '''  
  31.     
  32.     
  33.   f1 = 1.0*w_box/w # 1.0 forces float division in Python2  
  34.   f2 = 1.0*h_box/h  
  35.   factor = min([f1, f2])  
  36.   #print(f1, f2, factor) # test  
  37.   # use best down-sizing filter  
  38.   width = int(w*factor)  
  39.   height = int(h*factor)  
  40.   return pil_image.resize((width, height), Image.ANTIALIAS)  
  41.   
  42. root = tk.Tk()  
  43. # size of image display box you want  
  44. #期望图像显示的大小  
  45. w_box = 400  
  46. h_box = 400  
  47. # find yourself a picture on an internet web page you like  
  48. # (right click on the picture, under properties copy the address)  
  49. # a larger (1600 x 1200) picture from the internet  
  50. # url name is long, so split it  
  51. #从网页上找到一个图片,复制它的网址,这里网址太长,所以分开了  
  52. url1 = "http://freeflowerpictures.net/image/flowers/petunia/"  
  53. url2 = "petunia-flower.jpg"  
  54. url = url1 + url2  
  55. image_bytes = urlopen(url).read()  
  56. # internal data file  
  57. data_stream = io.BytesIO(image_bytes)  
  58.   
  59. # open as a PIL image object  
  60. #以一个PIL图像对象打开  
  61. pil_image = Image.open(data_stream)  
  62.   
  63. # get the size of the image  
  64. #获取图像的原始大小  
  65. w, h = pil_image.size  
  66.   
  67. # resize the image so it retains its aspect ration  
  68. # but fits into the specified display box  
  69. #缩放图像让它保持比例,同时限制在一个矩形框范围内  
  70. pil_image_resized = resize(w, h, w_box, h_box, pil_image)  
  71.   
  72. # optionally show resized image info ...  
  73. # get the size of the resized image  
  74. # 也可以显示缩放后的图像信息,获取大小  
  75. wr, hr = pil_image_resized.size  
  76.   
  77. # split off image file name  
  78. # 标题栏显示:缩放后的图像文件名和大小  
  79. fname = url.split('/')[-1]  
  80. sf = "resized {} ({}x{})".format(fname, wr, hr)  
  81. root.title(sf)  
  82.   
  83. # convert PIL image object to Tkinter PhotoImage object  
  84. # 把PIL图像对象转变为Tkinter的PhotoImage对象  
  85. tk_image = ImageTk.PhotoImage(pil_image_resized)  
  86.   
  87. # put the image on a widget the size of the specified display box  
  88. # Label: 这个小工具,就是个显示框,小窗口,把图像大小显示到指定的显示框   
  89. label = tk.Label(root, image=tk_image, width=w_box, height=h_box)  
  90. #padx,pady是图像与窗口边缘的距离   
  91. label.pack(padx=5, pady=5)  
  92. root.mainloop()  
[python] view plain copy
  1. # -*- coding:utf-8 -*-  
  2. ''''' tk_image_view_url_io_resize.py 
  3. display an image from a URL using Tkinter, PIL and data_stream 
  4. also resize the web image to fit a certain size display widget 
  5. retaining its aspect ratio 
  6. Pil facilitates resizing and allows file formats other then gif 
  7. tested with Python27 and Python33 by vegaseat 18mar2013 
  8. '''  
  9. ''''' tk_image_view_url_io_resize.py 
  10. 用Tkinter, PIL和data_stream从一个url地址加载图片, 
  11. 能保持比例缩放适应显示大小, Pil便于缩放和读取各种图像格式, 
  12. 在Python27和Python33上都测试过了 by vegaseat 18mar2013 
  13. '''  
  14.   
  15. import io  
  16. from PIL import Image, ImageTk  
  17. try:  
  18.   # Python2  
  19.   import Tkinter as tk  
  20.   from urllib2 import urlopen  
  21. except ImportError:  
  22.   # Python3  
  23.   import tkinter as tk  
  24.   from urllib.request import urlopen  
  25. def resize(w, h, w_box, h_box, pil_image):  
  26.   ''''' 
  27.   resize a pil_image object so it will fit into 
  28.   a box of size w_box times h_box, but retain aspect ratio 
  29.   对一个pil_image对象进行缩放,让它在一个矩形框内,还能保持比例 
  30.   '''  
  31.     
  32.     
  33.   f1 = 1.0*w_box/w # 1.0 forces float division in Python2  
  34.   f2 = 1.0*h_box/h  
  35.   factor = min([f1, f2])  
  36.   #print(f1, f2, factor) # test  
  37.   # use best down-sizing filter  
  38.   width = int(w*factor)  
  39.   height = int(h*factor)  
  40.   return pil_image.resize((width, height), Image.ANTIALIAS)  
  41.   
  42. root = tk.Tk()  
  43. # size of image display box you want  
  44. #期望图像显示的大小  
  45. w_box = 400  
  46. h_box = 400  
  47. # find yourself a picture on an internet web page you like  
  48. # (right click on the picture, under properties copy the address)  
  49. # a larger (1600 x 1200) picture from the internet  
  50. # url name is long, so split it  
  51. #从网页上找到一个图片,复制它的网址,这里网址太长,所以分开了  
  52. url1 = "http://freeflowerpictures.net/image/flowers/petunia/"  
  53. url2 = "petunia-flower.jpg"  
  54. url = url1 + url2  
  55. image_bytes = urlopen(url).read()  
  56. # internal data file  
  57. data_stream = io.BytesIO(image_bytes)  
  58.   
  59. # open as a PIL image object  
  60. #以一个PIL图像对象打开  
  61. pil_image = Image.open(data_stream)  
  62.   
  63. # get the size of the image  
  64. #获取图像的原始大小  
  65. w, h = pil_image.size  
  66.   
  67. # resize the image so it retains its aspect ration  
  68. # but fits into the specified display box  
  69. #缩放图像让它保持比例,同时限制在一个矩形框范围内  
  70. pil_image_resized = resize(w, h, w_box, h_box, pil_image)  
  71.   
  72. # optionally show resized image info ...  
  73. # get the size of the resized image  
  74. # 也可以显示缩放后的图像信息,获取大小  
  75. wr, hr = pil_image_resized.size  
  76.   
  77. # split off image file name  
  78. # 标题栏显示:缩放后的图像文件名和大小  
  79. fname = url.split('/')[-1]  
  80. sf = "resized {} ({}x{})".format(fname, wr, hr)  
  81. root.title(sf)  
  82.   
  83. # convert PIL image object to Tkinter PhotoImage object  
  84. # 把PIL图像对象转变为Tkinter的PhotoImage对象  
  85. tk_image = ImageTk.PhotoImage(pil_image_resized)  
  86.   
  87. # put the image on a widget the size of the specified display box  
  88. # Label: 这个小工具,就是个显示框,小窗口,把图像大小显示到指定的显示框   
  89. label = tk.Label(root, image=tk_image, width=w_box, height=h_box)  
  90. #padx,pady是图像与窗口边缘的距离   
  91. label.pack(padx=5, pady=5)  
  92. root.mainloop()