Skip to main content
 首页 » 编程设计

python之python中的镜像

2025年05月04日19lexus

我想在 python 中镜像一个图像,但是出现了这个错误

Exception has occurred: IndexError 
index -751 is out of bounds for axis 1 with size 750 
File "D:\PART 1\Miror.py", line 20, in <module> 
d=img.item(i,mn,0) 

这是我的代码

img = cv2.imread('D:\\PART 1\\gwk.jpg') 
 
tinggi = img.shape[0] 
lebar  = img.shape[1] 
brightness = 100 
nm=int(tinggi-1) 
mn=int(lebar-1) 
lebarBaru= int(lebar/2) 
tinggiBaru= int(tinggi/2) 
for i in np.arange(tinggiBaru): 
for j in np.arange(lebarBaru): 
    a=img.item(i,j,0)  
    b=img.item(i,j,1)  
    c=img.item(i,j,2)  
    d=img.item(i,mn,0)  
    e=img.item(i,mn,1)  
    f=img.item(i,mn,2)  
    img.itemset((i,j,0),d) 
    img.itemset((i,j,1),e) 
    img.itemset((i,j,2),f) 
    img.itemset((i,mn,0),a) 
    img.itemset((i,mn,1),b) 
    img.itemset((i,mn,2),c) 
    mn-=1 

我想在 python 中镜像图像而不使用 OpenCV 函数镜像图像

请您参考如下方法:

只需反转图像数组的一个(或两个)维度的方向。

使用 imageio 的示例,但对于类似的图像数组也应该同样有效,例如在 opencv 中:

import matplotlib.pyplot as plt 
import imageio 
im = imageio.imread('imageio:chelsea.png')  
 
fig, axs = plt.subplots(1, 3, sharey=True) 
 
axs[0].imshow(im) 
axs[1].imshow(im[:, ::-1, :]) 
axs[2].imshow(im[::-1, :, :])