Skip to content

Instantly share code, notes, and snippets.

@SS1031
Created December 21, 2013 13:25
Show Gist options
  • Save SS1031/8069262 to your computer and use it in GitHub Desktop.
Save SS1031/8069262 to your computer and use it in GitHub Desktop.
画像を減色して保存するスクリプト
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import sys
import numpy as np
TARGET_DIR = '../dataset/bijin/'
##############################################################################
# @brief 入力画像を減色した画像を返す
# @param numpy.ndarray img 画像ファイル
# @return numpy.ndarray d_img 減色画像ファイル
##############################################################################
def decleaseColor(img):
print type(img)
rgb = cv2.split(img)
for col in rgb:
idx = np.where(col < 64)
col[idx] = 32
idx = np.where((64 <= col) & (col < 128))
col[idx] = 96
idx = np.where((128 <= col) & (col < 196))
col[idx] = 160
idx = np.where(196 <= col)
col[idx] = 224
d_img = cv2.merge(rgb)
return d_img
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'ERROR : input file name'
sys.exit(-1)
img = cv2.imread(TARGET_DIR + sys.argv[1])
# RGBを4*4*4=64色に減色
d_img = decleaseColor(img)
cv2.imwrite('d_' + sys.argv[1], d_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment