Upload 2 files
Browse files- image2vidimg.py +141 -0
- tensor2video.py +23 -0
image2vidimg.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from extract.getim import load_image
|
5 |
+
from torchvision import transforms
|
6 |
+
import cv2
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
transform = transforms.Compose([
|
12 |
+
transforms.ToTensor(), # 将numpy数组或PIL.Image读的图片转换成(C,H, W)的Tensor格式且/255归一化到[0,1.0]之间
|
13 |
+
]) # 来自ImageNet的mean和variance
|
14 |
+
|
15 |
+
|
16 |
+
# fcontent = load_image("./ori/0.jpg",transform=None,shape=[512, 256])
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
def show_cut(path, left, upper, right, lower):
|
21 |
+
"""
|
22 |
+
原图与所截区域相比较
|
23 |
+
:param path: 图片路径
|
24 |
+
:param left: 区块左上角位置的像素点离图片左边界的距离
|
25 |
+
:param upper:区块左上角位置的像素点离图片上边界的距离
|
26 |
+
:param right:区块右下角位置的像素点离图片左边界的距离
|
27 |
+
:param lower:区块右下角位置的像素点离图片上边界的距离
|
28 |
+
故需满足:lower > upper、right > left
|
29 |
+
"""
|
30 |
+
|
31 |
+
img = path
|
32 |
+
|
33 |
+
# print("This image's size: {}".format(img.size)) # (W, H)
|
34 |
+
# img.save("kkk.jpg")
|
35 |
+
# plt.figure("Image Contrast")
|
36 |
+
#
|
37 |
+
# plt.subplot(1, 2, 1)
|
38 |
+
# plt.title('origin')
|
39 |
+
#
|
40 |
+
# plt.imshow(img)
|
41 |
+
# plt.axis('off')
|
42 |
+
#
|
43 |
+
# box = (left, upper, right, lower)
|
44 |
+
# roi = img.crop(box)
|
45 |
+
#
|
46 |
+
# plt.subplot(1, 2, 2)
|
47 |
+
# plt.title('roi')
|
48 |
+
# plt.imshow(roi)
|
49 |
+
# plt.axis('off')
|
50 |
+
# plt.show()
|
51 |
+
|
52 |
+
|
53 |
+
def image_cut_save(path, left, upper, right, lower):
|
54 |
+
"""
|
55 |
+
所截区域图片保存
|
56 |
+
:param path: 图片路径
|
57 |
+
:param left: 区块左上角位置的像素点离图片左边界的距离
|
58 |
+
:param upper:区块左上角位置的像素点离图片上边界的距离
|
59 |
+
:param right:区块右下角位置的像素点离图片左边界的距离
|
60 |
+
:param lower:区块右下角位置的像素点离图片上边界的距离
|
61 |
+
故需满足:lower > upper、right > left
|
62 |
+
:param save_path: 所截图片保存位置
|
63 |
+
"""
|
64 |
+
img = path # 打开图像
|
65 |
+
box = (left, upper, right, lower)
|
66 |
+
roi = img.crop(box)
|
67 |
+
# roi.save(save_path)
|
68 |
+
return transform(roi)
|
69 |
+
|
70 |
+
|
71 |
+
# 保存截取的图片
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
# def getcontent(fcontent,gap):
|
76 |
+
# Intgap=gap/9
|
77 |
+
# a=torch.Tensor()
|
78 |
+
# for i in range(10):
|
79 |
+
# pic_path = fcontent
|
80 |
+
# # pic_save_dir_path = './out2/0-'+str(i)+".jpg"
|
81 |
+
# left, upper, right, lower = Intgap*i, 0, Intgap*i+gap, gap
|
82 |
+
# a=torch.cat([a,image_cut_save(pic_path, left, upper, right, lower).unsqueeze(1)],dim=1)
|
83 |
+
# return a
|
84 |
+
# def cobtwoten(image_path):
|
85 |
+
# fcontent = load_image(image_path, transform=None, shape=[512, 256])
|
86 |
+
# Intgap = 256
|
87 |
+
# a = torch.Tensor()
|
88 |
+
# for i in range(2):
|
89 |
+
# pic_path = fcontent
|
90 |
+
# #pic_save_dir_path = './out2/0-' + str(i) + ".jpg"
|
91 |
+
# left, upper, right, lower = Intgap * i, 0, Intgap * i + Intgap, Intgap
|
92 |
+
# a = torch.cat([a, image_cut_save(pic_path, left, upper, right, lower).unsqueeze(1)], dim=1)
|
93 |
+
# return a.unsqueeze(0)
|
94 |
+
|
95 |
+
def cobtwoten(image_path):
|
96 |
+
fcontent = load_image(image_path, transform=None, shape=[256, 128])
|
97 |
+
Intgap = 128/9
|
98 |
+
a = torch.Tensor()
|
99 |
+
for i in range(10):
|
100 |
+
pic_path = fcontent
|
101 |
+
#pic_save_dir_path = './out2/0-' + str(i) + ".jpg"
|
102 |
+
left, upper, right, lower = Intgap * i, 0, Intgap * i + 128, 128
|
103 |
+
a = torch.cat([a, image_cut_save(pic_path, left, upper, right, lower).unsqueeze(1)], dim=1)
|
104 |
+
return a.unsqueeze(0)
|
105 |
+
|
106 |
+
def cobtwoten256(image_path):
|
107 |
+
fcontent = load_image(image_path, transform=None, shape=[512,256])
|
108 |
+
Intgap = 256/9
|
109 |
+
a = torch.Tensor()
|
110 |
+
for i in range(10):
|
111 |
+
pic_path = fcontent
|
112 |
+
#pic_save_dir_path = './out2/0-' + str(i) + ".jpg"
|
113 |
+
left, upper, right, lower = Intgap * i, 0, Intgap * i + 256, 256
|
114 |
+
a = torch.cat([a, image_cut_save(pic_path, left, upper, right, lower).unsqueeze(1)], dim=1)
|
115 |
+
return a.unsqueeze(0)
|
116 |
+
|
117 |
+
#
|
118 |
+
# fcontent = load_image("./extract/image/0.jpg",transform=None,shape=[256,128])
|
119 |
+
# Intgap = 128
|
120 |
+
# a = torch.Tensor()
|
121 |
+
# for i in range(2):
|
122 |
+
# pic_path = fcontent
|
123 |
+
# pic_save_dir_path = './out2/0-'+str(i)+".jpg"
|
124 |
+
# left, upper, right, lower = Intgap * i, 0, Intgap * i + 128, 128
|
125 |
+
# a = torch.cat([a, image_cut_save(pic_path, left, upper, right, lower,pic_save_dir_path).unsqueeze(1)], dim=1)
|
126 |
+
# print(a.shape)
|
127 |
+
import numpy as np
|
128 |
+
def imgsave(image, path):
|
129 |
+
image = image.squeeze(0)
|
130 |
+
image = image.permute(1, 2, 0)
|
131 |
+
image_np = image.cpu().numpy()*255
|
132 |
+
image_np = image_np.astype(np.uint8)
|
133 |
+
Image.fromarray(image_np).save(path) # 直接保存PIL图像对象
|
134 |
+
|
135 |
+
# lik=["0"]
|
136 |
+
# for name in lik:
|
137 |
+
# videos=cobtwoten("./extract/image/0.jpg").permute(0, 2, 1, 3, 4)
|
138 |
+
# print(videos.shape)
|
139 |
+
# for i in range(10):
|
140 |
+
# frame = videos[:, i, :, :]
|
141 |
+
# imgsave(frame, "./out2/"+str(i)+".jpg")
|
tensor2video.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import imageio
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# 设置生成的视频文件名和路径
|
6 |
+
filename = 'output.mp4'
|
7 |
+
filepath = os.path.join(os.getcwd(), filename)
|
8 |
+
print(filepath)
|
9 |
+
|
10 |
+
# 读取所有 PNG 图片
|
11 |
+
images = []
|
12 |
+
for file_name in sorted(os.listdir("./out2/")):
|
13 |
+
if file_name.endswith('.jpg'):
|
14 |
+
images.append(Image.open("./out2/"+file_name))
|
15 |
+
|
16 |
+
|
17 |
+
import numpy as np
|
18 |
+
# 将图片转换为视频
|
19 |
+
fps = 25 # 每秒钟30帧
|
20 |
+
with imageio.get_writer(filepath, fps=fps) as video:
|
21 |
+
for image in images:
|
22 |
+
frame = np.array(image.convert('RGB'))
|
23 |
+
video.append_data(frame)
|