from PIL import Image def calculate_image_pixels(image_path): try: with Image.open(image_path) as img: width, height = img.size total_pixels = width * height return total_pixels except Exception as e: print(f"Error calculating image pixels: {e}") return None def calculate_text_characters(text_path): try: with open(text_path, 'r', encoding='utf-8') as file: text = file.read() total_characters = len(text) return total_characters except Exception as e: print(f"Error calculating text characters: {e}") return None def main(): image_path = 'test_image' text_path = 'test_document' # Calculate total pixels in the image total_pixels = calculate_image_pixels(image_path) # Calculate total characters in the text document total_characters = calculate_text_characters(text_path) # Prepare the result dictionary result = { 'total_pixels': total_pixels, 'total_characters': total_characters } # Write the result to a text file try: with open('result.txt', 'w') as result_file: result_file.write(str(result)) except Exception as e: print(f"Error writing result to file: {e}") # Output the result print(result) if __name__ == "__main__": main()