首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用有限的阴影将图像转换为灰度?

使用有限的阴影将图像转换为灰度?
EN

Stack Overflow用户
提问于 2021-02-23 16:18:30
回答 2查看 1.4K关注 0票数 1

我希望将图像转换为灰度,但希望将阴影的数量限制在4-5。这是因为我试图创造一个分层的‘剪纸’效果的图像,以便我可以使用它作为一个基础,我正在工作的一些艺术作品,我有5阴影的黑白工作。

如果您对如何使用Python实现这一目标有更好的想法,我将全神贯注。如果Python中已经存在这样的过滤器,这将是非常方便的,但我似乎找不到任何东西。非常感谢。

项目的最终结果是如下所示:图像

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-23 17:55:16

您可以使用PIL对此进行量化:

这方面:

就像这样:

代码语言:javascript
复制
#!/usr/bin/env python3

from PIL import Image

# Load image and make greyscale
im = Image.open('artistic-swirl.jpg').convert('L')

# Quantize down to 5 shades and save
qu = im.quantize(5)
qu.save('result.png')

print(f'Colours: {qu.getcolors()}')

样本输出

您可以看到产生的5种颜色(调色板指数)的列表及其出现的频率如下:

代码语言:javascript
复制
Colours: [(32047, 0), (34515, 1), (59838, 2), (70181, 3), (53419, 4)]

您也可以使用ImageMagick检查颜色如下:

代码语言:javascript
复制
magick identify -verbose result.png

样本输出

代码语言:javascript
复制
Image:
  Filename: result.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 500x500+0+0
  Units: Undefined
  Colorspace: sRGB
  Type: Grayscale
  Base type: Undefined
  Endianness: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
  Channel statistics:
    Pixels: 250000
    Red:
      min: 106  (0.415686)
      max: 166 (0.65098)
      mean: 133.412 (0.523186)
      median: 139 (0.545098)
      standard deviation: 19.4166 (0.0761436)
      kurtosis: -0.979496
      skewness: 0.129338
      entropy: 0.972589
    Green:
      min: 106  (0.415686)
      max: 166 (0.65098)
      mean: 133.412 (0.523186)
      median: 139 (0.545098)
      standard deviation: 19.4166 (0.0761436)
      kurtosis: -0.979496
      skewness: 0.129338
      entropy: 0.972589
    Blue:
      min: 106  (0.415686)
      max: 166 (0.65098)
      mean: 133.412 (0.523186)
      median: 139 (0.545098)
      standard deviation: 19.4166 (0.0761436)
      kurtosis: -0.979496
      skewness: 0.129338
      entropy: 0.972589
  Image statistics:
    Overall:
      min: 106  (0.415686)
      max: 166 (0.65098)
      mean: 133.412 (0.523186)
      median: 139 (0.545098)
      standard deviation: 19.4166 (0.0761436)
      kurtosis: -0.979485
      skewness: 0.129339
      entropy: 0.972589
  Colors: 5
  Histogram:
         53419: (106,106,106) #6A6A6A srgb(106,106,106)
         70181: (125,125,125) #7D7D7D grey49
         59838: (139,139,139) #8B8B8B srgb(139,139,139)
         34515: (153,153,153) #999999 grey60
         32047: (166,166,166) #A6A6A6 grey65
  Colormap entries: 5
  Colormap:
    0: (166,166,166,1) #A6A6A6FF grey65
    1: (153,153,153,1) #999999FF grey60
    2: (139,139,139,1) #8B8B8BFF srgba(139,139,139,1)
    3: (125,125,125,1) #7D7D7DFF grey49
    4: (106,106,106,1) #6A6A6AFF srgba(106,106,106,1)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 500x500+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2022-06-30T13:25:02+00:00
    date:modify: 2022-06-30T13:25:02+00:00
    png:IHDR.bit-depth-orig: 4
    png:IHDR.bit_depth: 4
    png:IHDR.color-type-orig: 3
    png:IHDR.color_type: 3 (Indexed)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 500, 500
    png:PLTE.number_colors: 5
    png:sRGB: intent=0 (Perceptual Intent)
    signature: ff62d5806f38bc0228513619c9822015bc70ee8466714b0317441e89ff3b815b
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 15193B
  Number pixels: 250000
  Pixel cache type: Memory
  Pixels per second: 90.1415MP
  User time: 0.000u
  Elapsed time: 0:01.002
  Version: ImageMagick 7.1.0-33 Q16-HDRI arm 20040 https://imagemagick.org

关键词:Python,图像处理,量化,减少颜色。

票数 1
EN

Stack Overflow用户

发布于 2021-02-23 19:17:51

下面是如何在Python/OpenCV中直接对5个灰度进行量化。

输入:

代码语言:javascript
复制
import cv2
import numpy as np

# arguments
num_colors = 5

# read input
img = cv2.imread("bear2.png")

# convert to gray as float in range 0 to 1
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = gray.astype(np.float32)/255

# quantize and convert back to range 0 to 255 as 8-bits
result = 255*np.floor(gray*num_colors+0.5)/num_colors
result = result.clip(0,255).astype(np.uint8)

# save result
cv2.imwrite('bear2_gray5.png', result)

cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66336939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档