下面的代码内容是关于python通过PIL缩放互联网上的图片并保存的代码,应该对小伙伴有较大用。
‘’’ tk_image_view_url_io_resize.py
display an image from a URL using Tkinter, PIL and data_stream
also resize the web image to fit a certain size display widget
retaining its aspect ratio
Pil facilitates resizing and allows file formats other then gif
tested with Python27 and Python33 by vegaseat 18mar2013
‘’’
import io
from PIL import Image, ImageTk
try:
# Python2
import Tkinter as tk
from urllib2 import urlopen
except ImportError:
# Python3
import tkinter as tk
from urllib.request import urlopen
def resize(w, h, w_box, h_box, pil_image):
‘’’
resize a pil_image object so it will fit into
a box of size w_box times h_box, but retain aspect ratio
‘’’
factor = min([f1, f2])
#print(f1, f2, factor) # test
# use best down-sizing filter
return pil_image.resize((width, hei