bensimmo

Representing Data with Images and Sound 2.5

Jan 21st, 2021 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. # https://pillow.readthedocs.io/en/stable/handbook/tutorial.html
  2. # https://www.codespeedy.com/how-to-iterate-over-files-in-a-given-directory-in-python/
  3. # https://stackoverflow.com/questions/1996577/how-can-i-get-the-depth-of-a-jpg-file
  4.  
  5. import os
  6. from PIL import Image
  7.  
  8.   # folder|directory to iterate through.
  9. dirloc = r"C:\location\of\images"
  10.   # a mode conversion table
  11. mode_to_bpp = {'1':1, 'L':8, 'P':8, 'RGB':24, 'RGBA':32, 'CMYK':32, 'YCbCr':24, 'I':32, 'F':32}
  12.  
  13. for file in os.scandir(dirloc):
  14.     try:
  15.         with Image.open(file.path) as im:
  16.               # print filename, image format, image dimentions
  17.               print(f" File: {file}  |  Image Format: {im.format}  |  Dimensions: {im.size}  | Colour format(bit depth):  {im.mode}({mode_to_bpp[im.mode]})")
  18.               imgsize = im.size[0] * im.size[1] * mode_to_bpp[im.mode]
  19.               print(f"     - Image Size {imgsize} bits, ({int(imgsize*0.125)} bytes)")
  20.     except OSError:
  21.         pass
Add Comment
Please, Sign In to add comment