有个想法,利用一张图片做个特效,比如从左上到右下展示图片。
代码区
做了个源代码,削为的满足了我的想法:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/dota.jpg")
value = 500
ROI = srcImage[:value, :value]
frames = []
desImage = np.zeros((value, value, 3), dtype=np.uint8)
#左上右下展示图片特效
for i in range(value+value-1):
if i < value:
for j in range(i+1):
desImage[j][i-j] = ROI[j][i-j]
else:
for j in range(i-value+1,value):
desImage[j][i-j] = ROI[j][i-j]
cv2.imshow("desImage", desImage)
cv2.waitKey(1)
cv2.waitKey(0)
后来又想,能不能右下到左上来展示图片呢?
由写了个源代码:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/dota.jpg")
value = 500
ROI = srcImage[:value, :value]
frames = []
desImage = np.zeros((value, value, 3), dtype=np.uint8)
# 右下左上展示图片特效
for i in range(value + value - 1, 0, -1):
if i >= value:
for j in range(value - 1, i - value - 1, -1):
desImage[j][i - j - 1] = ROI[j][i - j - 1]
else:
for j in range(i-1, -1, -1):
desImage[j][i - j - 1] = ROI[j][i - j - 1]
cv2.imshow("desImage", desImage)
cv2.waitKey(1)
cv2.waitKey(0)
也削为的实现了我的想法。
后来又想,能不能把这展示出来的图片特效保存成gif动态图呢?
撸代码:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import imageio
srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/dota.jpg")
gif_name = "E:/Study/python/OpenCV_study/img/created_gif.gif"
value = 500
ROI = srcImage[:value, :value]
frames = []
desImage = np.zeros((value, value, 3), dtype=np.uint8)
for i in range(value+value-1):
if i < value:
for j in range(i+1):
desImage[j][i-j] = ROI[j][i-j]
else:
for j in range(i-value+1,value):
desImage[j][i-j] = ROI[j][i-j]
if i % 100 == 0:
frames.append(desImage)
frames.append(desImage)
imageio.mimsave(gif_name, frames, 'GIF', duration=0.1)
撸出问题来了吧。结果生成的gif根本就不动!!!而且跟原图都不是一个颜色(太绿了!!)
走查代码发现,应该是frames.append(desImage)这个地方有点问题,看文档,感觉这个desImage每次append后,都会覆盖前边的数据。所以判断append应该是引用的数据,在desImage有变动后,frames里的数据也跟着变了,所以生成的gif都是一张图片,才不会动。修改下代码吧
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import imageio
srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/dota.jpg")
gif_name = "E:/Study/python/OpenCV_study/img/created_gif.gif"
value = 500
ROI = srcImage[:value, :value]
frames = []
desImage = np.zeros((value, value, 3), dtype=np.uint8)
for i in range(value+value-1):
if i < value:
for j in range(i+1):
desImage[j][i-j] = ROI[j][i-j]
else:
for j in range(i-value+1,value):
desImage[j][i-j] = ROI[j][i-j]
if i % 100 == 0:
tmpImage = desImage.copy() # 修改此处,将desImage的数据复制一份,这样在desImage数据有变动的时候,不应影响frames里的数据
frames.append(tmpImage)
frames.append(desImage)
imageio.mimsave(gif_name, frames, 'GIF', duration=0.1)
果不其然,最起码能动了。但是还是跟原图的颜色不一致(还是太绿了!!!),看文档,看别人写的代码,发现都是用的png图片读取的形式,来做的gif,难道要把desImage矩阵转换为png图片,然后读出来append到frame里去嘛?试验试验再说吧,撸代码:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import imageio
srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/dota.jpg")
gif_name = "E:/Study/python/OpenCV_study/img/created_gif.gif"
value = 500
ROI = srcImage[:value, :value]
frames = []
desImage = np.zeros((value, value, 3), dtype=np.uint8)
for i in range(value+value-1):
if i < value:
for j in range(i+1):
desImage[j][i-j] = ROI[j][i-j]
else:
for j in range(i-value+1,value):
desImage[j][i-j] = ROI[j][i-j]
if i % 100 == 0:
# 修改此处,将desImage先保存为png图片,且文件名不一样。再imread,append到frame数据里。
cv2.imwrite("E:/Study/python/OpenCV_study/img/create_gif"+str(i)+".png",desImage)
frames.append(imageio.imread("E:/Study/python/OpenCV_study/img/create_gif"+str(i)+".png"))
cv2.imwrite("E:/Study/python/OpenCV_study/img/create_gif.png",desImage)
frames.append(imageio.imread("E:/Study/python/OpenCV_study/img/create_gif.png"))
imageio.mimsave(gif_name, frames, 'GIF', duration=0.1)
还算是满足要求的。不过,看gif演示的时候,图片颜色会出现变化,感觉应该是图片压缩了矩阵里的数据,所以会出现颜色偏差,毕竟生成的图片、gif动图都才几K。
总结下