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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428148.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自动生成业务单据流水号方案

我们在开发管理软件的时候,常常遇到流水号(单据号、登记号)自动生成、控制和管理的问题。由于流水号具有唯一性和连续性的特点,在实际开发过程中若处理不好,会产生流水号重复及断号的问题。特别是多个并发用户同时保存一张同样的业务单据时,系统会返回多个相同的流水…

IDEA快捷键的使用成就手速之旅(要想手速变得快,快捷练习必须刚)

IDEA快捷键的使用 (持续更新) 1.必备合集 a.Ctrl CtrlF 文本代码查找神器呀简直CrtlR 文本代码替换CtrlZ 撤销(基操基操)CtrlY 删除当前行或者选中行 b.Shirt c.CtrlShirt d.Ctrlalt e.altShirt f.CtrlShirtalt 2.慢慢积…

一个项目部署多个节点会导致锁失效么_Redis分布式锁

分布式锁在很多场景中是非常有用的原语, 不同的进程必须以独占资源的方式实现资源共享就是一个典型的例子。有很多分布式锁的库和描述怎么实现分布式锁管理器(DLM)的博客,但是每个库的实现方式都不太一样,很多库的实现方式为了简单降低了可靠性&#xff…

GIT_服务器与本地环境构建

linux安装git包 很多yum源上自动安装的git版本为1.7,这里手动编译重新安装1:安装依赖包yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker2:删除已有的gityum remove git3&#xff1a…

Maven项目的基本创建步骤

先来个自己笔记的图片备忘一下,如果以后有时间再慢慢更新详细。

visualvm远程监控jvm_大型企业JVM实战:优化及面试热点分析

本次课程的笔记非常多,而且内容已经整理了好几个小时了,接着下来内容也会更多,也是大型企业JVM性能调优实战的最后一节,希望对你有帮助!04:JVM性能监控与故障处理工具 大型企业JVM性能调优实战之总结17&…

Markdown图片路径的改变方法

Markdown图片路径的改变方法 Markdown用时一时爽,路径一改火葬场 Markdown在占用内存少的优点的同时,也注定了图片的存储不会是占用内存,而是根据路径和链接链到md文件里的。 相信有不少人会像我一样在做完一个Markdown笔记后,在…

SROP

title: SROP date: 2018-02-21 19:58:12 categories: 栈溢出 tags: - CTF - PWN - 栈溢出 SROP全称为 Sigreturn Oriented Programming ,表明利用sigreturn这个函数实现ROP的技术。 参考资料 http://www.freebuf.com/articles/network/87447.htmlhttp://bobao.360.c…

python字符串的方法和列表的方法_Python学习笔记字符串操作之join()和split()方法,列表转字符串,字符串转列表...

随笔记录方便自己和同路人查阅。#------------------------------------------------我是可耻的分割线-------------------------------------------如果有一个字符串列表,需要将它们连接起来,成为一个单独的字符串,join()方法就很有用。join…

变量的比较之equals 与 == 的区别

Java的数据类型分为两种 1.基本数据类型,byte,short,char,int,long,float,double,boolean,只要使用运算符就可以了,进行比较只是简单进行比较其中的字节组合。 两个引用变量是否引用到堆上的同一个对象,也可以使用。 2.复杂的对象…

scheduledthreadpoolexecutor使用_ScheduledThreadPoolExecutor详解

本文主要分为两个部分,第一部分首先会对ScheduledThreadPoolExecutor进行简单的介绍,并且会介绍其主要API的使用方式,然后介绍了其使用时的注意点,第二部分则主要对ScheduledThreadPoolExecutor的实现细节进行介绍。1. 使用简介Sc…

SpringContextHolder 静态持有SpringContext的引用

SpringContextHolder 静态持有SpringContext的引用 package com.test.quartz;import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;/**** 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何…

canvas绘制图像image

canvas绘制图像image 1.image的三个script的基本语法 准备工作:1.定义画布长度,获取2D绘图环境 ​ 2.建立对面对象,设置图片路径 ​ 3.载入图片,开始绘制 a.简单的画布上根据坐标绘制 ctx.drawImage(img,x,y) img为要绘制的图像&#…

根据时间戳生成编号_分布式系统的唯一ID生成算法对比

在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识。那么如何实现全局唯一id呢?有以下几种方案。(1)方案一:独立数据库自增id这个方案就是说你的系统每次要生成一个id,都是往一个独立库的一个独立表里插入一条没什么业务…

Ubuntu上安装Samba服务器实现家庭共享

如何在Ubuntu上安装Samba服务器 大多数Linux发行版都包含Samba。 要在Ubuntu上安装Samba,只需运行: sudo apt install samba 要检查您的Samba版本,请运行 sudo smbstatus 或者 sudo smbd --version 输出格式如下: Samba version …

解决使用Servlet输出乱码问题(一行代码解决一切)

使用servlet接收表单数据后,输出出现了乱码,如下: 解决方法: 将下面这行代码粘到你重写的doGet或者doPost方法里就可以了。 response.setContentType("text/html;charsetutf-8");

利用一维数组求菲波那契数列前40项的和并输出结果。_[W2D2]斐波那契数列

题目链接:斐波那契数列 - 题目 - 青藤 OJ题目来源:经典题题目大意输入 ,输出 0,1 开头的斐波那契数列的第 n 项。这里我们不讨论递推方法,我们采用这道简单的题目简单说一下记忆化搜索相关内容。解法首先,基…

Python网络编程(1)-socket

我会在近期尽快更新好之前写的博客,会添加新的知识点和注意问题,排版和内容都会较之前有很大的改观,感谢大家一直的支持! 1、 客户端/服务器架构 客户端/服务器架构也称主从式架构,简称C/S架构,它是一种网络…

Canvas之进度条的制作(矩形,圆环)

Canvas之进度条的绘制 基本进度条的绘制 1.矩形进度条 关键语法 获取画笔 var ctxdocument.getElementById(“id”).getContext(“2d”); 填充颜色 ctx.fillStytle“color”; setInternal()和clearInternal()的使用 代码(两种类型): <!DOCTYPE html> <html>…

该文件没有与之关联的程序来执行该操作_Liunx tty子系统分析之三 tty字符设备文件操作接口说明...

本章主要介绍tty字符设备文件对应的操作接口&#xff0c;从而说明tty设备的数据打开、关闭、读、写等接口的实现等内容。tyy file_operations定义tty字符设备文件操作接口的定义如下&#xff0c;主要包括tty_fops、console_fops、hung_up_tty_fops&#xff0c;其中console_fops…