python打开网络图片并缩放

2 篇文章 0 订阅

Supervisor运行Celery。

# -*- coding:utf-8 -*-  
''''' 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 
'''
''''' tk_image_view_url_io_resize.py 
用Tkinter, PIL和data_stream从一个url地址加载图片, 
能保持比例缩放适应显示大小, Pil便于缩放和读取各种图像格式, 
在Python27和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
    对一个pil_image对象进行缩放,让它在一个矩形框内,还能保持比例
    '''

    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 want  
# 期望图像显示的大小
w_box = 400
h_box = 400
# find yourself a picture on an internet web page you like  
# (right click on the picture, under properties copy the address)  
# a larger (1600 x 1200) picture from the internet  
# url name is long, so split it  
# 从网页上找到一个图片,复制它的网址,这里网址太长,所以分开了
url1 = "http://freeflowerpictures.net/image/flowers/petunia/"
url2 = "petunia-flower.jpg"
url = url1 + url2
image_bytes = urlopen(url).read()
# internal data file  
data_stream = io.BytesIO(image_bytes)

# open as a PIL image object  
# 以一个PIL图像对象打开
pil_image = Image.open(data_stream)


# get the size of the image  
# 获取图像的原始大小
w, h = pil_image.size

# resize the image so it retains its aspect ration  
# but fits into the specified display box  
# 缩放图像让它保持比例,同时限制在一个矩形框范围内
pil_image_resized = resize(w, h, w_box, h_box, pil_image)

# optionally show resized image info ...  
# get the size of the resized image  
# 也可以显示缩放后的图像信息,获取大小  
wr, hr = pil_image_resized.size

# split off image file name  
# 标题栏显示:缩放后的图像文件名和大小  
fname = url.split('/')[-1]
sf = "resized {} ({}x{})".format(fname, wr, hr)
root.title(sf)

# convert PIL image object to Tkinter PhotoImage object  
# 把PIL图像对象转变为Tkinter的PhotoImage对象  
tk_image = ImageTk.PhotoImage(pil_image_resized)

# put the image on a widget the size of the specified display box  
# Label: 这个小工具,就是个显示框,小窗口,把图像大小显示到指定的显示框   
label = tk.Label(root, image=tk_image, width=w_box, height=h_box)
# padx,pady是图像与窗口边缘的距离
label.pack(padx=5, pady=5)
root.mainloop()  

效果图:

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Flask应用程序中返回图像流以供前端展示,需要使用PythonPillow库和Flask的send_file功能。以下是如何实现的步骤: 1. 从Pillow库中导入Image模块。 ```python from PIL import Image ``` 2. 打开图像并将其转换为二进制数据。 ```python img = Image.open('image.png') img_byte_arr = io.BytesIO() img.save(img_byte_arr, format='PNG') img_byte_arr = img_byte_arr.getvalue() ``` 3. 使用Flask的send_file函数发送图像数据。 ```python from flask import Flask, send_file app = Flask(__name__) @app.route('/show_image') def show_image(): return send_file(io.BytesIO(img_byte_arr), mimetype='image/png') ``` 4. 在前端中通过img标签展示图像。 ```html <img src="{{ url_for('show_image') }}"> ``` 这样,当访问/show_image URL时,将返回包含图像的流数据,然后在前端通过img标签展示图像。 ### 回答2: Flask 是一款基于 Python 的微型 Web 框架,在实现 Web 应用中广受欢迎。在 Web 应用中,常常需要展示图片等静态资源,本文将介绍如何使用 Flask 实现返回图片流给前端展示。 首先,我们需要在 Flask 中引入相关库。在 Flask 中,我们使用 Pillow 库处理图片,使用 io 库将图片转化为二进制流。代码如下: ``` from flask import Flask, make_response, send_file from io import BytesIO from PIL import Image app = Flask(__name__) ``` 以上代码中,我们引入了 Flask、io 以及 Pillow 库。 接着,我们需要定义一个路由函数,用于响应用户请求,返回图片流。路由函数的具体实现如下: ``` @app.route('/image') def image(): # 打开一张图片 image_path = './test.jpg' with open(image_path, 'rb') as f: image_data = f.read() image = Image.open(BytesIO(image_data)) # 转化为二进制流 img_io = BytesIO() image.save(img_io, 'JPEG') img_io.seek(0) # 返回二进制流 response = make_response(img_io.getvalue()) response.headers['Content-Type'] = 'image/jpeg' return response ``` 第一步,我们使用 open 函数打开一张图片,并读取二进制数据。在示例代码中,我们将图片文件放在了代码根目录下,文件名为 test.jpg。 第二步,我们使用 Pillow 库将读取到的数据转换为 Image 对象,方便后续操作。 第三步,我们将 Image 对象转化为二进制流。在示例代码中,我们使用 BytesIO 对象将二进制数据存储起来,并将存储的数据格式转换为 JPEG 格式。 第四步,在将二进制流返回给前端之前,需要将其封装成一个 Response 对象。在代码中,我们使用 Flask 提供的 make_response 函数创建一个 Response 对象,并设置其 Content-Type 为 image/jpeg。 最后,我们将 Response 对象返回给前端即可实现图片流的展示。可以通过浏览器访问路由 /image,在页面上展示图片。 以上是使用 Flask 实现返回图片流给前端展示的方法,通过参考示例代码及相关库函数的用法,我们可以很快地实现将图片、音视频等二进制文件返回给前端的功能。 ### 回答3: 在Flask中,我们可以使用send_file()函数来返回一个图片(或者其他媒体文件)到前端展示。send_file()可以从本地文件系统中或者是通过网络获取的文件流来向客户端发送数据。 在Python中,我们可以使用Pillow或者OpenCV等库来处理图片,并将其转化为二进制数据流,方便在Flask框架中进行传输。 下面是一个简单的例子,它展示了如何在Flask中返回一张图片给前端展示: ```python from flask import Flask, send_file from PIL import Image import io app = Flask(__name__) @app.route('/getImage') def getImage(): # 读取图片文件 img = Image.open("test.jpg") # 将图片转化为二进制数据流 img_io = io.BytesIO() img.save(img_io, 'JPEG', quality=70) img_io.seek(0) # 返回二进制数据流给前端 return send_file(img_io, mimetype='image/jpeg') ``` 在这个例子中,我们首先使用Pillow库中的Image来读取一张图片文件。随后,我们将这张图片转化为二进制数据流,通过BytesIO类将其封装成可以在Flask中进行传输的对象。最后,我们利用send_file()函数将这个对象返回给前端展示。 需要注意的是,我们在调用send_file函数的时候设置mimetype为'image/jpeg',以便让浏览器正确解析这个数据流。如果我们返回的是其他类型的媒体文件,那么我们应该相应地修改mimetype参数。 在实际应用中,我们可能需要对图片进行裁剪、缩放、旋转、添加水印等处理,我们可以使用Pillow或者OpenCV等库来完成这些任务。我们只需要将处理后的图片通过类似上述代码的方式发送给前端即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值