If the model fails to load, fallback default OpenCV effects are applied.
1pip install torch torchvision opencv-python pillow numpy
2
3
4
5import torch
6from torchvision import transforms
7from PIL import Image
8import cv2
9import numpy as np
10
11# Load model
12try:
13 effect_model = torch.load('effect_model.pth', map_location='cpu')
14 effect_model.eval()
15except:
16 effect_model = None
17
18# Define transform
19transform = transforms.Compose([
20 transforms.Resize((256, 256)),
21 transforms.ToTensor(),
22 transforms.Normalize(mean=[0.485, 0.456, 0.406],
23 std=[0.229, 0.224, 0.225])
24])
25
26# Load image and apply model effect
27image = Image.open("your_image.jpg").convert("RGB")
28if effect_model:
29 img_tensor = transform(image).unsqueeze(0)
30 with torch.no_grad():
31 output = effect_model(img_tensor)
32 output = output.squeeze(0).permute(1, 2, 0).numpy()
33 output = (output * 255).astype(np.uint8)
34 result = Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
35else:
36 print("Fallback to default effects")
37
38
39
40
41Supported Effects
42The following effects are supported (via the model or fallback OpenCV operations):
43
44Horizontal / Vertical Flip
45
46Blur / Sharpen / Glow
47
48Grayscale / Sepia / Invert
49
50Brightness / Contrast / Saturation Adjustments
51
52Vintage / Vignette / HDR / Dehaze
53
54Cartoon / Sketch / Pencil / Charcoal / Halftone
55
56Oil Painting / Watercolor / Pastel
57
58Pixelate / Mosaic / Duotone / Thermal
59
60Rotate / Tilt Shift / Light Leak / Lens Flare
61
62Background Blur / Face Beautify
63
64Glass Effect / Fog / Shadow / Highlight
65
66Super Resolution / Toonify / Neon and many more...
67
68To apply effects, pass effect_name to your function like:
69
70
71result = apply_image_effect(img, effect_name="cartoon")