Importing a tuple to a list from a function

Issue

I’m trying to modify pixels using range in a loop but I can’t import the range from size function.

    load = bnw.load()
    loadpx = [
        (a),
        (b)
    ]
    bnw.size(loadpx)
    print(loadpx)
    for x in a:
        for y in b:
            new = load[x,y] + 10
            if new > 254:
                new = 254

            bnw = new

The output from bnw.size should be the number of pixels the image has ex. (1920,1080). after knowing the size a is inputted to the x and b to y.

the full code withe the first answer returned the same error

import tkinter as tk
import random
import os
import time
from tkinter import filedialog
from PIL import Image
import numpy as np  


main_window = tk.Tk()
main_window.title("foto epic")
judul = tk.Label(main_window, text="                Editor Instagram epic 2 BnW Edition wkwk                \n\n Silahkan Input foto Dibawah\n\/\n")
judul.pack()

def UploadAction():
    file = filedialog.askopenfilename()
    print('Selected:', file)
    img = Image.open(file)
    bnw2 = img.convert(mode = "L")
    print(bnw2.size)
    load = bnw2.load()
    r,c = bnw2.size()
    for x in range(r):
        for y in range(c):
            new = load[x,y] + 10
            if new > 254:
                new = 254

            new = bnw2
    arraying = np.asarray(bnw2)
    counting = np.bincount(arraying.flatten(), minlength = 128)
    px_num = np.sum(counting)

    counting = counting/px_num
    sum1 = np.cumsum(counting)
    floorcount = np.floor(255 * sum1).astype(np.uint8)
    donecount = list(arraying.flatten())

    transforming = [floorcount[p] for p in donecount]
    transforming = np.reshape(np.asarray(transforming), arraying.shape)
    done_transform = Image.fromarray(transforming)
    done = tk.Label(main_window, text=" \nFoto Telah di export!\nTerimakasih Sudah Menggunakan Program ini!\n")
    done.pack()
    done_transform.show()
    if os.path.exists('result.jpg'):
        done_transform.save('result_{}.jpg'.format(int(time.time())))
    else:
        done_transform.save('result.jpg')

button = tk.Button(main_window, text='Beautify!', command=UploadAction)
button.pack()

tk.mainloop()

I use Tkinter for UI I don’t know if it would affect the original question

the error

Selected: D:/Code/Kuliah/Semester 3/citra/yes/DSC_3044.jpg
(4608, 1975) < the resolution if printed
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "d:\Code\Kuliah\Semester 3\citra\yes\Tugas Pengolahan Citra.py", line 22, in UploadAction
    r,c = bnw2.size()
TypeError: 'tuple' object is not callable

Solution

Here i edited the answer this should work now:

file = filedialog.askopenfilename()
print('Selected:', file)
img = Image.open(file)
bnw2 = img.convert(mode = "L")
load = bnw2.load()
r,c = bnw2.size
for x in range(r):
    for y in range(c):
        new = load[x,y] + 10
        if new > 254:
            new = 254

        load[x,y] = new

Answered By – Bemwa Malak

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.