Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PIL转换图片导致不良结果_Python_Image Processing_Python Imaging Library - Fatal编程技术网

Python PIL转换图片导致不良结果

Python PIL转换图片导致不良结果,python,image-processing,python-imaging-library,Python,Image Processing,Python Imaging Library,我通过数码单反拍摄了一张照片,然后对其进行ps,并希望使用PIL调整其大小。下面是核心代码 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_sa

我通过数码单反拍摄了一张照片,然后对其进行ps,并希望使用PIL调整其大小。下面是核心代码

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转换的:

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


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

在GIMP中下载和查看这两个图像时,区别在于Flickr中的图像显示了嵌入的颜色配置文件,而PIL生成的图像则没有。由于我没有注意到任何对比度或锐度的差异,我想由此产生的色差就是你所担心的

你必须让你的PIL工作流ppreservign任何与图像相关的颜色配置文件-一个快速的谷歌搜索会出现pyCMS,它的首页有4-5行的例子。最有可能的pyCMS将是您所要求的:


我看不出PIL的链接和FlickrAs的链接有什么区别,不可能同时比较两张图片。Flikr为我们提供了白色背景和不同的图像大小。另一个是黑色背景。这样做没有什么区别。仅供参考,Flickr通常会对照片进行增强,我认为会进行一些锐化和色彩平衡。