枕头中可识别SRGB的图像大小 [英] SRGB-aware image resize in Pillow

查看:124
本文介绍了枕头中可识别SRGB的图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枕头的基本 Image.resize 函数不起作用" t似乎没有任何可识别SRGB的选项.有没有办法在枕头中调整支持SRGB的大小?

Pillow's basic Image.resize function doesn't appear to have any options for SRGB-aware filtering. Is there a way to do SRGB-aware resizing in Pillow?

我可以通过将图像转换为浮动图像并自己应用SRGB变换来手动完成此操作...但是我希望有一种内置方法.

I could do it manually by converting the image to float and applying the SRGB transforms myself...but I'm hoping there's a built-in way.

推荐答案

我最终使用以下例程实现了可感知sRGB的调整大小.它可以拍摄8位RGB图像,目标尺寸和重采样滤镜.

I ended up implementing sRGB-aware resize myself using the following routine. It takes an 8-bit RGB image and a target size and resampling filter.

from PIL import Image
import numpy as np

def SRGBResize(im, size, filter):
    # Convert to numpy array of float
    arr = np.array(im, dtype=np.float32) / 255.0
    # Convert sRGB -> linear
    arr = np.where(arr <= 0.04045, arr/12.92, ((arr+0.055)/1.055)**2.4)
    # Resize using PIL
    arrOut = np.zeros((size[1], size[0], arr.shape[2]))
    for i in range(arr.shape[2]):
        chan = Image.fromarray(arr[:,:,i])
        chan = chan.resize(size, filter)
        arrOut[:,:,i] = np.array(chan)
    # Convert linear -> sRGB
    arrOut = np.where(arrOut <= 0.0031308, 12.92*arrOut, 1.055*arrOut**(1.0/2.4) - 0.055)
    # Convert to 8-bit
    arrOut = np.uint8(np.rint(arrOut * 255.0))
    # Convert back to PIL
    return Image.fromarray(arrOut)

这篇关于枕头中可识别SRGB的图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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