-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg-convertor.sh
More file actions
55 lines (45 loc) · 1.58 KB
/
img-convertor.sh
File metadata and controls
55 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
====================================
= Convert JPG to PNG and viceversa =
====================================
# Define colors
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No color
# Check if correct arguments are provided
if [ $# -ne 2 ]; then
echo -e "${RED}[!] Error: Usage: $0 <source_extension> <target_extension>${NC}"
echo "Example: $0 jpg png"
exit 1
fi
# Get source and target extensions from arguments
SOURCE_EXT=$1
TARGET_EXT=$2
# Convert source extension to uppercase for display
SOURCE_EXT_UPPER=$(echo "$SOURCE_EXT" | tr '[:lower:]' '[:upper:]')
# Directory to search for image files
DIRECTORY="."
# Find all source files
image_files=("$DIRECTORY"/*."$SOURCE_EXT")
# Check if any source files are found
if [ ${#image_files[@]} -eq 0 ]; then
echo -e "${RED}[!] Error: No .$SOURCE_EXT_UPPER files found in the directory.${NC}"
exit 1
else
echo -e "${GREEN}[+] ${#image_files[@]} $SOURCE_EXT_UPPER files found in the directory.${NC}\n"
fi
# Loop through all source files and convert to target extension
for file in "${image_files[@]}"; do
if [[ -f "$file" ]]; then
# Get the filename without extension
filename=$(basename "$file" .$SOURCE_EXT)
# Convert the file to target format and handle errors
if convert "$file" "$DIRECTORY/$filename.$TARGET_EXT"; then
echo -e "${GREEN}[+] Converted $file to $filename.$TARGET_EXT${NC}"
else
echo -e "${RED}[!] Error: Failed to convert $file${NC}"
fi
else
echo -e "${RED}[!] Error: $file is not a valid file.${NC}"
fi
done