import numpy as np
from skimage import io
img = io.imread('https://istack.dev59.com/sbqcu.webp', as_grey=True)
left, right, top, bottom = 25, 25, 20, 20
crop = img[top: -bottom, left:- right]
threshold = .85
smiley = crop < threshold
rows, cols = np.nonzero(smiley)
rightmost = cols.max()
indices = np.nonzero(cols==rightmost)
for r, c, in zip(rows[indices], cols[indices]):
print('(%d, %d)' % (r + top, c + left))
(87, 184)
(88, 184)
(89, 184)
(90, 184)
(91, 184)
(92, 184)
(93, 184)
(94, 184)
(95, 184)
(96, 184)
(97, 184)
(98, 184)
threshold = .95
io.imshow(crop < threshold)
from PIL import Image
img = Image.open("c:\\smile.jpg")
img2 = img.crop((30, 26, 218, 165)) # remove the frame
img2.save("c:\\cropped-smile.jpg")
使用PIL修剪空白教如何去除圆形的白色部分。
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
im = Image.open("c:\\cropped-smile.jpg")
im = trim(im)
im.save("c:\\final-smile.jpg")
im = Image.open("c:\\final-sbqcu.jpg")
w, h = im.size