Concept

Reading and writing images

To identify image files, use the following snippet:

import sys from PIL import Image for infile in sys.argv[1:]: try: with Image.open(infile) as im: print (infile, im.format, f"{im.size}x{im.mode}") except OSError: pass

To convert file sto JPEG, use the following snippet:

import os, sys from PIL import Image for infile in sys.argv[1:]: f, e = os.path.splitext(infile) outfile = f + ".jpg" if infile != outfile: try: with Image.open(infile) as im: im.save(outfile) except OSError: print("cannot convert", infile)

0

1

Updated 2021-07-13

Tags

Python Programming Language

Data Science