2011-08-28 141 views
0

大多数上传到我的网站的所有JPEG文件的保存在​​逐行扫描格式,但没有通过SORL-缩略图生成的缩略图时,原始图像生成渐进。在创建大图像的缩略图时,这非常重要,例如,用于在旋转木马/滑块内显示。SORL-缩略图:保存的JPEG文件“先进性”

我已经sumbitted an issue上SORL的跟踪器,但我想,也许您根据现有的自定义后端,可以解决这个问题。有任何想法吗?

谢谢!

回答

2
from sorl.thumbnail.engines import pil_engine 

class ProgressiveBackend(pil_engine.Engine): 
    def _get_raw_data(self, image, format_, quality): 
     ImageFile.MAXBLOCK = 1024 * 1024 
     buf = StringIO() 
     try: 
      if format_=='JPEG': 
       image.save(buf, format=format_, quality=quality, optimize=1, progressive=image.progressive) 
      else: 
       image.save(buf, format=format_, quality=quality, optimize=1) 
     except IOError: 
      image.save(buf, format=format_, quality=quality) 
     raw_data = buf.getvalue() 
     buf.close() 
     return raw_data 
+0

我只是测试了一个很像这样的东西(大部分是PIL引擎的方法的复制粘贴)。在我的版本中,我没有检查格式是否为JPEG(这确实是必要的?)。真正的问题是'image.progressive'。我使用Python 2.6.7和1.1.7 PIL,并尝试这个答案时,该'image' PARAM没有暴露的'progressive'属性。然后我用'真',一切正常。那么是什么只留给这个答案是如何_detect_如果原始图像是渐进式JPEG或没有。 –

+0

是否有任何伤害使他们都进步? – Thomas

+0

我想没有,只是一个一致性的事......不管怎样,在这个方法很麻烦,以确定是否JPEG图像是逐行或没有,所以我接受你的答案,因为它是我现在使用。 –