将不同尺寸的四边形图像批量裁剪成圆形

batch crop quad images with diffenrent sizes to a circle

我有很多不同大小的行星图像,例如

它们都正好位于正方形图像的中间,但高度不同。 现在我想裁剪它们并使黑色边框透明。我试过 convert (ImageMagick 6.9.10-23) 这样的:

for i in planet_*.jpg; do
  nr=$(echo ${i/planet_/}|sed s/.jpg//g|xargs)
  convert $i -fuzz 1% -transparent black trans/planet_${nr}.png
done

但这会留下一些人工制品,例如:

是否有命令将所有图像裁剪成一个圆圈,这样地球就不会受到影响? (一定不能是imagemagick)。

我也可以想象一个解决方案,我会使用更大的 -fuzz 值,然后用黑色填充内行星圈中的所有透明像素。

这些都是行星,我要转换:download zip

这是使用来自 minEclosingCircle 的 Python Opencv 的一种方法。

输入:

import cv2
import numpy as np
import skimage.exposure

# read image
img = cv2.imread('planet.jpg')
h, w, c = img.shape

# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# get contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# get enclosing circle
center, radius = cv2.minEnclosingCircle(big_contour)
cx = int(round(center[0]))
cy = int(round(center[1]))
rr = int(round(radius))

# draw outline circle over input
circle = img.copy()
cv2.circle(circle, (cx,cy), rr, (0, 0, 255), 1)

# draw white filled circle on black background as mask
mask = np.full((h,w), 0, dtype=np.uint8)
cv2.circle(mask, (cx,cy), rr, 255, -1)

# antialias
blur = cv2.GaussianBlur(mask, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)
mask = skimage.exposure.rescale_intensity(blur, in_range=(127,255), out_range=(0,255))


# put mask into alpha channel to make outside transparent
imgT = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
imgT[:,:,3] = mask

# crop the image
ulx = int(cx-rr+0.5)
uly = int(cy-rr+0.5)
brx = int(cx+rr+0.5)
bry = int(cy+rr+0.5)
print(ulx,brx,uly,bry)
crop = imgT[uly:bry+1, ulx:brx+1]

# write result to disk
cv2.imwrite("planet_thresh.jpg", thresh)
cv2.imwrite("planet_circle.jpg", circle)
cv2.imwrite("planet_mask.jpg", mask)
cv2.imwrite("planet_transparent.png", imgT)
cv2.imwrite("planet_crop.png", crop)

# display it
cv2.imshow("thresh", thresh)
cv2.imshow("circle", circle)
cv2.imshow("mask", mask)
cv2.waitKey(0)

阈值图像:

输入圆圈:

蒙版图片:

透明图片:

裁剪后的透明图片:

要安装的包

sudo apt install python3-opencv python3-sklearn python3-skimage