please give me python code to store one image for following images names: علامت بانک…
- User
please give me python code to store one image for following images names: علامت بانک علامت هلال احمر علامت آتش نشانی علامت آمبولانس
- Assistant
please give me python code to store one image for following images names: علامت بانک علامت هلال احمر علامت آتش نشانی علامت آمبولانس
Certainly! Below is an example of Python code that maps each of your specified Persian names to an image and stores them with those names. This example uses the Pillow library (PIL) to handle image operations.
Assumptions:
source_images/) and will be saved to another directory (e.g., stored_images/) with the new Persian names..png, .jpg).First, ensure you have the Pillow library installed. You can install it using pip:
pip install Pillow
import os
from PIL import Image
# Define the mapping of Persian names to source image filenames
image_mapping = {
"علامت بانک": "bank_symbol.png", # Bank symbol
"علامت هلال احمر": "red_crescent.png", # Red Crescent symbol
"علامت آتش نشانی": "fire_department.png",# Fire Department symbol
"علامت آمبولانس": "ambulance.png" # Ambulance symbol
}
# Directory where the source images are located
source_dir = "source_images"
# Directory where you want to store the renamed images
destination_dir = "stored_images"
# Create the destination directory if it doesn't exist
os.makedirs(destination_dir, exist_ok=True)
# Supported image formats (you can add more if needed)
supported_formats = ['.png', '.jpg', '.jpeg', '.gif', '.bmp']
for persian_name, filename in image_mapping.items():
# Construct the full path to the source image
source_path = os.path.join(source_dir, filename)
# Check if the source image exists
if not os.path.isfile(source_path):
print(f"Source image not found: {source_path}")
continue
# Open the image using PIL
try:
with Image.open(source_path) as img:
# Optionally, you can perform image processing here
# For example, resizing:
# img = img.resize((256, 256))
# Determine the image format from the source filename
_, ext = os.path.splitext(filename)
ext = ext.lower()
if ext not in supported_formats:
print(f"Unsupported image format for file: {filename}")
continue
# Define the new filename with Persian name and original extension
new_filename = persian_name + ext
# Ensure the filename is valid in the filesystem
# Depending on your OS, some characters might not be allowed
# You might need to transliterate or sanitize the filename
# Here, we'll attempt to save it directly
destination_path = os.path.join(destination_dir, new_filename)
# Save the image to the destination directory
img.save(destination_path)
print(f"Saved '{filename}' as '{new_filename}' in '{destination_dir}'")
except Exception as e:
print(f"Error processing {filename}: {e}")
Importing Modules:
os for handling directory and file paths.PIL.Image from the Pillow library for image operations.Mapping Persian Names to Filenames:
image_mapping is a dictionary where each key is a Persian name, and the value is the corresponding source image filename.Setting Up Directories:
source_dir is the directory where your original images are stored.destination_dir is where you want to save the renamed images.os.makedirs function ensures that the destination directory exists.Processing Each Image:
image_mapping.PIL.Handling Errors:
Filesystem Compatibility: Some operating systems might have restrictions on filenames, especially with non-Latin characters. Ensure that your OS supports Persian characters in filenames. If you encounter issues, consider transliterating the Persian names to Latin characters or sanitizing the filenames.
Image Formats: The script preserves the original image format based on the file extension. Ensure that the extensions in image_mapping correspond to the actual image formats.
Further Enhancements:
transliterate.Assuming the following directory structure:
your_project/
│
├── source_images/
│ ├── bank_symbol.png
│ ├── red_crescent.png
│ ├── fire_department.png
│ └── ambulance.png
│
├── stored_images/ # This will be created if it doesn't exist
│
└── your_script.py # The Python script provided above
After running the script, the stored_images/ directory will contain:
stored_images/
├── علامت بانک.png
├── علامت هلال احمر.png
├── علامت آتش نشانی.png
└── علامت آمبولانس.png
Each image will be saved with its corresponding Persian name.