I want to allow the user to adjust an image's contrast by a slider in my kivy app. Because I want to modify and update the current image real time. I am struggling to implement in Kivy.
From researching the problem, I think I should be using StringIO and / or ImageLoaderPyGame (these resources listed at the bottom of this post). The problem is I am confused about how to tie this to my kv file, and how to properly implement the StringIO and ImageLoaderPyGame with my current image. Some pseudo code describing what I'm trying to do is:
Slider :
id: contrast
value: 100
min: 0
max: 199
on_value: root.update_contrast(contrast.value)
Image :
id: displayed_image
source: root.display_path
Python Code:
def update_contrast(self, value):
buff = StringIO(self.display_image)
buff.seek(0)
img = ImageLoaderPygame(buff)
self.display_image = self.change_contrast(img, value)
Which gives the following error:
buff = StringIO(self.display_image)
TypeError: initial_value must be str or None, not numpy.ndarray
Which I understand means I need to initiate the StringIO object with either None or a string, but I am confused about how to incorporate the current display image (which is defined through a file path variable - which I also cannot use to instantiate the StringIO, I get the still understandable error- "path should be string, bytes, os.PathLike or integer, not _io.StringIO")
Summary Currently, I have a display image object, defined through a user specified file path (which the kv image tag needs to display the image). I do not know how to allow the user to efficiently adjust the contrast of the display image. I think I need to use StringIO and ImageLoaderPyGame, but I do not see how to tie a file path and StringIO/ImageLoaderPyGame together.
Note: change_contrast is similar to the method suggested here: Change contrast of image in PIL
def change_contrast(img, level):
factor = (259 * (level + 255)) / (255 * (259 - level))
def contrast(c):
return 128 + factor * (c - 128)
return img.point(contrast)
Tried