我是整个 mod_wsgi 的新手,并通过 apache 提供文件。我对烧瓶真的很满意,但这是我无法理解的。我做了hello-world程序,成功显示hello world!现在我想显示一个图像文件。所以我将我的 hello-world.py 更新为:
from flask import *
yourflaskapp = Flask(__name__)
@yourflaskapp.route("/")
def hello():
file="203.jpg"
return render_template("hello.html",file=file)
# return"HEY"
if __name__ == "__main__":
yourflaskapp.run()
Run Code Online (Sandbox Code Playgroud)
我的目录结构类似于:/var/www/hello-world
/hello-world
test.py
yourflaskapp.wsgi
/static
-203.jpg
/templates
-hello.html
Run Code Online (Sandbox Code Playgroud)
我的模板很简单:
<!DOCTYPE html>
<html><head><title>hi</title></head>
<body>
<img src="{{url_for('static',filename=file)}}"/>
</body></html>
Run Code Online (Sandbox Code Playgroud)
我的 apache conf 文件是:
<VirtualHost *:80>
WSGIDaemonProcess yourflaskapp
WSGIScriptAlias / /var/www/hello-world/yourflaskapp.wsgi
Alias /static/ /var/www/hello-world/static
Alias /templates/ /var/www/hello-world/templates
<Directory /var/www/hello-world>
WSGIProcessGroup yourflaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/hello-world/static>
Order allow,deny
Allow from all …
Run Code Online (Sandbox Code Playgroud) 我在增加图像的不透明度方面遇到问题。我的原始图像是 230 KB
在我使用代码调整图像大小后:
Method 1: imh=imgg.resize((1000,500),Image.ANTIALIAS) #filesize is 558 KB
Method 2: imh=imgg.resize((1000,500),Image.ANTIALIAS)
im2 = imh.convert('P', palette=Image.ADAPTIVE) #filesize is 170KB
Run Code Online (Sandbox Code Playgroud)
现在我使用以下代码添加图像的透明度:
def reduce_opacity(im, opacity,pathname):
assert opacity >= 0 and opacity <= 1
if im.mode != 'RGBA':
im = im.convert('RGBA')
else:
im = im.copy()
alpha = im.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
im.putalpha(alpha)
jj=<pathname>
im.save(jj)
return im
Run Code Online (Sandbox Code Playgroud)
方法 1:文件大小为 598 KB 方法 2:文件大小为 383 KB
所以到目前为止我得到的最好的代码是
imh=imgg.resize((1000,500),Image.ANTIALIAS)
im2 = imh.convert('P', palette=Image.ADAPTIVE)
reduce_opacity(im2,0.5,name)
Run Code Online (Sandbox Code Playgroud)
这给了我 383KB 的文件大小。要添加不透明度,它必须以 RGBA 模式打开,这会将文件大小从 170 KB 增加到 383 KB。我对此不满意,我需要进一步减小大小,有什么办法可以实现这一点,而不是妥协质量在很大程度上?