Python Pil将GreyScale Tif更改为RGB [英] Python Pil Change GreyScale Tif To RGB

查看:239
本文介绍了Python Pil将GreyScale Tif更改为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个灰度TIF文件.我需要以一种可以使用它的方式将其转换为RGB/进行读取.

I have a greyscale TIF File. I need to convert it to RGB/ read from it in a way I can work with it.

img = Image.open(GIF_FILENAME)
rgbimg = img.convert('RGB')
for i in range(5):
    print rgbimg.getpixel((i, 0))

convert.("RGB")会自动生成所有内容(255,255,255),即使该图片是一个非常暗的,主要是黑色的图片.

The convert.("RGB") will automatically make everything (255,255,255) even though the picture is a really dark mostly black picture.

如果我只读取灰度数字,我会得到大约1400到1900之间的数字.

If I just read the greyscale numbers I get numbers from around 1400 to 1900.

我还需要将图片的副本另存为RGB Jpeg. 有问题的图片:[此处]: http://imgur.com/kEwfFs3

I need to also save a copy of the picture as a RGB Jpeg. Picture in question: [Here]: http://imgur.com/kEwfFs3

我将如何做呢?

推荐答案

有关:

img = Image.open(GIF_FILENAME)
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
rgbimg.save('foo.jpg')

创建了一个测试:

from PIL import Image
from collections import defaultdict
import pprint

img = Image.open("kEwfFs3.png")
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)

found_colors = defaultdict(int)
for x in range(0, rgbimg.size[0]):
    for y in range(0, rgbimg.size[1]):
        pix_val = rgbimg.getpixel((x, y))
        found_colors[pix_val] += 1 
pprint.pprint(dict(found_colors))

rgbimg.save('kEwfFs3.jpg')

然后输出:

{(0, 0, 0, 255): 747802,
 (1, 1, 1, 255): 397,
 (2, 2, 2, 255): 299,
 (3, 3, 3, 255): 255,
 (4, 4, 4, 255): 221,
 (5, 5, 5, 255): 200,
 (6, 6, 6, 255): 187,
 (7, 7, 7, 255): 138,
 (8, 8, 8, 255): 160,
 (9, 9, 9, 255): 152,
 (10, 10, 10, 255): 122,
 (11, 11, 11, 255): 116,
 (12, 12, 12, 255): 144,
 (13, 13, 13, 255): 117,
 (14, 14, 14, 255): 117,
 (15, 15, 15, 255): 102,
 (16, 16, 16, 255): 119,
 (17, 17, 17, 255): 299641,
 (18, 18, 18, 255): 273,
 (19, 19, 19, 255): 233,
.................... etc .......
.................... etc .......
 (249, 249, 249, 255): 616,
 (250, 250, 250, 255): 656,
 (251, 251, 251, 255): 862,
 (252, 252, 252, 255): 1109,
 (253, 253, 253, 255): 1648,
 (254, 254, 254, 255): 2964175}

您所期望的是什么. 您的输出是否不同?

Which is what you would expect. Is your output different?

这篇关于Python Pil将GreyScale Tif更改为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆