#! -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont
def reverseColor(r,g,b):
return (255-r,255-g,255-b)
def grayscaleColor(r,g,b):
a=(r+g+b)/3
return (a,a,a)
text="Hello World!"
#textColor =(255,255,0) # RGB Yellow
#textBackgroundColor=(255,0,0) # RGB Red
textX=400 # text width im pixels
textY=100 # text height in pixels
textTopLeftX=0
textTopLeftY=0
image=Image.open("input.png")
(imgx,imgy)=image.size
font=ImageFont.load_default()
(width, height)=font.getsize(text)
textImage=font.getmask(text)
pixels=image.load()
for y in range(imgy):
by=int(height*(y-textTopLeftY)/textY +0.5)
if by>=0 and by<height:
for x in range(imgx):
bx=int(width*(x-textTopLeftX)/textX+0.5)
if bx>=0 and bx<width:
if textImage.getpixel((bx,by))==0: #text background
#pass #transparent background
#pixels[x,y[=textBackgroundColor
(r,g,b,a)=pixels[x,y]
pixels[x,y]=grayscaleColor(r,g,b)
else:
(r,g,b,a)=pixels[x,y]
pixels[x,y]=reverseColor(r,g,b)
image.save("output.png","PNG")
'Python' 카테고리의 다른 글
connect4.py (0) | 2015.02.08 |
---|---|
oscylator.py (0) | 2015.02.08 |
scriptinfo.py / scriptinfoUsage.py (0) | 2015.02.08 |
IMDB.py (0) | 2015.02.08 |
hanGame.py (0) | 2015.02.08 |