要创建类似施乐的效果,一种方法是将图像转换为灰度,然后减少色彩空间。
def xeroxFilter(imgPath, colorSpaceReduction=8, rotate=False, fillColor=(255,255,255)):
'''Takes image as input and returns a xerox like image of the input image.
impPath: Name of Image(keep the images in the same folder as this script)
colorSpaceReduction: Reducing the color space. Higher the value, Higher the reduction.
rotate: Rotate to the image
fillColor: Specify the RGB value of the background color after roatation
'''
print("Original Image")
cv2_imshow(cv2.imread(imgPath))
print('GreyScale')
cv2_imshow(cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE))
print('Color Space Reduced')
color_reduced_img = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE) // colorSpaceReduction * colorSpaceReduction + colorSpaceReduction // 2
cv2_imshow(color_reduced_img)
file_name = 'ColorSpaceReduced-'+imgPath.split('.')[0]+'.png'
cv2.imwrite(file_name, color_reduced_img)
if rotate:
print("Rotated")
img = Image.open(file_name)
rgb_img = Image.new("RGBA", img.size)
rgb_img.paste(img)
rotated_img = rgb_img.rotate(random.randint(-25,25), expand = 1, fillcolor = fillColor)
display(rotated_img)
rotated_img.save('Rotated-'+file_name)
print("Original Image")
cv2_imshow(cv2.imread(imgPath))
xeroxFilter('MCRV7.jpg', rotate=True)
色彩空间缩减代码 (credits @elizer )
将第一张图像传入脚本时的输出如下所示:
[
1