python - 如何压缩从 url 获取的 PNG 图像而不更改其尺寸

标签 python image google-app-engine

我正在尝试将图片从 url 保存到数据存储区。不幸的是,我的应用程序遇到了相当小的 PNG 文件,> 1MB。

把格式改成JPG就可以了

def save_image_from_url(url):
    image = urllib2.urlopen(url).read()

    if len(image)>1000000:
        # MAKE FILESIZE <1MB
        # WITHOUT CHANGING DIMENSTIONS

    picture = classes.Pictures(picture=image)
    picture.put()

    return str(picture.key.id())

编辑:我更愿意使用from google.appengine.api import images

最佳答案

好吧,我明白了。

if len(image)>1000000:
    temp = images.Image(image)
    image = images.resize(
        image, 
        width=temp.width, 
        output_encoding=images.JPEG, 
        quality=80
    )

关于python - 如何压缩从 url 获取的 PNG 图像而不更改其尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313338/

相关文章:

python - Flask:Apache Httpd 字体端和 websocket 支持

python - 检索 Excel 工作簿连接属性

python - GAE Python - 将数据库查询保存到cookie中

file - 从 AppEngine 部署中删除不需要的上传

java - 获取 java.lang.RuntimeException : Unable to complete the HTTP request while uploading document

python - 如何从列表列表中获取整数

python - 当从组合框发出信号时将布局插入 QDialog

java - 调用 ImageIO.read(file) 时程序阻塞

java - 在 JFrame 中显示 .png 图像?

ios - 快速下载图像(如果服务器上存在)