forked from WisconsinAIVision/UniversalFakeDetect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
52 lines (44 loc) · 1.7 KB
/
demo.py
File metadata and controls
52 lines (44 loc) · 1.7 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
import torchvision.transforms.functional as TF
from PIL import Image
from random import random, choice, shuffle
# 模拟 opt 对象
class Opt:
def __init__(self):
self.rz_interp = ['bilinear'] # 插值方法列表
self.loadSize = 256 # 目标尺寸
# 定义 rz_dict
rz_dict = {'bilinear': Image.BILINEAR,
'bicubic': Image.BICUBIC,
'lanczos': Image.LANCZOS,
'nearest': Image.NEAREST}
def sample_discrete(s):
if len(s) == 1:
return s[0]
return choice(s)
def custom_resize(img, opt):
"""
对图像进行自定义尺寸调整
参数:
img: 输入图像,通常为PIL Image或Tensor格式
opt: 配置选项对象,包含图像处理相关参数
- opt.rz_interp: 尺寸调整插值方法的采样选项
- opt.loadSize: 目标图像尺寸大小
返回值:
调整尺寸后的图像,格式与输入图像一致
"""
# 从配置选项中采样离散的插值方法
interp = sample_discrete(opt.rz_interp)
# 使用指定插值方法调整图像尺寸
return TF.resize(img, opt.loadSize, interpolation=rz_dict[interp])
# 创建一个 Opt 对象
opt = Opt()
# 打开一张示例图像
image_path = '/home/data1/Forever/Effort-AIGI-Detection/UniversalFakeDetect_Benchmark/origal_img/original_27_image.jpg' # 请替换为实际的图像路径
try:
img = Image.open(image_path).convert('RGB')
print(f"原始图像尺寸: {img.size}")
# 调用 custom_resize 函数进行尺寸调整
resized_img = custom_resize(img, opt)
print(f"调整后图像尺寸: {resized_img.size}")
except FileNotFoundError:
print("未找到示例图像,请检查文件路径。")