3

Am just having four weeks of experience in Python. Creating a tool using Tkinter to paste a new company logo on top of the existing images.

The Below method is to, get all images in the given directory and paste the new logo on the initial level. Existing image, edited image, x-position, y-position, a preview of the image and few data's are store in global instance self.images_all_arr.

def get_img_copy(self):
    self.images_all_arr = []
    existing_img_fldr = self.input_frame.input_frame_data['existing_img_folder']
    for file in os.listdir(existing_img_fldr):
        img_old = Image.open(os.path.join(existing_img_fldr, file))
        img_new_copy = img_old.copy()
        self.pasteImage(img_new_copy, initpaste=True) #process to paste new logo.
        view_new_img = ImageTk.PhotoImage(img_new_copy)
        fname, fext = file.split('.')
        formObj = {
            "fname": fname,
            "fext": fext,
            "img_old": img_old,
            "img_new": img_new_copy,
            "img_new_view": view_new_img,
            "add_logo": 1,
            "is_default": 1,
            "is_opacityImg": 0,
            "pos_x": self.defult_x.get(),
            "pos_y": self.defult_y.get()
        }
        self.images_all_arr.append(formObj)

After previewing each image in Tkinter screen, doing some adjustment in position x and y(updating pos_x and pos_y in the list self.images_all_arr) depends upon the necessity.

Well, once all done. Need to save the edited images. Below method to save images, iterating the list self.images_all_arr and call save method as img['img_new'].save(dir_output) since img['img_new'] has updated image.

 def generate_imgae(self):
    if len(self.images_all_arr):
        dir_output = 'xxxxx'
        for img in self.images_all_arr:
            print(img['img_new'])
            img['img_new'].save(dir_output)

        print('completed..')

But it returns below error,

Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py", line 2138, in save format = EXTENSION[ext] KeyError: '' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\tkinter_init_.py", line 1883, in call return self.func(*args) File "C:\Users\662828\WORKSPACE\AAA_python_projects\AMI_automation_poc2\position_and_images.py", line 241, in generate_imgae img['img_new'].save(dir_output) File "C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py", line 2140, in save raise ValueError("unknown file extension: {}".format(ext)) from e ValueError: unknown file extension:

0

2 Answers 2

9

dir_output doesn't contain the file extension, its just xxxxx. You need to specify what image file format you want. the error tells us this by saying "unknown file format".

Basically, you either need to include the extension in the file name, or pass it as the next parameter in image.save. You can check out the documentation here

eg.

image.save('image.png')

or

image.save('image', 'png')

0

The below code solved my issue. By giving the exact directory, filename and extension of the image as param to the method image.save(). Here the result of opfile is, C:\Users\WORKSPACE\AAA_python_projects\test\valid.png.

def generate_imgae(self):
    if len(self.images_all_arr):
        dir_output = r"C:\Users\WORKSPACE\AAA_python_projects\test"
        if not os.path.isdir(dir_output):
            os.mkdir(dir_output)
        for img in self.images_all_arr:
            opfile = os.path.join(dir_output, '{}.{}'.format(img['fname'], img['fext'] ))
            img['img_new'].save(opfile)
        print('completed..')

Thanks @dantechguy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.