简体   繁体   中英

how to save PIL and skimage processed images

Please help me. How can I save the following processed images?

I was able to process an image with the code below but don't know how to save the output. cv2.imwrite() and Pil's Image.save() are not working.

For Image.save() when I try to save canny, it says AttributeError: 'list' object has no attribute 'save'. And when I try to save img_edge, it says AttributeError: 'numpy.ndarray' object has no attribute 'save'

For cv2.imwrite(), canny says error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'imwrite'

Overload resolution failed:

  • img is not a numpy array, neither a scalar
  • Expected Ptr<cv::UMat> for argument 'img'

while img_edge says error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:730: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite_'

code

from PIL import Image
from skimage import measure
from skimage.io import imread
from skimage.filters import sobel
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
import cv2

img = Image.open('0b_5kg--1-_jpg.rf.952c6c43d86afae6c428073873377a28.jpg')
#img.show()

img = imread('0b_5kg--1-_jpg.rf.952c6c43d86afae6c428073873377a28.jpg')

gray = rgb2gray(img)

img_edge = sobel(gray)

canny = measure.find_contours(img_edge, 0.2)

You can draw the contours you found and then store the image using matplotlib, for instance like this:

from PIL import Image
from skimage import measure
from skimage.io import imread
from skimage.filters import sobel
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
import cv2

img = Image.open('0b_5kg--1-_jpg.rf.952c6c43d86afae6c428073873377a28.jpg')
#img.show()

img = imread('0b_5kg--1-_jpg.rf.952c6c43d86afae6c428073873377a28.jpg')

gray = rgb2gray(img)

img_edge = sobel(gray)

canny = measure.find_contours(img_edge, 0.2)

fig, ax = plt.subplots()
ax.imshow(img_edge, cmap=plt.cm.gray)

for contour in canny:
    ax.plot(contour[:, 1], contour[:, 0], linewidth=2)

ax.set_xticks([])
ax.set_yticks([])
plt.savefig("test.png",bbox_inches='tight', pad_inches=0)

Reference for the extension of your code:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM