有个业务,需要将图片压缩转化为64位编码上传到服务端。

import json,requests,base64
#网上下载图片素材
r = requests.get("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552573900887&di=4e80542ac9bbb801c7f1cf60fe355570&imgtype=0&src=http%3A%2F%2Fimg009.hc360.cn%2Fg1%2FM09%2F67%2F82%2FwKhQL1L26KyEFHl5AAAAAN7_Cqw821.jpg")
with open('beng.jpg','wb') as f:
f.write(r.content) # 图片的二进制字符串
with open('beng.jpg', 'rb') as f:
content2 = f.read()
with open("beng2.jpg", 'wb') as f:
f.write(content2) #图片的64位字符串
with open('beng.jpg','rb') as f:
content1 = base64.b64encode(f.read())
with open('beng1.jpg','wb') as f:
f.write(base64.b64decode(content1))

然后应用,先将客户端生成的64位字符保存为baseC.txt,再将服务端返回的64位字符串保存为baseS.txt。

1.先对2组64位字符长度进行比较

2.分别保存为图片

import json,base64

#获取客户端的64位字符串
with open('baseC.txt', 'rb') as f:
byteC = f.read()
# print(isinstance(byteC,bytes)) #获取服务端的64位字符串
with open('baseS.txt','r') as f:
#获取服务端的图片编码
strS = json.loads(f.read())['data']
#去除b'',先转化为byte
byte1 = str.encode(strS,"ASCII")
#使用decode去除b''
strS = bytes.decode(byte1,"utf-8")
byteS = str.encode(strS, "ASCII") with open("baseC.jpg","wb") as f:
f.write(base64.b64decode(byteC)) with open("baseS.jpg","wb") as f:
f.write(base64.b64decode(byteS))

经过比对,服务端保存的字符串存储图片失败,客户端保存图片无内容,因此先修复客户端,再进行比对

python图片和字符串的转换的更多相关文章

  1. Python十六进制与字符串的转换

    电脑上装了Python2.7和3.3两个版本,平时运行程序包括在Eclipse里面调试都会使用2.7,但是由于某些原因在cmd命令行中输入python得到的解释器则是3.3, 一直没对此做处理,因为这 ...

  2. python 爬虫数据处理字符串时间转换格式方法

    startDate = "2018-10-01"endDate = "2018-10-31" ###字符转化为日期startTime = datetime.da ...

  3. Python 字节与字符串的转换

    html = urlopen("http://www.cnblogs.com/ryanzheng/p/9665224.html") bsObj = BeautifulSoup(ht ...

  4. python数组和字符串互相转换

    字符串转数组 str = '1,2,3' arr = str.split(',') 数组转字符串 arr = ['a','b'] str = ','.join(arr) arr = [1,2,3] s ...

  5. java自带BASE64工具进行图片和字符串转换

    java自带BASE64工具进行图片和字符串转换 import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

  6. java自带BASE64工具进行图片和字符串转换【转】

    java自带BASE64工具进行图片和字符串转换 import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

  7. python base64 编解码,转换成Opencv,PIL.Image图片格式

    二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...

  8. Python技巧——list与字符串互相转换

    Python技巧——list与字符串互相转换   在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.字符串转换成list 命令:list() ...

  9. 【转】Python 字符串大小写转换

    转载自:python 中字符串大小写转换 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: print 'just to ...

随机推荐

  1. maven 总结整理(二)——download source code

    当我们用maven下载jar包时,有时希望下载jar包的源代码,此时可以在pom.xml文件中,进行设置. <build>    <finalName>WebProject&l ...

  2. 原版win10

    windows10专业版:ed2k://|file|cn_windows_10_multiple_editions_x64_dvd_6848463.iso|4303300608|94FD861E824 ...

  3. MogileFS-2.44 安装与配置

    MogileFS-2.44 安装与配置 (转:https://my.oschina.net/u/1259000/blog/182277) 目录 一.MogileFS 介绍 1.1.环境 二.Mogil ...

  4. openresty 一些可选的模板引擎

    以下为一些 openresty 可选的模板引擎,方便使用 lemplate (https://github.com/openresty/lemplate) lua-resty-tags (https: ...

  5. Modularizing your graphQL schemas

    转自: https://tomasalabes.me/blog/nodejs/graphql/2018/07/11/modularizing-graphql.html Modularizing you ...

  6. openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务

    lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http ...

  7. 解决GitHub下载速度比较慢

    第一步,打开本机上的Hosts文件 首先,什么是Hosts文件? 在互联网协议中,host表示能够同其他机器互相访问的本地计算机.一台本地机有唯一标志代码,同网络掩码一起组成IP地址,如果通过点到点协 ...

  8. C# to il 9 Properties and Indexers(属性和索引器)

    A field is simply a memory location, whereas, a property is a collection of methods. Aproperty is re ...

  9. Electron-vue 新建Demo

    vue init simulatedgreg/electron-vue Test(项目名)

  10. spring+mybaits xml配置解析----转

    一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...