Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- from PIL import Image
- def encode_image(image_path):
- with open(image_path, "rb") as image_file:
- image_data = image_file.read()
- encoded_data = base64.b64encode(image_data)
- return encoded_data.decode("utf-8")
- def decode_image(encoded_data, output_path):
- decoded_data = base64.b64decode(encoded_data)
- with open(output_path, "wb") as output_file:
- output_file.write(decoded_data)
- def main():
- while True:
- print("1. Comprimir imagen")
- print("2. Descomprimir imagen")
- print("3. Salir")
- choice = input("Selecciona una opción: ")
- if choice == "1":
- image_path = input("Ingresa la ruta de la imagen: ")
- encoded_image = encode_image(image_path)
- output_path = input("Ingresa el nombre del archivo de salida (con extensión .txt): ")
- with open(output_path, "w") as text_file:
- text_file.write(encoded_image)
- print("Imagen comprimida y guardada como texto.")
- elif choice == "2":
- input_path = input("Ingresa la ruta del archivo de texto: ")
- output_path = input("Ingresa el nombre del archivo de salida (con extensión compatible): ")
- with open(input_path, "r") as text_file:
- encoded_image = text_file.read()
- decode_image(encoded_image, output_path)
- print("Imagen descomprimida y guardada.")
- elif choice == "3":
- break
- else:
- print("Opción no válida. Por favor, selecciona una opción válida.")
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment