python 图片base64编码和解码--大坑--↵和%0A

通过requests获取到的图片是base64编码过的,如果处理的话需要进行解码,代码如下

 

 1 import base64   
 2 from PIL import Image
 3 from io import BytesIO
 4 
 5 ##base64_data = '/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a%0AHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy%0AMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AOYDASIA%0AAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA%0AAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3%0AODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm%0Ap6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA%0AAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx%0ABhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK%0AU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3%0AuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3O3g+%0AzxFPNllzI77pW3EbmLY+gzgDsABUtFFANtu7CiiigQUUUUAFFFFABRRRQBSvLORpheWbKl4i7fmO%0AEmTrsfH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ysRznr61bopWTGpSSsmFFFRzRecgXzHTDq2UODwwOPocYPsTT↵JOG/4Vr/AMXb/wCE7/tb/ty+zf8ATDyfv7vx+77e9d7RRQB5jdfCHyviCPFuh65/ZcnnCZrb7J5i↵sx++M71+VucjHc/h6dRRQB5lo/wi/sn4myeMv7c83fdXFz9k+ybceaHG3fvPTf1xzjtXptFFAESx↵zC7kkafMLRoqQ7B8rAtubPU5BUY7bfeqPiHSTrvh7UNLWcW7XdvJAJvL3+XvUqTjIzwT3FadFC0H↵L3tzwgfs4Yt3h/4SlDudW3nTPmGARgHzeh3c/QelSW37Onkbo5PFXm28mPMi/s/GcdCD5vDDJwfc↵g5BIPudFAjxHwf8AAz+yNc03Wm8ReZLYzrJLamy2kOuCULeYfzxyCCOCK9upoRBIZAih2AUtjkgZ↵wM/ifzNCRrGpVBgElvxJyf1NAtbnPeLPA2g+NLVYtXtN0sYxFcRHbLH9G9PY5HtXl1z+zdavMTa+↵Jpoouyy2YkP5h1/lXulFAzynw78A/DOkXKXOpT3GrSIciOUBIs+6jk/QnHtXqiIkcaxxqqooAVVG↵AAOwp1FABRRRQAUUUUAFFFFAH//Z'
 8 
 9 base64_data = base64_data.replace('','').replace('%0A', '')
10 
11 img = base64.b64decode(base64_data)
12 
13 image_data = BytesIO(img)
14 im = Image.open(image_data)
15 im.show()

 

 

获取的数据中竟然有 这个符号:---> ‘↵’    直接转换会报错

替换了它就没问题了

 

posted @ 2020-06-17 15:21  偶神采飞扬  阅读(1184)  评论(1编辑  收藏  举报