python Color By Number 图片生成像素图

#!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image
import math
from array import *
import json

class PixelData(object):
    R=0
    G=0
    B=0
    Key=""
    number=0

        

def make_regalur_image(img, size = (50, 50)):
    return img.resize(size).convert('RGBA')

def greyColor(color):
    R=color[0]
    G=color[1]
    B=color[2]
    A=color[3]

    if(A<255):
        A=0
        R=0
        G=0
        B=0
    else:
        A=255
    #value=int(R*0.3+G*0.59+B*0.11)
    #R=value
    #G=value
    #B=value



    return R,G,B,A


def compare(dict,R,G,B):
    minDis=255
    targetKey=None
    for key in dict:
        pixelData=dict[key]
        disR=pixelData.R-R
        disG=pixelData.G-G
        disB=pixelData.B-B
        value=math.sqrt(disR*disR+disG*disG+disB*disB)
        if value<minDis:
            minDis=value
            targetKey=key
    if minDis<20:
        return targetKey
    return None

def pixelSort(pixelData1,pixelData2):
    if pixelData1.number>pixelData2.number:
        return -1
    elif pixelData1.number<pixelData2.number:
        return 1
    else:
        return 0

def getColorDict(pixL,w,h):
    colorsDict={}
    for j in xrange(0,h):
        for i in xrange(0,w):
            R=pixL[i,j][0]
            G=pixL[i,j][1]
            B=pixL[i,j][2]
            A=pixL[i,j][3]
            if A==255:
                targetKey=compare(colorsDict,R,G,B)
                if targetKey==None:
                    key=str(R)+"_"+str(G)+"_"+str(B)
                    pixelData=PixelData()
                    pixelData.key=key
                    pixelData.R=R
                    pixelData.G=G
                    pixelData.B=B
                    pixelData.number=1
                    colorsDict[key]=pixelData
                else:
                    pixelData=colorsDict[targetKey]
                    pixelData.number=pixelData.number+1
                    colorsDict[targetKey]=pixelData

    return colorsDict

def getSortValues(colorsDict):
    values=[]
    for key in colorsDict:
        values.append(colorsDict[key])
    values.sort(pixelSort)
    return values

def getIndexAndDistance(sortValues):
    index=len(sortValues)
    targetNumber=20
    distance=30

    while index>targetNumber:
        for pixelData in sortValues:
            if pixelData.number<distance:
                index=sortValues.index(pixelData)
                break
        if index>targetNumber:
            distance=distance+5
    if index<=5:
        distance=20
        index=len(sortValues)
        while index>targetNumber:
            for pixelData in sortValues:
                if pixelData.number<distance:
                    index=sortValues.index(pixelData)
                    break
                if index>targetNumber:
                    distance=distance+5
    if index<=5:
        distance=10
        index=len(sortValues)
        while index>targetNumber:
            for pixelData in sortValues:
                if pixelData.number<distance:
                    index=sortValues.index(pixelData)
                    break
                if index>targetNumber:
                    distance=distance+5
    return index,distance

def getColorimeterWithList(mList,R,G,B):
    minDis=255
    mIndex=-1
    for value in mList:
        lR=value[0]
        lG=value[1]
        lB=value[2]

        disR=lR-R
        disG=lG-G
        disB=lB-B

        sqrtValue=math.sqrt(disR*disR+disG*disG+disB*disB)
        if sqrtValue<minDis:
            minDis=sqrtValue
            mIndex=mList.index(value)

    if(minDis<64):
        return mIndex
    else:
        return -1


def operationImage(filename):
    orginName=filename
    filename=filename+".png"
    img=make_regalur_image(Image.open(filename))
    pixL=img.load()
    print type(pixL)

    w,h=img.size
    for j in xrange(0,h):
        for i in xrange(0,w):
            pixL[i,j]=greyColor(pixL[i,j])

    savePath="/new_image/"+filename
    img.save(savePath);        


    colorsDict=getColorDict(pixL,w,h)
    print type(colorsDict)
    
    sortValues=[]
    for key in colorsDict:
        sortValues.append(colorsDict[key])
    sortValues.sort(pixelSort)
    
    index,distance=getIndexAndDistance(sortValues)
    print index,distance

    numberDict={}
    colorsValueList=[]
    if len(sortValues)>=index:
        for pixelData in sortValues:
            R=pixelData.R
            G=pixelData.G
            B=pixelData.B
            mList=[R,G,B]
            xindex=sortValues.index(pixelData)
            if xindex<=index:
                colorsValueList.append(mList)
                mkey=str(xindex)
                numberDict[mkey]=mList
    else:
        for pixelData in sortValues:
            xindex=len(colorsValueList)
            R=pixelData.R
            G=pixelData.G
            B=pixelData.B
            mList=[R,G,B]
            colorsValueList.append(mList)
            numberDict[str(xindex)]=mList

    numberList=[]
    for j in xrange(0,h):
        rowList=[]
        for i in xrange(0,w):
            R=pixL[i,j][0]
            G=pixL[i,j][1]
            B=pixL[i,j][2]
            A=pixL[i,j][3]
            if A<255:
                R=0
                G=0
                B=0
                A=0
                rowList.append(-1)
            else:
                rate=A/255.0
                R=int(R*rate)
                G=int(G*rate)
                B=int(B*rate)
                A=255
                mIndex=getColorimeterWithList(colorsValueList,R,G,B)
                if mIndex>=0:
                    rowList.append(mIndex)
                else:
                    mIndex=len(colorsValueList)
                    rowList.append(mIndex)
                    mList=[R,G,B]
                    colorsValueList.append(mList)
                    mKey=str(mIndex)
                    numberDict[mKey]=mList

            greyValue=int(R*0.3+G*0.59+B*0.11)
            # pixL[i,j]=(R,G,B,A)
            pixL[i,j]=(greyValue,greyValue,greyValue,A)

        numberList.append(rowList)

    jsonDict={}
    jsonDict["palette"]=numberDict
    jsonDict["pixel_map"]=numberList

    jsonFile=orginName+".json"
    print jsonFile
    with open(jsonFile,"w") as f:
        f.write(json.dumps(jsonDict))
        f.close()


    newImg=Image.new("RGBA",(w*2,h*2))
    pixNew=newImg.load()
    newW,newH=newImg.size
    for j in xrange(0,newH):
        for i in xrange(0,newW):
            x=int(i/2)
            y=int(j/2)
            pixNew[i,j]=pixL[x,y]

    savePath="./new_image/"+filename
    newImg.save(savePath)
            



            
            


for i in xrange(1,65):
    filename="animal"+str(i)
    operationImage(filename)
for i in xrange(1,32):
    filename="bird"+str(i)
    operationImage(filename)
for i in xrange(1,38):
    filename="xmas"+str(i)
    operationImage(filename)
for i in xrange(1,21):
    filename="emoji"+str(i)
    operationImage(filename)
for i in xrange(1,15):
    filename="flower"+str(i)
    operationImage(filename)
for i in xrange(1,26):
    filename="food"+str(i)
    operationImage(filename)
for i in xrange(1,15):
    filename="fruit"+str(i)
    operationImage(filename)
for i in xrange(1,35):
    filename="fun"+str(i)
    operationImage(filename)
for i in xrange(1,31):
    filename="landscape"+str(i)
    operationImage(filename)
for i in xrange(1,17):
    filename="love"+str(i)
    operationImage(filename)
for i in xrange(1,19):
    filename="object"+str(i)
    operationImage(filename)
for i in xrange(1,50):
    filename="people"+str(i)
    operationImage(filename)
for i in xrange(1,15):
    filename="space"+str(i)
    operationImage(filename)
for i in xrange(1,32):
    filename="kid"+str(i)
    operationImage(filename)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值