import os import imageio from PIL import Image Define the directory where your .eps…
- User
import os import imageio from PIL import Image
Define the directory where your .eps files are located
eps_directory = 'tempo'
Define the directory where you want to save temporary images
temp_image_directory = 'tempimg'
Create the directory for temporary images if it doesn't exist
if not os.path.exists(temp_image_directory): os.makedirs(temp_image_directory)
List all .eps files in the directory
eps_files = [f for f in os.listdir(eps_directory) if f.endswith('.eps')]
Sort the files if necessary
eps_files.sort()
Convert each .eps file to an image format such as PNG
images = [] total_files = len(eps_files) for index, eps_filename in enumerate(eps_files): # Define the full path to the .eps file and the output image file eps_path = os.path.join(eps_directory, eps_filename) image_path = os.path.join(temp_image_directory, f'image_{index}.png')
# Open the .eps file and convert to RGB (necessary for .mp4) image = Image.open(eps_path).convert('RGB') # Save the image as .png image.save(image_path) # Append the image to the list of images images.append(image_path) # Print the progress status print(f"Converting file {index+1} of {total_files} ({(index + 1) / total_files * 100:.2f}%)")Define the output .mp4 file path
output_video_path = 'output_video.mp4'
Create the .mp4 video from the images
fps = 24 # Frames per second (you can change this according to your needs) writer = imageio.get_writer(output_video_path, fps=fps)
for i, image_path in enumerate(images): # Read each image and append it to the video image = imageio.imread(image_path) writer.append_data(image) # Print the video creation progress print(f"Adding image {i+1} of {total_files} to video ({(i + 1) / total_files * 100:.2f}%)")
Close the writer
This code seem incomplete, can you finish it and make it delete every images in tempo and tempimpg
- Assistant
Sure! Here's the updated code that removes the temporary image files and the EPS files after creating the video:
import os import imageio from PIL import Image # Define the directory where your .eps files are located eps_directory = 'tempo' # Define the directory where you want to save temporary images temp_image_directory = 'tempimg' # Create the directory for temporary images if it doesn't exist if not os.path.exists(temp_image_directory): os.makedirs(temp_image_directory) # List all .eps files in the directory eps_files = [f for f in os.listdir(eps_directory) if f.endswith('.eps')] # Sort the files if necessary eps_files.sort() # Convert each .eps file to an image format such as PNG images = [] total_files = len(eps_files) for index, eps_filename in enumerate(eps_files): # Define the full path to the .eps file and the output image file eps_path = os.path.join(eps_directory, eps_filename) image_path = os.path.join(temp_image_directory, f'image_{index}.png') # Open the .eps file and convert to RGB (necessary for .mp4) image = Image.open(eps_path).convert('RGB') # Save the image as .png image.save(image_path) # Append the image to the list of images images.append(image_path) # Print the progress status print(f"Converting file {index+1} of {total_files} ({(index + 1) / total_files * 100:.2f}%)") # Define the output .mp4 file path output_video_path = 'output_video.mp4' # Create the .mp4 video from the images fps = 24 # Frames per second (you can change this according to your needs) writer = imageio.get_writer(output_video_path, fps=fps) for i, image_path in enumerate(images): # Read each image and append it to the video image = imageio.imread(image_path) writer.append_data(image) # Print the video creation progress print(f"Adding image {i+1} of {total_files} to video ({(i + 1) / total_files * 100:.2f}%)") # Close the writer writer.close() # Remove temporary image files for image_path in images: os.remove(image_path) # Remove EPS files for eps_file in eps_files: eps_path = os.path.join(eps_directory, eps_file) os.remove(eps_path) # Print completion message print("Video creation complete. Temporary files removed.") - User
I can have several thousand file, can you make it a progession bar so it doesn't print for each individual file and loose time
- Assistant
Certainly! We can use the
tqdmlibrary to create a progress bar for the file conversion and video creation process. Here's the updated code with the progress bar:import os import imageio from PIL import Image from tqdm import tqdm # Define the directory where your .eps files are located eps_directory = 'tempo' # Define the directory where you want to save temporary images temp_image_directory = 'tempimg' # Create the directory for temporary images if it doesn't exist if not os.path.exists(temp_image_directory): os.makedirs(temp_image_directory) # List all .eps files in the directory eps_files = [f for f in os.listdir(eps_directory) if f.endswith('.eps')] # Sort the files if necessary eps_files.sort() # Convert each .eps file to an image format such as PNG images = [] total_files = len(eps_files) for index, eps_filename in tqdm(enumerate(eps_files), desc="Converting Files", total=total_files): # Define the full path to the .eps file and the output image file eps_path = os.path.join(eps_directory, eps_filename) image_path = os.path.join(temp_image_directory, f'image_{index}.png') # Open the .eps file and convert to RGB (necessary for .mp4) image = Image.open(eps_path).convert('RGB') # Save the image as .png image.save(image_path) # Append the image to the list of images images.append(image_path) # Define the output .mp4 file path output_video_path = 'output_video.mp4' # Create the .mp4 video from the images fps = 24 # Frames per second (you can change this according to your needs) writer = imageio.get_writer(output_video_path, fps=fps) for i, image_path in tqdm(enumerate(images), desc="Creating Video", total=total_files): # Read each image and append it to the video image = imageio.imread(image_path) writer.append_data(image) # Close the writer writer.close() # Remove temporary image files for image_path in images: os.remove(image_path) # Remove EPS files for eps_file in eps_files: eps_path = os.path.join(eps_directory, eps_file) os.remove(eps_path) # Print completion message print("Video creation complete. Temporary files removed.")