pil相关剧本:画面没有两极分化 [英] pil related script : picture not getting polarized

查看:139
本文介绍了pil相关剧本:画面没有两极分化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写的一个脚本,用于将图片分成两种颜色。我这样做是因为所有看似黑色和白色的迷宫都不是只读了两种颜色而是一堆它们。所以我想为什么不写一个脚本自己将图像双极化。

This is a script I wrote to to divide pictures into two colors .I did this because all the seemingly black and white mazes weren't read as just two colors but a bunch of them.So I thought why not write a script to bipolarize the image myself.

脚本如下:

import sys
import Image

file=Image.open(sys.argv[1])
file=file.convert('RGB')
w,h=file.size
color={}
for i in range(w):
    for j in range(h):
        aux=file.getpixel((i,j))
        if aux >(200,200,200):
            file.putpixel((i,j),(255,0,0))
        else:
            file.putpixel((i,j),(0,0,0))


file.save(sys.argv[1])

问题
当以下脚本尝试读取上述脚本结果中显示的颜色时

The problem when the following script tries to read the colors present in the result of the above script

import sys
import Image

file=Image.open(sys.argv[1])
file=file.convert('RGB')
w,h=file.size
color={}
for i in range(w):
    for j in range(h):
        aux=file.getpixel((i,j))
        if aux not in color:
            color[aux]=1
        else:
            color[aux]+=1


print "file stat"
print color

图片似乎没有偏振为两种颜色,即使它在视觉上是这样的

The picture doesn't appear to polarized into just two colors, even if it does so visually

发生了什么事?

推荐答案

我认为这里的问题是您正在使用的源文件是压缩的(可能是.jpeg文件)。当您将 sys.argv [1] 传递给 file.save PIL决定是否压缩基于极化的图像在文件扩展名上。因此,如果 sys.argv [1] ==test.jpg,那么写入磁盘的文件将被压缩。

I think that the problem here is that the source file you are using is compressed (probably a .jpeg file). When you pass sys.argv[1] to file.save PIL decides whether to compress the "polarized" image based on the file extension. So if sys.argv[1]=="test.jpg" then the file written to disk will be compressed.

压缩会导致你期待的纯红色和黑色之间的颜色。

Compression can induce colours in between the pure red and black that you are expecting.

一个简单的方法这个问题是使用非压缩格式(例如.png)将输出写入文件。

An easy way around this problem is to write the output to file using an uncompressed format such as .png instead.

顺便提一句,这种极化通常被称为阈值化和你生成的双色调图像是一个阈值图像。

Incidentally, this "polarization" is usually referred to as "thresholding" and the two-tone image you are generating is a thresholded image.

另外,我认为使用这个词不是一个好主意/ code>作为变量名,因为它用于其他事物,例如 http://docs.python.org/2/library/functions.html#file

下面的代码版本演示了让PIL写入阈值图像的压缩版本与未压缩版本之间的区别:

The version of your code below demonstrates the difference between letting PIL write a compressed version of your thresholded image, compared to an uncompressed version:

import sys
from PIL import Image

im=Image.open("test.jpg")
im=im.convert('RGB')
w,h=im.size
color={}
for i in range(w):
    for j in range(h):
        aux=im.getpixel((i,j))
        if aux >(200,200,200):
            im.putpixel((i,j),(255,0,0))
        else:
            im.putpixel((i,j),(0,0,0))


im.save("test_copy.jpg")
im.save("test_copy.png")


im=Image.open("test_copy.jpg")
im=im.convert('RGB')
w,h=im.size
color={}
for i in range(w):
    for j in range(h):
        aux=im.getpixel((i,j))
        if aux not in color:
            color[aux]=1
        else:
            color[aux]+=1


print "Compressed file stat"
print color

im=Image.open("test_copy.png")
im=im.convert('RGB')
w,h=im.size
color={}
for i in range(w):
    for j in range(h):
        aux=im.getpixel((i,j))
        if aux not in color:
            color[aux]=1
        else:
            color[aux]+=1


print "Uncompressed file stat"
print color

这篇关于pil相关剧本:画面没有两极分化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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