Skip to content

Commit fbf2fc7

Browse files
Merge pull request #3 from renan-siqueira/develop
Merge develop into main
2 parents b55ded0 + ab9982e commit fbf2fc7

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 renan-siqueira
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# X-ray Image Effect Project
2+
3+
This project applies an X-ray effect to images using Python and the Python Imaging Library (PIL). It is a useful tool for creating datasets with characteristics of x-ray images.
4+
5+
---
6+
7+
## Environment Setup
8+
9+
### Prerequisites
10+
11+
- Python 3.x
12+
- pip (Python package manager)
13+
14+
### Setting Up a Virtual Environment
15+
16+
1. **Create a Virtual Environment**:
17+
18+
In your terminal, navigate to your project directory and run:
19+
20+
```bash
21+
python -m venv .venv
22+
```
23+
24+
This will create a virtual environment named `.venv` in your project directory.
25+
26+
2. **Activate the Virtual Environment**:
27+
- On Windows, run:
28+
```
29+
.\venv\Scripts\activate
30+
```
31+
- On Linux or macOS, run:
32+
```
33+
source venv/bin/activate
34+
```
35+
36+
### Installing Dependencies
37+
38+
With the virtual environment activated, install the necessary dependencies by running:
39+
40+
```bash
41+
pip install -r requirements.txt
42+
```
43+
44+
## Project Configuration
45+
46+
1. **Application Settings**:
47+
48+
Configure the input and output paths in the `src/config.py` file. For example:
49+
```python
50+
APP_PATH_INPUT_IMAGES = 'path/to/input/images/directory'
51+
APP_PATH_OUTPUT_IMAGES = 'path/to/output/images/directory'
52+
```
53+
54+
---
55+
56+
## Usage
57+
58+
To use the application, ensure the virtual environment is activated. In the root directory of the project, run:
59+
60+
```bash
61+
python main.py
62+
```
63+
64+
This will process all images in the specified input directory and save the X-ray effect images to the output directory.
65+
66+
---
67+
68+
## Additional Notes
69+
70+
- Ensure the input directory contains images in supported formats (`.png`, `.jpg`, `.jpeg`, `.webp`).
71+
- The script will create the output directory if it does not exist.
72+
73+
---
74+
75+
## License
76+
77+
This project is open-sourced and available to everyone under the [MIT License](LICENSE).

main.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
from PIL import Image, ImageOps
3+
from src.settings import config
4+
5+
6+
def get_image_paths(directory):
7+
"""
8+
Returns a list of file paths for all images in the given directory.
9+
10+
Parameters:
11+
directory (str): Path to the directory to scan for images.
12+
13+
Returns:
14+
list: A list of paths to the images in the directory.
15+
"""
16+
image_extensions = ['.png', '.jpg', '.jpeg', '.webp']
17+
return [os.path.join(directory, file) for file in os.listdir(directory)
18+
if os.path.splitext(file)[1].lower() in image_extensions]
19+
20+
def xray_effect(image_path, output_dir):
21+
"""
22+
Applies an X-ray effect to the provided image and saves it to the output directory.
23+
24+
Parameters:
25+
image_path (str): The path to the input image.
26+
output_dir (str): The directory where the X-ray effect image will be saved.
27+
"""
28+
try:
29+
with Image.open(image_path) as img:
30+
inverted_image = ImageOps.invert(img)
31+
grayscale_image = inverted_image.convert("L")
32+
contrasted_image = ImageOps.autocontrast(grayscale_image)
33+
34+
output_path = os.path.join(output_dir, get_output_filename(image_path))
35+
contrasted_image.save(output_path)
36+
return f"X-ray effect image saved as {output_path}"
37+
except Exception as e:
38+
return f"An error occurred: {e}"
39+
40+
def get_output_filename(input_path):
41+
"""
42+
Generates the output filename for the X-ray effect image based on the input image path.
43+
44+
Parameters:
45+
input_path (str): The path to the input image.
46+
47+
Returns:
48+
str: The output filename for the X-ray effect image.
49+
"""
50+
base_name = os.path.basename(input_path)
51+
name, ext = os.path.splitext(base_name)
52+
return f"{name}_x-ray{ext}"
53+
54+
def main(input_dir, output_dir):
55+
"""
56+
Main function to apply X-ray effect to all images in the input directory and save them to the output directory.
57+
58+
Parameters:
59+
input_dir (str): Path to the directory containing input images.
60+
output_dir (str): Path to the directory where the X-ray effect images will be saved.
61+
"""
62+
if not os.path.exists(output_dir):
63+
os.makedirs(output_dir)
64+
65+
image_paths = get_image_paths(input_dir)
66+
for path in image_paths:
67+
print(xray_effect(path, output_dir))
68+
69+
70+
if __name__ == '__main__':
71+
main(config.APP_PATH_INPUT_IMAGES, config.APP_PATH_OUTPUT_IMAGES)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pillow==10.1.0

src/settings/__init__.py

Whitespace-only changes.

src/settings/config.example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_PATH_INPUT_IMAGES = 'data/input'
2+
APP_PATH_OUTPUT_IMAGES = 'data/output'

0 commit comments

Comments
 (0)