PIL转换图片会导致糟糕的结果 [英] PIL convert picture leads to bad result

查看:399
本文介绍了PIL转换图片会导致糟糕的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过数码单反相机拍照,然后拍照,并希望使用PIL调整大小。这是核心代码

i take a photo via a DSLR, then ps it, and want to resize it using PIL. here is the core code

image = Image.open(img_obj, 'r')
for pic_size_name, pic_size_val in pic_sizes.items():
    width, height = [int(item) for item in pic_size_val.split('x')]
    img_width, img_height = image.size
    pic_save_path = os.path.join(
                        save_path,
                        hash_val + '_' + pic_size_name + '.jpg'
                        )

    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGBA')

    if width > img_width and height > img_height:
        image.save(pic_save_path, "jpeg", quality=90)
        continue

    img = image.copy()
    if pic_size_name == 's' or pic_size_name == 'xs':
        dest_ratio = float(width) / height
        current_ratio = float(img_width) / img_height
        if dest_ratio > current_ratio:
            offset = int((img_height - img_width / dest_ratio) / 2)
            box = (0, offset, img_width, img_height - offset)
        else:
            offset = int((img_width - img_height * dest_ratio) / 2)
            box = (offset, 0, img_width - offset, img_height)
        img = img.crop(box)
        img = img.resize((width, height), Image.ANTIALIAS)
        img.save(pic_save_path, "jpeg", quality=90)
    elif pic_size_name == 'm':
        new_height = img_height * width / img_width
        img = img.resize((width, new_height), Image.ANTIALIAS)
        img.save(pic_save_path, "jpeg", quality=90)
    else:
        img.thumbnail((width, height), Image.ANTIALIAS)
        img.save(pic_save_path, "jpeg")

,但调整结果不太好。

这是由PIL转换的:

http://cl.ly / CgR9

这是由Flickr转换的,应该是:

this is converted by Flickr which is what should be:

http://www.flickr.com/photos/lzyy/6524414285/sizes / z / in / photostream /

我使用PIL错了还是有一些我不知道的技巧?

am I using PIL wrong or there is some trick i don't know?

推荐答案

然而,下载和查看GIMP中的两个图像时,不同之处在于Flickr中的图像具有嵌入的颜色配置文件,而PIL生成的颜色配置文件不。由于我没有注意到任何对比度或锐度差异,我认为产生的色差是你所困扰的。

WHen downloading and looking at both images in GIMP, however, the difference is that the one in Flickr sported an embedded Color Profile, while the one generated by PIL did not. As I don't notice any contrast or sharpness differences, I suppose the resulting color difference is what you are troubled about.

你必须让你的PIL工作流程ppreservign任何颜色与图像相关联的配置文件 - 快速谷歌搜索带来了pyCMS,其首页有4-5行示例。很可能pyCMS将是你要求的:

You have to make your PIL workflow ppreservign any color profiles associated with the image - a quick google search brings up pyCMS which has got 4-5 lines examples in its frontpage. Most likely pyCMS will be what you are asking for:

http://www.cazabon.com/pyCMS/

这篇关于PIL转换图片会导致糟糕的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆