带白色圆圈背景使图像更平滑 [英] Make an image smoother with White Circled Background

查看:167
本文介绍了带白色圆圈背景使图像更平滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Python完成该项目
我有这个法师,我想让我上传的每个图像都像下面的图像
如果需要一些边框以使其圆,则每个图像都应像这样用圆圈圈起来圈添加边框,否则不要添加任何边框并添加灰色背景

I want this project to be Done in Python
I have this mage , I want to make each image i upload like image below
Each image should be Circled like this if some border is needed to make it Circle add border otherwise don't add any border and add gray background

推荐答案

这是在Python OpenCV中执行此操作的一种方法.

Here is one way to do that in Python OpenCV.

  • 阅读输入内容
  • 计算输入的最大尺寸,偏移量和中心
  • 创建最大尺寸加上填充的白色图像
  • 将输入图像插入白色图像的中心
  • 创建与白色图像尺寸相同的灰色背景图像
  • 在灰色背景的中心绘制一个直径等于最大尺寸的黑色圆圈
  • 模糊黑色圆圈以创建阴影
  • 在黑色图像的中心创建一个直径等于最大尺寸的白色圆圈
  • 将白色背景上的图像与背景模糊的黑色圆圈混合在一起以形成结果
  • 保存结果
  • Read the input
  • Compute max dimension, offsets and center of input
  • Create white image of max dimension plus padding
  • Insert input image into center of white image
  • Create gray background image of same dimension as white image
  • Draw a black circle of diameter equal to max dimension in the center of gray background
  • Blur the black circle to create the drop shadow
  • Create a white circle of diameter equal to max dimension in center of black image
  • Blend the image on white background with blurred black circle on background to form the result
  • Save the result

输入:

import cv2
import numpy as np

# load image and get maximum dimension
img = cv2.imread("radio_skull.jpg")
hh, ww = img.shape[:2]
maxwh = max(ww,hh)
offx = (maxwh - ww) // 2
offy = (maxwh - hh) // 2
cx = maxwh // 2
cy = maxwh // 2
pad = 10
pad2 = 2*pad

# create white image of size maxwh plus 10 pixels padding all around
white = np.full((maxwh+pad2, maxwh+pad2, 3), (255,255,255), dtype=np.uint8)

# put input img into center of white image
img_white = white.copy()
img_white[offy+pad:offy+pad+hh, offx+pad:offx+pad+ww] = img

# create light gray background image with 10 pixel padding all around
bckgrnd = np.full((maxwh+pad2,maxwh+pad2,3), (192,192,192), dtype=np.uint8)

# create black circle on background image for drop shadow
cv2.circle(bckgrnd, (cx+pad,cy+pad), cx, (0,0,0), -1)

# blur black circle
bckgrnd = cv2.GaussianBlur(bckgrnd, (25,25), 0)

# create white circle on black background as mask
mask = np.zeros_like(img_white)
cv2.circle(mask, (cx+pad,cy+pad), cx, (255,255,255), -1)

# use mask to blend img_white and bckgrnd
img_white_circle = cv2.bitwise_and(img_white, mask)
bckgrnd_circle = cv2.bitwise_and(bckgrnd, 255-mask)
result = cv2.add(img_white_circle, bckgrnd_circle)

# write result to disk
cv2.imwrite("radio_skull_img_white.jpg", img_white)
cv2.imwrite("radio_skull_background.jpg", bckgrnd)
cv2.imwrite("radio_skull_mask.jpg", mask)
cv2.imwrite("radio_skull_img_white_circle.jpg", img_white_circle)
cv2.imwrite("radio_skull_bckgrnd_circle.jpg", bckgrnd_circle)
cv2.imwrite("radio_skull_result.jpg", result)

# display it
cv2.imshow("img_white", img_white)
cv2.imshow("bckgrnd", bckgrnd)
cv2.imshow("mask", mask)
cv2.imshow("img_white_circle", img_white_circle)
cv2.imshow("bckgrnd_circle", bckgrnd_circle)
cv2.imshow("result", result)
cv2.waitKey(0)

在白色背景上输入:

背景上出现黑圈:

面具:

白色的蒙版图像:

被掩盖的黑色圆圈:

结果:

这篇关于带白色圆圈背景使图像更平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆