Search code examples
pythonpython-imaging-librarypython-3.7syspython-os

Using Pillow (PIL) Library, I am trying to take images from a folder, convert all of them from JPG to PNG but it does not work in the command prompt


The expected output is → If the folder is there, convert the images from JPG to PNG and save it in that. If it is not there, make a folder and save it there.

When I run it through the command prompt, it should print("All done") according to the number of pictures in the folder that we have the JPGs.

If not I should get an error in that as well. But nothing comes. The error comes only in PyCharm. Please refer the attached screenshot for reference]1

import os
from PIL import Image

ImageFolder = sys.argv[1]
OutputFolder = sys.argv[2]

if not os.path.exists(OutputFolder):
    os.makedirs(OutputFolder)

    for filename in os.listdir(ImageFolder):
        img = Image.open(f'{ImageFolder}{filename}')
        CleanName = os.path.splitext(filename)[0]
        img.save(f'{OutputFolder}{CleanName}.png', 'png')
        print("all done")

That is my code in PyCharm

Microsoft Windows [Version 10.0.18363.836]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\SharangaF>cd C:\Users\SharangaF\Desktop\Jezon Fernando\004 Tutorials\004 Programming\002 Python\003 Tutorial Practice\005 High level Projects\001 Image Processing

C:\Users\SharangaF\Desktop\Jezon Fernando\004 Tutorials\004 Programming\002 Python\003 Tutorial Practice\005 High level Projects\001 Image Processing>python3 JPGtoPNG.py Pokedex\ New\

C:\Users\SharangaF\Desktop\Jezon Fernando\004 Tutorials\004 Programming\002 Python\003 Tutorial Practice\005 High level Projects\001 Image Processing>

This is the commands that I typed in the command prompt.


Solution

  • f'{ImageFolder}{filename}' is joining 2 texts using a '.'

    use os.path.join(ImageFolder,filename) instead

    and also replace f'{OutputFolder}{CleanName}.png' with os.path.join(OutputFolder,CleanName+".png")