python图文_图文转换【Python】

近来看到的创意,重写之

# !/usr/bin/env python3

# encoding : utf-8

from PIL import Image

class way:

def __init__(self):

self.string = "描述"

def __str__(self):

return self.string

def gbk2utf8(filepath):

texts = None

try:

texts = open(filepath,"r").read()

except Exception as e:

texts = open(filepath,"r",encoding="utf-8").read()

open(filepath,"w",encoding="utf-8").write(texts)

def char2corp(chars):

# 文字转元组

num1 = int(ord(chars[0]) / 256)

num2 = ord(chars[0]) % 256

num3 = int(ord(chars[1]) / 256)

num4 = ord(chars[1]) % 256

return (num1,num2,num3,num4)

def corp2char(corp):

# 元组转文字

char1 = chr(corp[0]*256 + corp[1])

char2 = chr(corp[2]*256 + corp[3])

return char1+char2

def txt2png(text_file_path,png_file_path):

way.gbk2utf8(text_file_path)

# 文字转图片

texts = open(text_file_path,"r",encoding="utf-8").read()

if len(texts) % 2 != 0:

texts = texts + "填"

px_num = int(len(texts) / 2)

if pow(px_num,0.5) != int(pow(px_num,0.5)):

width = int(pow(px_num,0.5)) + 1

height = width

else:

width = int(pow(px_num,0.5))

height = width

im = Image.new("RGBA", (width,height), (0,0,0,255))

index = 0

while index < width*height:

if index*2 >= len(texts): texts += "填"

if index*2+2 > len(texts): texts += "填"

im.putpixel((int(index/width), index-int(index/width)*width),way.char2corp(texts[index*2:index*2+2]))

index += 1

im.save("%s.png"%png_file_path)

def png2txt(png_file_path,text_file_path):

im = Image.open(png_file_path)

width,height = im.size

tmp = ""

for i in range(width*height):

tmp_corp = im.getpixel((int(i/width), i-int(i/width)*width))

tmp += way.corp2char(tmp_corp)

while tmp[-1] == "填":

tmp=tmp[:-1]

open(text_file_path+".txt","w",encoding="utf-8").write(tmp)

def main():

a1 = input("\na-转化,b-反转:")

if a1=="a":

way.txt2png(input("\npath(请拖入):"),input("\npath(请输入):"))

else:

way.png2txt(input("\npath(请拖入):"),input("\npath(请输入):"))

if __name__ == '__main__':

main()

毫无诚意的代码,莫得解释哒!

顺带一提,这是《老娘好萌好可爱》的那个啥,懂?

# !/usr/bin/env python3

# encoding : utf-8

from PIL import Image

from base64 import b64encode, b64decode

# 这里使用的是base64产生的字符数必为4的倍数的特性

from os import path

def GenerateXY(nums):

# 构建图片宽高

corps = (0,0)

tmpnum = int(pow(nums,0.5))

while True:

if nums/tmpnum == int(nums/tmpnum):

break

else:

tmpnum -= 1

corps = (int(nums/tmpnum),tmpnum)

return corps

def Becomepx(chars):

# 字符转RGBA

return (ord(chars[0]),ord(chars[1]),ord(chars[2]),ord(chars[3]))

def Becomechar(corps):

# RGBA转字符

return chr(corps[0])+chr(corps[1])+chr(corps[2])+chr(corps[3])

def GeneratePNG(inpath,outpath):

# 文件到图片

if path.exists(inpath) and path.isfile(inpath):

tmpbytes = open(inpath, "rb").read()

tmpstrings = b64encode(tmpbytes).decode()

corps = GenerateXY(int(len(tmpstrings)/4))

im = Image.new("RGBA",corps,(0,0,0,0))

for i in range(int(len(tmpstrings)/4)):

im.putpixel((int(i/corps[1]),i-int(i/corps[1])*corps[1]),Becomepx(tmpstrings[i*4:i*4+4]))

im.save(outpath+".png")

def GenerateFile(inpath,outpath):

# 图片到文件

if path.exists(inpath) and path.isfile(inpath):

im = Image.open(inpath)

width,height = im.size

tmpchar = ""

for i in range(width*height):

tmpchar += Becomechar(im.getpixel((int(i/height), i-int(i/height)*height)))

tmpstrings = b64decode(tmpchar.encode())

open(outpath,"wb").write(tmpstrings)

def main():

# 使用方法如下

GeneratePNG("test.avi","shell")

GenerateFile("shell.png","x.doc")

pass

if __name__ == '__main__':

main()

# 进阶一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值