像物反射

2024-05-15 07:59:49 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我想在图像中反映与此图像相同的对象[在此处输入图像描述][1]

[1]:https://i.stack.imgur.com/N9J3I.jpg我怎样才能得到这样的结果


Tags: 对象https图像comstackjpgimgurn9j3i
2条回答

下面是在Python/OpenCV中进行反射的一种方法

一个翻转图像。然后制作一个垂直渐变(渐变)图像,并将其放入翻转图像的alpha通道。然后连接原始图像和翻转图像

输入:

enter image description here

import cv2
import numpy as np

# set top and bottom opacity percentages
top = 85
btm = 15

# load image
img = cv2.imread('bear2.png')
hh, ww = img.shape[:2]

# flip the input
flip = np.flip(img, axis=0)

# add opaque alpha channel to input
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)

# make vertical gradient that is bright at top and dark at bottom as alpha channel for the flipped image
gtop = 255*top//100
gbtm = 255*btm//100
grady = np.linspace(gbtm, gtop, hh, dtype=np.uint8)
gradx = np.linspace(1, 1, ww, dtype=np.uint8)
grad = np.outer(grady, gradx)
grad = np.flip(grad, axis=0)
# alternate method
#grad = np.linspace(0, 255, hh, dtype=np.uint8)
#grad = np.tile(grad, (ww,1))
#grad = np.transpose(grad)
#grad = np.flip(grad, axis=0)

# put the gradient into the alpha channel of the flipped image
flip = cv2.cvtColor(flip, cv2.COLOR_BGR2BGRA)
flip[:,:,3] = grad

# concatenate the original and the flipped versions
result = np.vstack((img, flip))

# save output
cv2.imwrite('bear2_vertical_gradient.png', grad)
cv2.imwrite('bear2_reflection.png', result)

# Display various images to see the steps
cv2.imshow('flip',flip)
cv2.imshow('grad',grad)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

倾斜(渐变)图像:

enter image description here

结果:

enter image description here

OpenCV可能没有很好的解决方案,请仔细看看枕头

    from PIL import Image, ImageFilter

def drop_shadow(image, iterations=3, border=8, offset=(5,5), background_colour=0xffffff, shadow_colour=0x444444):
    shadow_width  = image.size[0] + abs(offset[0]) + 2 * border
    shadow_height = image.size[1] + abs(offset[1]) + 2 * border
    
    shadow = Image.new(image.mode, (shadow_width, shadow_height), background_colour)
    
    shadow_left = border + max(offset[0], 0)
    shadow_top  = border + max(offset[1], 0)
    shadow.paste(shadow_colour, [shadow_left, shadow_top, shadow_left + image.size[0], shadow_top  + image.size[1]])
    
    for i in range(iterations):
        shadow = shadow.filter(ImageFilter.BLUR)

    img_left = border - min(offset[0], 0)
    img_top  = border - min(offset[1], 0)
    shadow.paste(image, (img_left, img_top))

    return shadow
    

drop_shadow(Image.open('boobs.jpg')).save('shadowed_boobs.png')

相关问题 更多 >