使用OpenCV和Python编写图像的难度
我使用下面的代码从一个文件夹中读取多个图像,从所有图像中获取特定的裁剪,并在最后写入它们。裁剪部分似乎不起作用,这意味着裁剪部分无法写入使用OpenCV和Python编写图像的难度,python,opencv,image-processing,Python,Opencv,Image Processing,我使用下面的代码从一个文件夹中读取多个图像,从所有图像中获取特定的裁剪,并在最后写入它们。裁剪部分似乎不起作用,这意味着裁剪部分无法写入 # import the necessary packages import cv2 import os, os.path #image path and valid extensions imageDir = "C:\\Users\\Sayak\\Desktop\\Training\\eye\\1" #specify your path here ima
# import the necessary packages
import cv2
import os, os.path
#image path and valid extensions
imageDir = "C:\\Users\\Sayak\\Desktop\\Training\\eye\\1" #specify your path here
image_path_list = []
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your vald extensions here
valid_image_extensions = [item.lower() for item in valid_image_extensions]
#create a list all files in directory and
#append files with a vaild extention to image_path_list
for file in os.listdir(imageDir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(imageDir, file))
#loop through image_path_list to open each image
for imagePath in image_path_list:
image = cv2.imread(imagePath)
# display the image on screen with imshow()
# after checking that it loaded
if image is not None:
cv2.imshow(imagePath, image)
elif image is None:
print ("Error loading: " + imagePath)
#end this loop iteration and move on to next image
continue
crop_img = image[200:400, 100:300]
cv2.imwrite('pic{:>05}.jpg'.format(imagePath), crop_img)
# wait time in milliseconds
# this is required to show the image
# 0 = wait indefinitely
# exit when escape key is pressed
key = cv2.waitKey(0)
if key == 27: # escape
break
# close any open windows
cv2.destroyAllWindows()
我已经修改了上面发布的代码。问题在于cv2.imwrite()中使用的字符串格式设置。第一个参数必须是文件应保存位置的绝对路径,但您的代码正在传入类似的内容。 jpg=>pic/home/ubuntu/Downloads/10_page.png.jpg。当我使用有效的文件名时,保存裁剪的图像根本没有问题
#loop through image_path_list to open each image
for i,imagePath in enumerate(image_path_list):
image = cv2.imread(imagePath)
# display the image on screen with imshow()
# after checking that it loaded
if image is not None:
cv2.imshow(imagePath, image)
elif image is None:
print ("Error loading: " + imagePath)
#end this loop iteration and move on to next image
continue
# Please check if the size of original image is larger than the pixels you are trying to crop.
(height, width, channels) = image.shape
if height >= 400 and width >= 300:
plt.imshow(image)
plt.title('Original')
plt.show()
crop_img = image[100:400, 100:400]
# Problem lies with this path. Ambiguous like pic/home/ubuntu/Downloads/10_page.png.jpg
print('pic{:>05}.jpg'.format(imagePath))
# Try a proper path. A dirname + file_name.extension as below, you will have no problem saving.
new_image = os.path.join(imageDir,'{}.png'.format(str(i)))
cv2.imwrite(new_image, crop_img)
plt.imshow(crop_img)
plt.title('Cropped')
plt.show()
什么是
裁剪部分似乎不工作
,是否有任何错误?或者根本不写入裁剪的图像?是的,根本不写入裁剪的图像。对不起,我不清楚那个部分。后编辑。然后请检查您传递给cv2.imwrite()
的路径是否有效?这是调试的基础,不是吗?如果没有写入文件,可能是因为权限或文件名不可靠导致文件写入失败,或者要写入的数据不正确。您有能力对此进行调试:打印文件名以检查其是否有效以及写入的路径是否具有写入权限,或者显示裁剪后的图像以确保其良好。应该教会人们调试,而不是编程。检查权限和文件名。没问题。