没有内置功能的情况下,PIL中的图像变亮或变暗 [英] Brightening or darkening images in PIL without built in functions

查看:251
本文介绍了没有内置功能的情况下,PIL中的图像变亮或变暗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个uni项目中使用PIL,我们有一项任务是必须使图像变暗或变亮,而无需使用PIL的任何功能.该函数采用原始文件名,操作(变亮"或变暗")和范围(以百分比表示-0到100之间的整数).到目前为止,这是我要提出的:

from PIL import Image

def change_brightness(filename, action, extent):
    """
    This function either increases or decreases the brightness of an image
    by altering each pixel in each band

    """
    #load the original image into a list
    original_image = Image.open(filename, 'r')
    pixels = original_image.getdata()

    #initialise the new image
    new_image = Image.new('RGB', original_image.size)
    new_image_list = []

    brightness_multiplier = 1.0

    if action == 'lighten':
        brightness_multiplier += (extent/100)
    else:
        brightness_multiplier -= (extent/100)

    #for each pixel, append the brightened or darkened version to the new image list
    for pixel in pixels:
        new_pixel = (int(pixel[0] * brightness_multiplier),
                     int(pixel[1] * brightness_multiplier),
                     int(pixel[2] * brightness_multiplier))

        #check the new pixel values are within rgb range
        for pixel in new_pixel:
            if pixel > 255:
                pixel = 255
            elif pixel < 0:
                pixel = 0

        new_image_list.append(new_pixel)

    #save the new image
    new_image.putdata(new_image_list)
    new_image.save('colour_brightness.jpg')

当我运行此命令时,新图像不会从原始图像中修改(从一些新的jpg伪像中保存).我尝试使用显式值(变亮为1.1,变暗为0.9)对brightness_multiplier进行了测试,因此它不起作用,所以我不知道为什么当我从extent参数中获取该值时为什么它不起作用.

如果有人可以阐明一点,将不胜感激!

解决方案

这是(extent/100)表达式中整数除法的问题.要解决此问题,您可以:

使用浮点文字

extent/100.0

如果术语是文字,则很方便.

将分子或分母转换为浮点数

float(extent)/a_hundred

如果没有术语是文字.

确保/是浮点除法

from __future__ import division

将其插入源文件的开头,就像所有__future__语句一样.

使用-Qnew调用python

python -Qnew

如果使用python2,否则

使用python3:)

在所有情况下,//仍为整数除.

I'm using PIL for a uni project and we have one task where we have to darken or brighten an image without using any of PIL's functions to do so. The function takes the original filename, the action ('lighten' or 'darken') and the extent (in percent - an int between 0 and 100). Here's what I've come up with so far:

from PIL import Image

def change_brightness(filename, action, extent):
    """
    This function either increases or decreases the brightness of an image
    by altering each pixel in each band

    """
    #load the original image into a list
    original_image = Image.open(filename, 'r')
    pixels = original_image.getdata()

    #initialise the new image
    new_image = Image.new('RGB', original_image.size)
    new_image_list = []

    brightness_multiplier = 1.0

    if action == 'lighten':
        brightness_multiplier += (extent/100)
    else:
        brightness_multiplier -= (extent/100)

    #for each pixel, append the brightened or darkened version to the new image list
    for pixel in pixels:
        new_pixel = (int(pixel[0] * brightness_multiplier),
                     int(pixel[1] * brightness_multiplier),
                     int(pixel[2] * brightness_multiplier))

        #check the new pixel values are within rgb range
        for pixel in new_pixel:
            if pixel > 255:
                pixel = 255
            elif pixel < 0:
                pixel = 0

        new_image_list.append(new_pixel)

    #save the new image
    new_image.putdata(new_image_list)
    new_image.save('colour_brightness.jpg')

When I run this, the new image is not modified from the original (save from some new jpg artefacts). I tried brightness_multiplier with an explicit value (1.1 for lighten, and 0.9 for darken) and it worked, so I have no idea why it isn't working when I have it taking the value from the extent argument.

If anyone could shed some light it would be greatly appreciated!

解决方案

It's an issue of integer division in the (extent/100) expression. To rectify this, you can:

use a floating point literal

extent/100.0

Handy if a term is a literal.

convert either the numerator or the denominator into float

float(extent)/a_hundred

If no term is a literal.

make sure that / is floating-point division

from __future__ import division

Insert that at the beginning of your source file, like all __future__ statements.

invoke python with -Qnew

python -Qnew

If using python2, otherwise

use python3 :)

In all cases, // remains integer division.

这篇关于没有内置功能的情况下,PIL中的图像变亮或变暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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