简体   繁体   English

在Python中将所有RGB像素的亮度降低20%?

[英]Lower the brightness of all RGB pixels by 20% in Python?

I have been trying to teach myself more advanced methods in Python but can't seem to find anything similar to this problem to base my code off of. 我一直在尝试用Python教自己一些更高级的方法,但是似乎找不到与该问题类似的东西作为我的代码的基础。

First question: Is this only way to display an image in the terminal to install Pillow? 第一个问题:这是在终端中显示图像以安装Pillow的唯一方法吗? I would prefer not to, as I'm trying to then teach what I learn to a very beginner student. 我宁愿不这样做,因为我正尝试向初学者学习所学内容。 My image.show() function doesn't do anything. 我的image.show()函数什么都不做。

Second question: What is the best way to go about lowering the brightness of all RGB pixels in an image by 20%? 第二个问题:将图像中所有RGB像素的亮度降低20%的最佳方法是什么? What I have below doesn't do anything to the alter the brightness, but it also can compile completely. 我下面的内容对更改亮度没有任何作用,但也可以完全编译。 I would prefer the most simple way to go about this as far as importing minimal libraries. 就导入最少的库而言,我更喜欢采用最简单的方法。

Third Question: How do I made a new picture instead of changing the original? 第三个问题:如何制作新图片而不是更改原始图片? (IE- lower brightness 20%, "image-decreasedBrightness.jpg" is created from "image.jpg") (即从“ image.jpg”创建较低的亮度20%,即“ image-decreasedBrightness.jpg”)

here is my code - sorry it isn't formatted correctly. 这是我的代码-很抱歉,格式不正确。 Every time i tried to indent it would tab down to the tags bar. 每次我尝试缩进时,都将跳至标签栏。

import Image
import ImageEnhance

fileToBeOpened = raw_input("What is the file name? Include file type.")
image = Image.open(fileToBeOpened)

def decreaseBrightness(image):
    image.show()
    image = image.convert('L')
    brightness = ImageEnhance.Brightness(image) 
    image = brightness.enhance(20) 
    image.show() 
    return image

decreaseBrightness(image)

To save the image as a file, there's an example on the documentation: 要将图像另存为文件,文档中有一个示例:

from PIL import ImageFile

fp = open("lena.pgm", "rb")

p = ImageFile.Parser()

while 1:
    s = fp.read(1024)
    if not s:
        break
    p.feed(s)

im = p.close()

im.save("copy.jpg")

The key function is im.save . 关键功能是im.save

For a more in-depth solution, get a nice beverage, find a comfortable place to sit and enjoy your read: Pillow 3.4.x Documentation . 要获得更深入的解决方案,请获取优质的饮料,找到一个舒适的地方坐下来享受阅读的乐趣: 枕头3.4.x文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM