Home>

I intend to write a program that reads an image and adjusts the gamma value. I'm looping because I want to change the gamma from 0.6 to 0.9. However, when it is 0.6, it is executed, but after 0.7, it does not loop and an error occurs.
When I loaded the image and fixed the gamma value without looping, it worked, but after looping it, I couldn't. Thank you.

Since I am a beginner in the program, I think there are some points that I cannot reach, but thank you.

0.6
0.7
Traceback (most recent call last):
  File "d:/~~~ /change.py", line 40, in<module>
    im_1 = 255.0 * (picture/255.0) ** (1/gamma)
TypeError: unsupported operand type (s) for /:'Image' and'float'
Corresponding source code
import cv2
import numpy as np
import glob
import os.path
import os
from PIL import Image
import matplotlib.pyplot as plt
(Path name omitted)
f = open (new_annotation_txt, mode ='w')
f.close ()
with open (picture_name_pass, encoding ='utf-8!') As picture_name_data:
    i = 0
    for line in picture_name_data:
        picture_pass0 = line.replace ('\ n','')
        # picture_pass1 = picture_pass0 + ".jpg"
        picture = np.array (Image.open (picture_pass0), dtype ='float16')
        #Gamma correction
        gamma = 6/10
        while gamma<1.0:<-Problem
            print (gamma)
            im_1 = 255.0 * (picture/255.0) ** (1/gamma)
            im_2 = 255.0 * (picture/255.0) ** gamma
            picture = Image.fromarray (np.uint8 (im_1))
            #save_pass = picture_pass0 + ".jpg"
            save_pass = savepoint_pass + str (gamma) + "/"
+ Picture_pass0.replace (picture_pass,'')
            picture.save (save_pass)
            picture = Image.fromarray (np.uint8 (im_2))
            save_pass = picture_pass0 + ".jpg"
            save_pass = savepoint_pass + "1_" + str (gamma) + "/"
+ Picture_pass0.replace (picture_pass,'')
            picture.save (save_pass)
            gamma + = 1/10
        i + = 1
        print (i)
        picture.close ()
  • Answer # 1

    The picture before entering while is np.array, but it may be because the picture is updated with Image.fromarray in while and becomes an Image object.

  • Answer # 2

    Excerpt from the problem

    gamma_list = [0.6,0.7,0.8,0.9] When<-gamma + = 0.1, it became 0.7999999, so for the time being
    num = 0
    while num pic = picture
    <-If the picture is rewritten, it cannot be used even if the original image is looped, so it is assigned to another variable.
    gamma = gamma_list [num]
    print (gamma)
    im_1 = 255.0 * (pic/255.0)(1/float (format (gamma,'.1f'))) im_2 = 255.0 * (pic/255.0)float (format (gamma,'.1 f'))
    pic = Image.fromarray (np.uint8 (im_1))
    save_pass = savepoint_pass + str (format (gamma,'.1f')) + "/"
    + picture_pass0.replace (picture_pass,'')
    pic.save (save_pass)