用python将过滤器变暗到图像(8.7.10变暗过滤器代码)
我正在尝试编写一个变暗过滤器和一个更改图片功能,该功能将图像左半部分的像素修改为更暗。非常感谢你的帮助。这是我目前的代码:用python将过滤器变暗到图像(8.7.10变暗过滤器代码),python,python-imaging-library,image-manipulation,Python,Python Imaging Library,Image Manipulation,我正在尝试编写一个变暗过滤器和一个更改图片功能,该功能将图像左半部分的像素修改为更暗。非常感谢你的帮助。这是我目前的代码: # Constants for the image IMAGE_URL = "https://codehs.com/uploads/e07cd01271cac589cc9ef1bf012c6a0c" IMAGE_WIDTH = 280 IMAGE_HEIGHT = 200 DARKENING_FACTOR = 50 MIN_PIXEL_VALUE = 0
# Constants for the image
IMAGE_URL = "https://codehs.com/uploads/e07cd01271cac589cc9ef1bf012c6a0c"
IMAGE_WIDTH = 280
IMAGE_HEIGHT = 200
DARKENING_FACTOR = 50
MIN_PIXEL_VALUE = 0
image = Image(IMAGE_URL)
image.set_position(70, 70)
image.set_size(IMAGE_WIDTH, IMAGE_HEIGHT)
add(image)
# This function should take a pixel and return a tuple
# with the new color
def darken_filter(pixel):
pass
# This function should loop through each pixel on the
# left hand side and call the darken filter to calculate
# the new color then update the color.
def change_picture():
pass
# Give the image time to load
print("Making Darker....")
print("Might take a minute....")
timer.set_timeout(change_picture, 1000)
使用枕头将图像分割成两半 来自ImageEnhance模块的使用,该模块包含许多可用于图像增强的类 类PIL.ImageEnhanced.Brightness(图像): '调整图像亮度 此类可用于控制图像的亮度。增强因子为0.0时会显示黑色图像。系数为1.0表示原始图像。” 重新组合您的图像 无法真正解决一半大小的图像可能最好将您的问题分成两个问题,但我使用PIL.ImageEnhance.Brightness(image),image.point()尝试了下面假定的图像代码的变暗过滤部分,如web上所示,并尝试使用您的方法,我相信在第三种方法中有一种更直接的方法,但我也是一个新手,可能遗漏了一些东西:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 6 16:38:32 2021
@author: Pietro
"""
from PIL import Image, ImageEnhance
import numpy as np
image_input = Image.open('Immagine_6.tif')
image_input.show()
print(image_input.size)
print(image_input.mode)
image_input = image_input.convert('RGB')
# method one from https://www.daniweb.com/programming/software-development/code/216419/darken-lighten-an-image-python
# Image.point() : https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=image.point#PIL.Image.Image.point
image_output =image_input.point(lambda p: p * 0.5)
image_output.show()
# method two using ImageEnhance module of pillow
# https://pillow.readthedocs.io/en/3.0.x/reference/ImageEnhance.html#PIL.ImageEnhance.Brightness
enhancer = ImageEnhance.Brightness(image_input)
image_output_2 = enhancer.enhance(0.1)
image_output_2.show()
# method three your method sure there is a better way than this one
# This function should take a pixel and return a tuple
# with the new color
def darken_filter(pixel):
pixel = (0.3*np.array(pixel)).astype('int')
pixel = tuple(pixel[0:3])
return pixel
image_input_dark = image_input.copy()
for x in range(image_input_dark.size[0]):
for y in range(image_input_dark.size[1]):
i = image_input_dark.getpixel((x,y))
ii = darken_filter(i)
image_input_dark.putpixel((x,y),ii)
image_input_dark.show()
样本图像
在这个社区中,我们不为您做这些工作,我们帮助您解决问题,所以您是否在两个功能中尝试了一些东西,或者只是问了一下?问题是什么?你说“函数应该取一个像素”,但你没有给我们定义“像素”是什么(例如:它是十六进制RGB值吗?是整数吗?是数组吗?是元组吗?)。您知道如何使用
For
循环检查像素值吗?