Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit 451ae6f

Browse files
documentation
1 parent ed6b62c commit 451ae6f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Image Converter Documentation
2+
3+
## Overview
4+
5+
This Python script is designed to convert images from one format to another using the PIL (Python Imaging Library) module.
6+
7+
## Usage
8+
9+
To use this script, follow these steps:
10+
11+
1. Ensure you have Python installed on your system.
12+
2. Install the required Python modules using pip:
13+
```
14+
pip install pillow
15+
```
16+
3. Run the script using Python:
17+
```
18+
python image_converter.py
19+
```
20+
21+
## Functions
22+
23+
### `convert_image(input_path, output_path, output_format)`
24+
25+
This function converts an input image to the specified output format and saves it to the output path.
26+
27+
- `input_path` (string): The file path of the input image.
28+
- `output_path` (string): The file path where the converted image will be saved.
29+
- `output_format` (string): The desired output format for the image (e.g., JPEG, PNG, BMP, GIF).
30+
31+
## Error Handling
32+
33+
- If the input image file cannot be opened, an IOError is raised, and an error message is displayed.
34+
- If the conversion process fails, an exception is caught, and an error message is displayed.
35+
36+
## Example
37+
38+
```python
39+
from PIL import Image
40+
import os
41+
42+
def convert_image(input_path, output_path, output_format):
43+
try:
44+
img = Image.open(input_path)
45+
except IOError:
46+
print("Unable to open image file:", input_path)
47+
return
48+
49+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
50+
51+
try:
52+
img.save(output_path, format=output_format)
53+
print("Image converted successfully!")
54+
except Exception as e:
55+
print("Failed to convert image:", e)
56+
57+
if __name__ == "__main__":
58+
input_path = input("Enter the path to the input image: ")
59+
output_path = input("Enter the path for the output image: ")
60+
output_format = input("Enter the desired output format (JPEG, PNG, BMP, GIF): ").upper()
61+
62+
if output_format not in ["JPEG", "PNG", "BMP", "GIF"]:
63+
print("Invalid output format!")
64+
else:
65+
convert_image(input_path, output_path, output_format)
66+
```
67+
68+
## Dependencies
69+
70+
- **PIL (Python Imaging Library)**: This script requires the PIL module to manipulate images. You can install it using pip:
71+
72+
```
73+
pip install pillow
74+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /