替换照片的所有颜色,但现有黑白像素除外 [英] Replace all the colors of a photo except from the existing black and white pixels PYTHON

查看:81
本文介绍了替换照片的所有颜色,但现有黑白像素除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者,我想通过一种方法将照片的所有像素更改为白色,但照片中已经存在的白色或黑色像素除外.我尝试使用PIL,但找不到. 谢谢!

I'm a beginner in python and i would like the way to change all of the pixels of a photo to white, except the pixels that white or black that were already in the photo. I tried to use PIL but I couldn't find it. Thanks!

推荐答案

我想将照片的所有像素更改为白色的方法,除了照片中已经存在的白色或黑色像素.

i would like the way to change all of the pixels of a photo to white, except the pixels that white or black that were already in the photo.

因此,基本上,您想将除黑色以外的所有像素都更改为白色,对吧?如果是这样,则可以进行以下工作(注意:它期望在您的计算机上安装了cv2 lib)

So basically you want to change all pixels to white except black, right? If so, then below work (note: it expect cv2lib is installed on your machine)

import cv2
import numpy as np

img = cv2.imread('my_img.jpeg')
img[img != 0] = 255 # change everything to white where pixel is not black
cv2.imwrite('my_img2.jpeg', img)

这篇关于替换照片的所有颜色,但现有黑白像素除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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