[英]image scaling in loop using python OpenCV
I am new to Python and I am having a really hard time figuring out how to transform what I got here into a loop. 我是Python新手,我很难搞清楚如何将我在这里转换成循环。 As you can see here I am doing a basic image scaling to 3 different images (from mask_0001.png to mask_0003.png), but I will have more than 100 to go later on, so I will have to do it in a loop I guess. 正如你在这里看到的,我正在做一个基本的图像缩放到3个不同的图像(从mask_0001.png到mask_0003.png),但我将有超过100个以后去,所以我将不得不在循环中做到这一点我猜测。 (from 0004.png to 0150.png ) (从0004.png到0150.png)
Here is the code I just wrote: 这是我刚写的代码:
import cv2
import numpy as np
img = cv2.imread("C://Users//user//Desktop//research//images//Mask//set1//set1_mask_0001.png")
res = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
cv2.imwrite('0.25_mask_set1_0001.png', res)
img = cv2.imread("C://Users//user//Desktop//research//images//Mask//set1//set1_mask_0002.png")
res = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
cv2.imwrite('0.25_mask_set1_0002.png', res)
img = cv2.imread("C://Users//user//Desktop//research//images//Mask//set1//set1_mask_0003.png")
res = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
cv2.imwrite('0.25_mask_set1_0003.png', res)
Thanks! 谢谢!
This loops i
from 1 to 150, setting the mask to i
. 这将循环i
从1到150,将掩码设置为i
。
Python 2 Python 2
for i in range(1,151):
img = cv2.imread("C://Users//user//Desktop//research//images//Mask//set1//set1_mask_" + str(i).zfill(4) +".png")
res = cv2.resize(img,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
cv2.imwrite('0.25_mask_set1_' + str(i).zfill(4) + '.png', res)
Python 3 Python 3
Instead of using str(i).zfill(4)
, try format(i, '04d')
而不是使用str(i).zfill(4)
,尝试format(i, '04d')
You should verify the behaviour, particularly with my use of i
in cv2.imwrite
to make sure it works with your problem 您应该验证行为,特别是在cv2.imwrite
使用i
以确保它可以解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.