2017-11-29 17 views
1

this nice answer의 스크립트로 시작했습니다. "RGB"에 대해서는 문제없이 작동하지만 8 비트 그레이 스케일 "L"및 1 비트 흑백/"1"PIL 이미지 모드는 검은 색으로 보입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?PIL을 사용하여 다국어 텍스트 그리기 및 1 비트 및 8 비트 비트 맵으로 저장

from PIL import Image, ImageDraw, ImageFont 
import numpy as np 

w_disp = 128 
h_disp = 64 
fontsize = 32 
text  = u"你好!" 

for imtype in "1", "L", "RGB": 
    image = Image.new(imtype, (w_disp, h_disp)) 
    draw = ImageDraw.Draw(image) 
    font = ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf", fontsize) 
    w, h = draw.textsize(text, font=font) 
    draw.text(((w_disp - w)/2, (h_disp - h)/2), text, font=font) 
    image.save("NiHao! 2 " + imtype + ".bmp") 
    data = np.array(list(image.getdata())) 
    print data.shape, data.dtype, "min=", data.min(), "max=", data.max() 

출력 :

(8192,) int64 min= 0 max= 0 
(8192,) int64 min= 0 max= 0 
(8192, 3) int64 min= 0 max= 255 

imtype = "1":enter image description here

imtype = "L":enter image description here

imtype = "RGB":enter image description here

답변

2

업데이트 :

This answer.convert() 대신 PIL의 Image.point() 메서드를 사용하는 것이 좋습니다.

모든 것은 다음과 같습니다 ORIGINAL

from PIL import Image, ImageDraw, ImageFont 
import numpy as np 
w_disp = 128 
h_disp = 64 
fontsize = 32 
text  = u"你好!" 

imageRGB = Image.new('RGB', (w_disp, h_disp)) 
draw = ImageDraw.Draw(imageRGB) 
font = ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf", fontsize) 
w, h = draw.textsize(text, font=font) 
draw.text(((w_disp - w)/2, (h_disp - h)/2), text, font=font) 

image8bit = imageRGB.convert("L") 
imageRGB.save("NiHao! RGB.bmp") 
image8bit.save("NiHao! 8bit.bmp") 

imagenice_80 = image8bit.point(lambda x: 0 if x < 80 else 1, mode='1') 
imagenice_128 = image8bit.point(lambda x: 0 if x < 128 else 1, mode='1') 
imagenice_80.save("NiHao! nice 1bit 80.bmp") 
imagenice_128.save("NiHao! nice 1bit 128.bmp") 

NiHao! RGBNiHao! 8 bitNiHao! 1 bit 80NiHao! 1 bit 128


:

그것은 트루 타입 글꼴처럼 보이는 아무것도 작동하지 않으을 RGB보다 작습니다.

PIL의 .convert() 방법을 사용하여 이미지를 다운 컨버전 할 수 있습니다. RGB 화상을 시작으로

,이 제공 :

image.convert("1"):enter image description here

image.convert("L"):enter image description here

는 그레이 스케일이 잘 작동 8 비트로 변환하지만, 트루 타입 글꼴, 또는 어떤 글꼴로 시작 그레이 스케일에 기초하여, 1- 비트 변환은 항상 거칠게 보일 것이다.

1 비트 이미지를보기 좋게하려면 디지털 온/오프 디스플레이 용으로 설계된 1 비트 비트 맵 중국어 글꼴로 시작해야 할 수 있습니다.