Using Python PIL, I'm trying to adjust the hue of a given image.

I'm not very comfortable with the jargon of graphics, so what I mean by “adjusting hue” is doing the Photoshop operation called “Hue/saturation”: this is to change the color of the image uniformly as shown below:

Original:

With hue adjusted to +180 (red):

With hue adjusted to -78 (green):

FYI, Photoshop uses a scale of -180 to +180 for this hue setting (where -180 equals +180), that may represents the HSL hue scale (expressed in 0-360 degree).

What I'm looking for is a function that, given an PIL image and a float hue within [0, 1] (or int within [0, 360], it doesn't matter), returns the image with its hue shifted by hue as in the example above.

What I've done so far is ridiculous and obviously doesn't give the desired result. It just half-blend my original image with a color-filled layer.

import Image

im = Image.open('tweeter.png')

layer = Image.new('RGB', im.size, 'red') # "hue" selection is done by choosing a color...

output = Image.blend(im, layer, 0.5)

output.save('output.png', 'PNG')

(Please-don't-laugh-at-) result:

Thanks in advance!

Solution: here is the unutbu code updated so it fits exactly what I've described.

import Image

import numpy as np

import colorsys

rgb_to_hsv = np.vectorize(colorsys.rgb_to_hsv)

hsv_to_rgb = np.vectorize(colorsys.hsv_to_rgb)

def shift_hue(arr, hout):

r, g, b, a = np.rollaxis(arr, axis=-1)

h, s, v = rgb_to_hsv(r, g, b)

h = hout

r, g, b = hsv_to_rgb(h, s, v)

arr = np.dstack((r, g, b, a))

return arr

def colorize(image, hue):

"""

Colorize PIL image `original` with the given

`hue` (hue within 0-360); returns another PIL image.

"""

img = image.convert('RGBA')

arr = np.array(np.asarray(img).astype('float'))

new_img = Image.fromarray(shift_hue(arr, hue/360.).astype('uint8'), 'RGBA')

return new_img

解决方案

There is Python code to convert RGB to HSV (and vice versa) in the colorsys module in the standard library. My first attempt used

rgb_to_hsv=np.vectorize(colorsys.rgb_to_hsv)

hsv_to_rgb=np.vectorize(colorsys.hsv_to_rgb)

to vectorize those functions. Unfortunately, using np.vectorize results in rather slow code.

I was able to obtain roughly a 5 times speed up by translating colorsys.rgb_to_hsv and colorsys.hsv_to_rgb into native numpy operations.

import Image

import numpy as np

def rgb_to_hsv(rgb):

# Translated from source of colorsys.rgb_to_hsv

# r,g,b should be a numpy arrays with values between 0 and 255

# rgb_to_hsv returns an array of floats between 0.0 and 1.0.

rgb = rgb.astype('float')

hsv = np.zeros_like(rgb)

# in case an RGBA array was passed, just copy the A channel

hsv[..., 3:] = rgb[..., 3:]

r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]

maxc = np.max(rgb[..., :3], axis=-1)

minc = np.min(rgb[..., :3], axis=-1)

hsv[..., 2] = maxc

mask = maxc != minc

hsv[mask, 1] = (maxc - minc)[mask] / maxc[mask]

rc = np.zeros_like(r)

gc = np.zeros_like(g)

bc = np.zeros_like(b)

rc[mask] = (maxc - r)[mask] / (maxc - minc)[mask]

gc[mask] = (maxc - g)[mask] / (maxc - minc)[mask]

bc[mask] = (maxc - b)[mask] / (maxc - minc)[mask]

hsv[..., 0] = np.select(

[r == maxc, g == maxc], [bc - gc, 2.0 + rc - bc], default=4.0 + gc - rc)

hsv[..., 0] = (hsv[..., 0] / 6.0) % 1.0

return hsv

def hsv_to_rgb(hsv):

# Translated from source of colorsys.hsv_to_rgb

# h,s should be a numpy arrays with values between 0.0 and 1.0

# v should be a numpy array with values between 0.0 and 255.0

# hsv_to_rgb returns an array of uints between 0 and 255.

rgb = np.empty_like(hsv)

rgb[..., 3:] = hsv[..., 3:]

h, s, v = hsv[..., 0], hsv[..., 1], hsv[..., 2]

i = (h * 6.0).astype('uint8')

f = (h * 6.0) - i

p = v * (1.0 - s)

q = v * (1.0 - s * f)

t = v * (1.0 - s * (1.0 - f))

i = i % 6

conditions = [s == 0.0, i == 1, i == 2, i == 3, i == 4, i == 5]

rgb[..., 0] = np.select(conditions, [v, q, p, p, t, v], default=v)

rgb[..., 1] = np.select(conditions, [v, v, v, q, p, p], default=t)

rgb[..., 2] = np.select(conditions, [v, p, t, v, v, q], default=p)

return rgb.astype('uint8')

def shift_hue(arr,hout):

hsv=rgb_to_hsv(arr)

hsv[...,0]=hout

rgb=hsv_to_rgb(hsv)

return rgb

img = Image.open('tweeter.png').convert('RGBA')

arr = np.array(img)

if __name__=='__main__':

green_hue = (180-78)/360.0

red_hue = (180-180)/360.0

new_img = Image.fromarray(shift_hue(arr,red_hue), 'RGBA')

new_img.save('tweeter_red.png')

new_img = Image.fromarray(shift_hue(arr,green_hue), 'RGBA')

new_img.save('tweeter_green.png')

yields

and

python对图片颜色校正_使用Python PIL更改图像色调相关推荐

  1. python制作图片墙_利用python生成照片墙的示例代码

    PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了.其官方主页为:PIL. PIL历 ...

  2. python对图片颜色校正,使用Python PIL更改图像色调

    Using Python PIL, I'm trying to adjust the hue of a given image. I'm not very comfortable with the j ...

  3. python透明图片合并_如何使用PIL将透明png图像与另一个图像合并

    正如olt已经指出的那样,Image.paste当源和目标都包含alpha 时,将无法正常工作. 请考虑以下情形: 两个测试图像都包含alpha: layer1 = Image.open(" ...

  4. python界面显示图片更换背景_用python制作一个简陋的证件照换底色的桌面控制台应用...

    获取抠图API密钥 前往RemoveBg官网注册一个账号 注册账户界面已翻译,Api每月可用50次 注册成功后登录,按图示所选点击 点击 Get Api Key 获取Api密钥 安装所需支持库 imp ...

  5. python简单图片识别_用Python进行简单的图片识别(1)

    起因是这样的,我想买一个定焦镜头,但也不是必需品,可以长期观望购买.最初的想法是,写个程序,每天爬一下各大电商网站的价格,或者直接爬etao把该镜头的价格记录下来,突然有一个惊爆的低价,就发邮件通知. ...

  6. python代码图片头像_用 Python 把朋友头像变表情包? Easy!

    22点24分准时推送,第一时间送达 编辑:技术君 | 来源:Python学习与数据挖掘 上一篇: 正文 在日常生活中,我们经常会存取一些朋友们的丑照,在这个项目中,我们以萌萌哒的熊猫头作为背景,然后试 ...

  7. python设置图片透明度_学习python第40天

    今天是在尚学堂学习python第40天今天学习了CSS 中常用样式总结,边框的风格,css中的定位,盒模型,css3中的选择器,css3中常用的属性 CSS 中常用样式总结 字体:(font) 字体的 ...

  8. python下载图片代码_使用Python下载Bing图片(代码)

    直接上代码: # -*- coding: cp936 -*- import urllib import os print 'Download data......' url = 'http://cn. ...

  9. python改图片颜色_通过python改变图片特定区域的颜色详解

    首先让我祭出一张数学王子高斯的照片,这位印在德国马克上的神人有多牛呢? 他是近代数学的奠基人之一,与牛顿, 阿基米德并称顶级三大数学家,随便找一个编程语言的数学库,里面一定有和他名字相关的一堆函数. ...

最新文章

  1. 在IIS6上部署WebService
  2. 日产ftt传感器是什么_日产将发布最牛自动驾驶:选最棒的陪驾,走最快的车道...
  3. 【转载保存】Java丨jsoup网络爬虫登录得到cookie并带上cookie访问
  4. Redis学习总结(15)——Redis 基本数据类型使用场景
  5. jQuery琐碎笔记
  6. InstallShield安装过程介绍
  7. [转载] 【python第四天】 注释和缩进
  8. 浏览器Quirksmode(怪异模式)与CSScompat
  9. Android App 启动时立即崩溃无法debug的解决方法
  10. 怎样选择图纸加密软件?
  11. Echarts教程1_ ECharts官方教程(一)【5分钟上手ECharts】
  12. 靠卖艺还债:罗永浩的冬天来了!
  13. Intel opreation mode
  14. 金融业务-美港股和A股的区别
  15. 股神涨跌求剩余股票价格
  16. Intellij idea 报错:Error : java 不支持发行版本5
  17. 爬取绝对领域jk制服区全图片 新人笔记
  18. android webview 找不到网页,webview loadUrl 显示“找不到网页”
  19. java 最烧脑的继承题_java烧脑面试题总结
  20. Linux下PostSQL编译安装

热门文章

  1. Java连接SQL数据库失败的分析思路
  2. android中常见的回调模式
  3. Silverlight4.0教程之WebBrowser控件(Silverlight内置HTML浏览器控件)
  4. SendMessage函数的常用消息及其应用
  5. 韩国首尔公交车站将被指定为禁烟场所
  6. erp系统服务器都是维护些什么意思,erp系统维护服务器维护管理文档.doc
  7. ORA-28000: the account is locked
  8. win2003/XP删除桌面回收站
  9. go操作mysql创建多对多_Django 数据库表多对多的创建和增删改查
  10. asp判断是否移动端_asp判断用户端是电脑访问还是移动设备方法