Convert .bmp to .png using Python and PIL

    In a previous post, I used the standard "PrtSc" on Windows XP, making a series of screenshots, processing and saving them in regular Paint, we got about 3.5 MB per image.

    Here I want to correct the resulting misunderstanding with python (as before, version 2.6).

    Generally funny, but after installing PIL, the problem is solved in two lines of code.
    import Image

    Image.open('1.bmp').save('1.png')

    It turned out about 27 Kb per picture, against the first 3.5 Mb


    We were glad and wrote the code for mass conversion from an existing folder.
    import osImage

    os.chdir('F:\\4habr\\1publication') # поменяем директорию на ту, где у нас расположены картинки

    for fname in os.listdir(os.getcwd()): # os.listdir - соответственно, есть ли что-нибудь, у нас, в папке,
        try:
            Image.open(fname).save(os.path.splitext(fname)+'.png') # а os.getcwd() - папка, в которую мы однажды перешли
        except DefaultError: 
            print('Sorry, we have no pictures.')

    And yet, who is interested, to simplify the mass conversion procedure, you can use this script , which in turn uses Tkinter .

    If you have ideas on how to improve the code, I will gladly accept them.

    Also popular now: