Python obtains the image Base64 encoding instance code and converts the image to Base64 encoding.

Source: Internet
Author: User
Tags base64 in python

Use Python to obtain the Base64 encoding of the image. The source code is as follows:

The code is as follows: Copy code
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
'''
# Base64-pic.py
'''

Import OS, base64

Icon = open('ya.png ', 'RB ')
IconData = icon. read ()
IconData = base64.b64encode (iconData)
LIMIT = 60
LiIcon = []
While True:
SLimit = iconData [: LIMIT]
IconData = iconData [LIMIT:]
LiIcon. append (''% s' % sLimit)
If len (sLimit) <LIMIT:
Break
Print OS. linesep. join (liIcon)


Python base64 encoded image


A page has been saved many years ago, but it is found that the image is not saved as a file. When the page is opened, the image is displayed,
I started to think it was a game of JavaScript (because a lot of js jumps in and out) and linked to other places. After investigation, I found that there was a large segment of code in the source code that I could not understand,
Although it is estimated that this piece of code is an image, due to the limited vision at that time, it cannot be understood ~
Now I understand it! Python base64 encoded image-(pai_^)-Quiet

Test example: convert text to base64 encoding

>>> Import base64
>>> Ls_s = 'string text'
>>> Ls_t = base64.b64encode (ls_s) # Convert text content to base64
>>> Print ls_t
19a3 + 7 SuzsSxvg =
>>> Print base64.b64decode (ls_t) # decode
String text
>>>

Convert the image content to base64 encoding

Import base64
F = open (r 'X: 1.jpg ', 'RB') # open the image file in binary mode.
Ls_f = base64.b64encode (f. read () # read the file content and convert it to base64 encoding
F. close ()

Write the encoded text into a txt file

Fw = open (r 'X: 1.txt ', 'w') # open a blank text file and prepare to write
Fw. write (ls_f)
Fw. flush ()
Fw. close ()

Webpage expression

<Html> <body> </body>
Note that image/jpeg must be modified here if the image is of another type; image/png, image/gif, image/bmp, etc.

Data: URI is defined in RFC 2397 of the IETF standard.
Data: The basic format of URI is as follows:
Data: [<MIME-type>] [; base64 | charset = some_charset], <data>


Finally, make an instance, save the section as an htm file, and open it in a browser to see if it is a picture,

<HTML> <BODY> Src = "data: image/bmp; base64, invalid values/mt70/zg1M/9 DqMr/YWJg/values/Xouz/5O qpP81Vn7/O4ut/logs/TmaU/logs/g3Z1/x3d9v + JvNj/LCYk/values/HUKV /w0bJ/8XW7L/YNT7/7b1/P + Niof/I43F // commandid was too large Before = "/> </BODY> </HTML>

Available in Chrome, firefox, Opera, and ie8
Here, the Cup has come. It is tested that it cannot be used in ie6, but I think it should be an ie family, maybe ie5.

Check the information. ie8 is also limited to 32 k or less, and ie6/ie7 is not supported, and ie5 is the first to support it (because of efficiency and security issues, ie6 is abandoned ).
It's not easy to understand ~ Python base64 encoded image-(pai_^)-Quiet

Reference
Web screenshot Chrome plugin
Https://chrome.google.com/extensions/detail/ckibcdccnfeookdmbahgiakhnjcddpki? Itemlang = zh-CN
Base64 encoding and decoding in Python
Http://blog.csdn.net/lxdcyh/archive/2009/03/24/4021476.aspx
JavaScript Image Preview 2
Http://www.wolaikanshu.cn/Article/web/htmlcss/201002/39292.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.