0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python Pillow を使って、画像のグレースケール/カラー を判別する

Last updated at Posted at 2023-04-17

このケースでは、誤差許容のしきい値を 0.001 としている。0.01では誤判定が出るので。。

isgray.py
from PIL import Image, ,ImageStat

def is_grayscale(fn):
	im = Image.open(fn).convert("RGB")
	stat = ImageStat.Stat(im)

	if abs(sum(stat.sum)/3 - stat.sum[0]) < (stat.sum[0] * 0.001) :
		return True #yes, grayscale
	else:
		return False #no,  colorimg


is_grayscale("hoge.jpg")

see also

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?