Skip to content

Instantly share code, notes, and snippets.

@pattersonrptr
Last active April 18, 2024 15:00
Show Gist options
  • Save pattersonrptr/74d828fda61cb8c02ec3bbe782489343 to your computer and use it in GitHub Desktop.
Save pattersonrptr/74d828fda61cb8c02ec3bbe782489343 to your computer and use it in GitHub Desktop.
Extract info about images and pdf files
import os
import sys
from PIL import Image
from PyPDF2 import PdfReader
def evaluate_image(image_path):
try:
image = Image.open(image_path)
image_format = image.format
image_mode = image.mode
image_size = os.path.getsize(image_path)
image_resolution = image.size
pixel_density = image.size[0] * image.size[1]
print(f"Format: {image_format}")
print(f"Mode: {image_mode}")
print(f"File Size: {image_size} bytes")
print(f"Resolution: {image_resolution[0]}x{image_resolution[1]} pixels")
print(f"Pixel Density: {pixel_density} pixels")
except IOError:
print("Unable to open the image.")
def evaluate_pdf(pdf_path):
try:
pdf_reader = PdfReader(pdf_path)
num_pages = len(pdf_reader.pages)
pdf_size = os.path.getsize(pdf_path)
metadata = pdf_reader.metadata
title = metadata.get('/Title', "N/A")
author = metadata.get('/Author', "N/A")
creation_date = metadata.get('/CreationDate', "N/A")
print(f"Number of pages: {num_pages}")
print(f"PDF File Size: {pdf_size} bytes")
print(f"Title: {title}")
print(f"Author: {author}")
print(f"Creation Date: {creation_date}")
except FileNotFoundError:
print("PDF file not found.")
except Exception as e:
print(f"Error opening the PDF file: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python main.py <input-file>")
sys.exit(1)
input_file = sys.argv[1]
if input_file.endswith(".pdf"):
evaluate_pdf(input_file)
else:
evaluate_image(input_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment