在Django上无损压缩图像 [英] Losslessly compressing images on django
问题描述
我正在做优化,Google建议对图像进行无损压缩,以寻找在Django中实现此方法的方法.
I'm doing optimization and Google recommends Lossless compression to images, looking for a way to implement this in Django.
这是他们指定的图像,我认为要使其有效地完成,可能需要使用中间件类在系统范围内实现,想知道以前是否有人这样做过.这是Google Analytics for pagespeed的链接 https://developers.google.com/speed/pagespeed/insights/?url=www.kenyabuzz.com
Here's the images they specified, I think for it to be done effectively it needs to implemented systemwide possibly using a middleware class wondering if anyone has done this before. Here's the link to google analytics for pagespeed https://developers.google.com/speed/pagespeed/insights/?url=www.kenyabuzz.com
优化图像 正确格式化和压缩图像可以节省许多字节的数据. 优化以下图像以将其大小减小627.3KiB(减少74%).
Optimize images Properly formatting and compressing images can save many bytes of data. Optimize the following images to reduce their size by 627.3KiB (74% reduction).
Losslessly compressing http://www.kenyabuzz.com/media/uploads/clients/kenya_buzz_2.jpg could save 594.3KiB (92% reduction).
Losslessly compressing http://www.kenyabuzz.com/media/uploads/clients/new_tribe_2.jpg could save 25KiB (44% reduction).
Losslessly compressing http://www.kenyabuzz.com/…a/uploads/clients/EthiopianAirlines2.jpg could save 3KiB (22% reduction).
Losslessly compressing http://www.kenyabuzz.com/static/kb/images/Nightlife.Homepage.jpg could save 1.3KiB (2% reduction).
Losslessly compressing http://www.kenyabuzz.com/static/kb/img/social/blog.png could save 1.1KiB (43% reduction).
Losslessly compressing http://www.kenyabuzz.com/static/kb/img/social/twitter.png could save 969B (52% reduction).
Losslessly compressing http://www.kenyabuzz.com/…der-Board---Email-Signature--Neutral.jpg could save 920B (2% reduction).
Losslessly compressing http://www.kenyabuzz.com/static/kb/img/social/youtube.png could save 757B (31% reduction).
推荐答案
毫不费力地压缩 http://www.kenyabuzz.com/media/uploads/client/kenya_buzz_2.jpg 可以节省594.3KiB(减少92%).
Losslessly compressing http://www.kenyabuzz.com/media/uploads/clients/kenya_buzz_2.jpg could save 594.3KiB (92% reduction).
首先,日志中的信息颇具误导性,因为无法使用无损格式将图像压缩92%(某些情况下,例如单色图像,基本几何形状,例如正方形等).阅读此答案和此答案了解更多信息.确实,阅读它们,都是很好的答案.
First of all, the information in the logs is rather misleading because it is impossible to compress images by 92% using a lossless format (except for some cases like single-colour images, basic geometric shapes like squares, etc). Read this answer and this answer for more info. Really, do read them, both are excellent answers.
第二,您可以使用有损压缩格式不损失质量" –差异是如此微妙,人眼甚至都没有注意到.
Second, you can use lossy compression formats "without losing quality" – the differences are so subtle, human eye doesn't even notice.
因此,我通过以下链接从正在优化的网站上下载了图片: http://www.kenyabuzz.com/media/uploads/clients/kenya_buzz_2.jpg
So, I downloaded an image from the website you're optimizing from this link: http://www.kenyabuzz.com/media/uploads/clients/kenya_buzz_2.jpg
我打开了我的Python控制台并编写了这个代码:
I opened my Python console and wrote this:
>>> from PIL import Image
>>> # Open the image
>>> im = Image.open("kenya_buzz_2.jpg")
>>> # Now save it
>>> im.save("kenya_buzz_compressed.jpg", format="JPEG", quality=70)
这在磁盘上创建了一个新映像.下面是两个图像.
This created a new image on my disk. Below are both the images.
原始(655.3kB)
受压缩(质量= 70时,压缩22.4kB降低了96%)
您可以使用quality
选项进行操作.例如,80
的值将为您提供质量更好的图像,但尺寸稍大.
You can play around with the quality
option. Like, value of 80
will give you a better quality image but with a little larger size.
由于这是一个非常受欢迎的问题,我决定添加一个示例代码来压缩Django中的图像.
Since this is a pretty popular question, I've decided to add a sample code to compress images in Django.
此代码适用于Django> = 1.7.
This code works for Django >= 1.7.
from io import BytesIO
from PIL import Image
from django.core.files import File
def compress(image):
im = Image.open(image)
# create a BytesIO object
im_io = BytesIO()
# save image to BytesIO object
im.save(im_io, 'JPEG', quality=70)
# create a django-friendly Files object
new_image = File(im_io, name=image.name)
return new_image
这是您可以在Django模型(或其他任何地方)中使用以上compress
函数的方法:
And this is how you can use the above compress
function in your Django model (or anywhere):
# models.py
class MyModel(...):
image = models.ImageField(...)
def save(self, *args, **kwargs):
# call the compress function
new_image = compress(self.image)
# set self.image to new_image
self.image = new_image
# save
super().save(*args, **kwargs)
基本上就是这样.这是相当基本的代码.您可以通过仅在图像更改时而不是在每次保存模型时才压缩图像来改进代码.
That is basically it. This is fairly basic code. You can improve the code by compressing the image only when the image changes, not every time the model is saved.
这篇关于在Django上无损压缩图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!