-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpng_info.py
More file actions
executable file
·127 lines (113 loc) · 4.41 KB
/
png_info.py
File metadata and controls
executable file
·127 lines (113 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
########################################################################
# png_info.py: PNG File Information Reader
#
# Description:
# This Python script reads basic information from PNG files, such as
# width, height, bit depth, and color type. It supports handling
# multiple files and uses glob patterns for file selection.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# png_info.py <file_pattern>
#
# Example:
# png_info.py *.png
# Reads width, height, bit depth, and color type information from PNG files.
#
# Requirements:
# - Python Version: 3.1 or later
#
# Version History:
# v1.5 2025-07-01
# Standardized termination behavior for consistent script execution.
# v1.4 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v1.3 2025-04-14
# Unify error and info message formatting with stderr and prefix tags.
# v1.2 2023-12-08
# Removed f-strings for compatibility with Python versions below 3.6.
# Added help message display when no arguments are provided.
# v1.1 2023-12-06
# Refactored for clarity and improved error handling.
# v1.0 2023-11-25
# Initial release. Reads width, height, bit depth, and color type
# information from PNG files.
#
########################################################################
import glob
import os
import struct
import sys
def usage():
""" Display the script header as usage information and exit. """
script_path = os.path.abspath(__file__)
in_header = False
try:
with open(script_path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip().startswith('#' * 10):
if not in_header:
in_header = True
continue
else:
break
if in_header and line.startswith('#'):
if line.startswith('# '):
print(line[2:], end='')
else:
print(line[1:], end='')
except Exception as e:
print("Error reading usage information: %s" % str(e), file=sys.stderr)
sys.exit(1)
sys.exit(0)
def read_png_info(png_file_path):
"""
Reads basic information from a PNG file.
Args:
png_file_path (str): The path to the PNG file.
Returns:
tuple: Contains width, height, bit depth, and color type name of the PNG.
Raises:
ValueError: If the file is not a valid PNG or lacks essential data.
"""
with open(png_file_path, "rb") as f:
# Verify PNG file signature
sign = f.read(8)
if sign != b'\x89PNG\r\n\x1a\n':
raise ValueError("Not a valid PNG file")
# Read and process the IHDR chunk for image properties
length, chunk_type = struct.unpack(">I4s", f.read(8))
if chunk_type != b'IHDR':
raise ValueError("IHDR chunk not found")
data = f.read(length)
width, height, bit_depth, color_type = struct.unpack(
">IIBB", data[:10])
# Mapping of color type values to human-readable names
color_types = {3: "PNG-8", 2: "PNG-24", 6: "PNG-32"}
color_type_name = color_types.get(color_type, "UNKNOWN")
return width, height, bit_depth, color_type_name
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help', '-v', '--version'):
usage()
else:
# Process each file matching the provided glob pattern
for arg in sys.argv[1:]:
for filename in glob.glob(arg):
try:
width, height, bit_depth, color_type_name = read_png_info(
filename)
print("[INFO] File: {}".format(filename))
print("Width: {:4d}".format(width))
print("Height: {:4d}".format(height))
print("Bit Depth: {:4d}".format(bit_depth))
print("Color Type: {}".format(color_type_name))
print("")
sys.exit(0)
except Exception as e:
print("[ERROR] Error processing {}: {}".format(filename, e), file=sys.stderr)
sys.exit(1)