在Python中将图像处理为棕褐色调

时间:2016-04-05 19:02:41

标签: python python-3.x image-processing

我需要帮助弄清楚如何将图像转换为棕褐色。这就是我到目前为止所做的事情。但它只会改变一切为黑色和白色的颜色,并带有非常小的棕色。我不确定我做错了什么:(

import image

def convertSepia(input_image):
    grayscale_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())

    for col in range(input_image.getWidth()):
        for row in range(input_image.getHeight()):
            p = input_image.getPixel(col, row)

            R = p.getRed()
            G = p.getGreen()
            B = p.getBlue()

            newR = (R * 0.393 + G * 0.769 + B * 0.189)
            newG = (R * 0.349 + G * 0.686 + B * 0.168)
            newB = (R * 0.272 + G * 0.534 + B * 0.131)

            newpixel = image.Pixel(newR, newG, newB)
            grayscale_image.setPixel(col, row, newpixel)

    sepia_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())
    for col in range(input_image.getWidth()):
        for row in range(input_image.getHeight()):
            p = grayscale_image.getPixel(col, row)
            red = p.getRed()
            if red > 140:
                val = (R * 0.393 + G * 0.769 + B * 0.189)
            else:
                val = 0
            green = p.getGreen()
            if green > 140:
                val = (R * 0.349 + G * 0.686 + B * 0.168)
            else:
                val = 0
            blue = p.getBlue()
            if blue > 140:
                val = (R * 0.272 + G * 0.534 + B * 0.131)
            else:
                val = 0

            newpixel = image.Pixel(val, val, val)
            sepia_image.setPixel(col, row, newpixel)
    return sepia_image


win = image.ImageWin() img = image.Image("luther.jpg")

sepia_img = convertSepia(img) sepia_img.draw(win)

win.exitonclick()

有关从何处开始的更多提示?谢谢:))

3 个答案:

答案 0 :(得分:1)

您的灰度图像不是灰度图像。在灰度图像中,所有三个频道rgb都具有相同的值。

打开画图并尝试验证您的代码是否有意义。

修正这些行:

newR = (R * 0.393 + G * 0.769 + B * 0.189)
newG = (R * 0.349 + G * 0.686 + B * 0.168)
newB = (R * 0.272 + G * 0.534 + B * 0.131)

只需使用rgb的平均值,并将其放入newRnewGnewG

也有一些加权手段。只是谷歌的RGB强度公式。

答案 1 :(得分:0)

您可以通过操纵像素值将图像转换为棕褐色。以下是代码(免责声明:摘自this文章。)

from PIL import Image

def sepia(image_path:str)->Image:
    img = Image.open(image_path)
    width, height = img.size

    pixels = img.load() # create the pixel map

    for py in range(height):
        for px in range(width):
            r, g, b = img.getpixel((px, py))

            tr = int(0.393 * r + 0.769 * g + 0.189 * b)
            tg = int(0.349 * r + 0.686 * g + 0.168 * b)
            tb = int(0.272 * r + 0.534 * g + 0.131 * b)

            if tr > 255:
                tr = 255

            if tg > 255:
                tg = 255

            if tb > 255:
                tb = 255

            pixels[px, py] = (tr,tg,tb)

    return img

原始图片enter image description here

棕褐色图片enter image description here

答案 2 :(得分:0)

这是我的解决方案,不需要在棕褐色之前转换为灰度。我使用给定的棕褐色公式:

newR = (R × 0.393 + G × 0.769 + B × 0.189)
newG = (R × 0.349 + G × 0.686 + B × 0.168)
newB = (R × 0.272 + G × 0.534 + B × 0.131)

完整代码:

import image

img= image.Image("luther.jpg")
win=image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1,100)

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p=img.getPixel(col,row)
        R= p.getRed()
        G= p.getGreen()
        B= p.getBlue()
        newR = 0.393*R + 0.769*G + 0.189*B
        newG = 0.349*R + 0.686*G + 0.168*B
        newB = 0.272*R + 0.534*G + 0.131*B
        newpixel= image.Pixel(newR,newG,newB)
        img.setPixel(col, row, newpixel)
        
img.draw(win)
win.exitonclick()