我正在使用numpy在python中创建所有图像转换的简单图像编辑器。我遇到了亮度更改的问题-当我将亮度更改为-255或255时,我(显然)丢失了所有信息,并且将亮度更改回0并没有给我原始图像。我了解这种行为,但是如果我想获取原始图像怎么办?
我的解决方案是使用默认亮度保留图像的副本,但是这有点无效,因为现在我要更改两个图像-image_data
和image_data_no_brightness
进行除亮度更改(旋转(旋转)之外的其他所有变换) ,镜像,否定等)。
有没有更有效的解决方案?
import numpy as np
from matplotlib.pyplot import imshow, imsave, imread
from PIL import Image
MIRROR_VERTICAL = 0
MIRROR_HORIZONTAL = 1
class ImageEditor:
def __init__(self):
self.image_data = None
self.image_data_og = None
self.image_data_no_brightness = None
def load_data(self, image_path):
# todo fix imread and imwrite to be compatible with png images
self.image_data = np.asarray(Image.open(image_path).convert('RGB'))
self.image_data.setflags(write=True)
self.image_data_og = self.image_data.copy()
self.image_data_no_brightness = self.image_data.copy()
def get_data(self):
return self.image_data
def save_image(self, image_path):
imsave(image_path, self.image_data)
def show(self):
imshow(self.image_data, cmap='gray')
def reset(self):
self.image_data = self.image_data_og.copy()
self.image_data_no_brightness = self.image_data_og.copy()
def rotate(self, angle):
assert angle % 90 == 0 # GUI shouldn't let this happen
self.image_data = np.rot90(self.image_data, k=angle / 90)
self.image_data_no_brightness = np.rot90(self.image_data_no_brightness, k=angle / 90)
def mirror(self, axis=MIRROR_HORIZONTAL):
self.image_data = np.flip(self.image_data, axis=axis)
self.image_data_no_brightness = np.flip(self.image_data_no_brightness, axis=axis)
def negative(self):
self.image_data = 255 - self.image_data
self.image_data_no_brightness = 255 - self.image_data_no_brightness
def brightness(self, change):
# convert to int16 as uint8 values could overflow
self.image_data = np.clip(np.int16(self.image_data_no_brightness) + change, 0, 255)
# convert back to uint8
self.image_data = np.uint8(self.image_data)