📜 ⬆️ ⬇️

Convert .bmp to .png using Python and PIL

In the previous post I used the standard “PrtSc”, on Windows XP, making a series of screenshots, processing and saving them in ordinary Paint — it turned out about 3.5 MB for each picture.

Here I want to correct the resulting misunderstanding, the means of python (still, 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 image, against the first 3.5 MB

Glad, and wrote the code for mass conversion from an existing folder.
import os , Image

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 also, to whom it is interesting, to simplify the bulk 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.


Source: https://habr.com/ru/post/102463/

All Articles