圖片轉二進制python
Ⅰ 關於python如何實現各進制轉換的總結大全
ctf經常遇到進制轉換的問題,就正好做一個進制轉換總結,分享出來供大家參考學習,下面來一起看看詳細的介紹:
字元串與十六進制轉換
例如網路ctf 12月的第二場第一個misc
?
1
626536377D
比較簡單的一種做法就是直接調用字元串的.decode('hex')解密即可, 但如果不用這個函數你會怎麼解呢?
一種思路就是先2個分組,解出每組的ascii值,合並下字元串即可得到,具體代碼如下
?
1234567
import res='626536377D's = re.findall(r'.{2}',s)s = map(lambda x:chr(int(x,16)),s)print ''.join(s)>>>flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}
前面說了字元串的decode('hex')函數,另外還有兩個轉16進制的函數,這里都總結一下
內置函數hex()
只能轉換10進制整數為十六進制,不能轉字元串
binascii庫的hexlify()和b2a_hex()
這兩個函數的功能是將字元串轉換成十六進制,對應的解密函數分別為 unhexlify()和a2b_hex()
進制互轉
二進制,八進制,十六進制轉10進制比較簡單,直接調用
int函數
?
1
int(str,base) //返回十進制整數,但注意此時第一個參數為字元串
對應的解密函數分別是
?
12345
bin() //10進制轉二進制 oct() //十進制轉八進制 hex() //十進制轉十六進制
但二進制直接轉16進制就需要多走一步了,先用int轉十進制,在用上面提到的hex()函數將十進制轉換成十六進制,比較精簡的寫法是
?
1
map(lambda x:hex(int(x,2)),['0011']) //lambda表達式
或者是
?
1
[hex(int(x,2)) for x in ['0011']] //列表解析
對應的解密函數就是
?
1
map(lambda x:bin(int(x,16)),['ef'])
最後在附上自己用python寫的一個進制轉換小工具,主要功能是對一組二進制,或者ascii,或十六進制轉換成字元串,想必ctf上也經常會遇到這類題型吧
?
041424344
# make by 江sir#coding:utf-8import reimport argparse def bintostr(text): text = text.replace(' ','') text = re.findall(r'.{8}',text) s = map(lambda x:chr(int(x,2)),text) #批量二進制轉十進制 flag = ''.join(s) return flag def asciitostr(text): if ' ' in text: text = text.split(' ') elif ',' in text: text = text.split(',') s = map(lambda x:chr(int(x)),text) flag = ''.join(s) return flag def hextostr(text): text = re.findall(r'.{2}',text) #print text s = map(lambda x:chr(int(x,16)),text) #print s flag = ''.join(s) return flag if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-b") parser.add_argument("-a") parser.add_argument("-x") argv = parser.parse_args() #print argv if argv.b: res = bintostr(argv.b) elif argv.a: res = asciitostr(argv.a) elif argv.x: res = hextostr(argv.x) print res
用法:
十六進制轉字元串:
626536377D
?
12
bintostr.py -x "626536377D"flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}
二進制轉字元串:
可以有空格,也可以無空格
00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100
?
12
bintostr.py -b "00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100"/.txt
ascii轉字元串
可以是空格分隔,也可以是,分隔
s='45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32'
?
12
bintostr.py -a "45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32"-.-. - ..-. .-.. ... -... ..--.- -... ... .-..
以上實例均來自某些ctf賽題
總結
Ⅱ 怎麼把JPG格式圖片轉化成二進制數據。
作為2二進制文件打開,一個位元組一個位元組地讀入,寫出,直到EOF(文件結束符)。當然寫出時要按資料庫約定的方式。
下面作為普通文件輸出
FILE *fin,*fout;
void main(){int c;fin=fopen( ck.jpg,rb); // 作為2二進制文件打開
fout=fopen(tmp.jpg,wb);while(1){c=fgetc(fin); // 一個位元組一個位元組地讀入
fputc ( c , fout ); // 改此句,讓它符合資料庫約定。}}
Ⅲ 圖片能轉成2進制的代碼嗎
可以轉的,可以把圖片序列化哼二進位元組數組,也可以把二進制位元組數組反序列化為圖片。使用BinaryFormatter類來操作,圖片質量不會有損失。 給你個示例。
/// <summary>
/// 把對象序列化並返回相應的位元組
/// </summary>
/// <param name="pObj">需要序列化的對象</param>
/// <returns>byte[]</returns>
public byte[] SerializeObject(object pObj)
{
if (pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory, pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read, 0, read.Length);
_memory.Close();
return read;
}
/// <summary>
/// 把位元組反序列化成相應的對象
/// </summary>
/// <param name="pBytes">位元組流</param>
/// <returns>object</returns>
public object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if (pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
Ⅳ Python中讀取二進制圖片(例如 a.jpg)有關解碼編碼的問題
binascii.hexlify估計能滿足你的要求。不過不建議把二進制數據用編解碼來處理,因為編解碼只是文本採用了不同的編碼方案。而圖像這些二進制有自己的含義,文本編解碼方案不應該用在這些二進制文件上。s就是你的數據,你也別想看懂,除非你知道jpg格式。
Ⅳ 圖像二進制原理 圖像二進制化是如何進行的
1、二進制圖片是指圖片是二進制文件,圖片保存在磁碟是二進制文件。實際就是稱作文本文件。它在磁碟保存時也是一種二進制文件。計算機的存儲在物理上是都二進制的,所以文本文件與二進制文件的區別並不是物理上的,而是邏輯上的。這兩者只是在編碼層次上有差異。
2、通過分割從彩色圖像生成二進制圖像。分割是將源圖像中的每個像素分配給兩個或更多個類的過程。如果有兩個以上的類,則通常的結果是幾個二進制圖像。最簡單的分割方式可能是基於灰度強度將像素分配給前景或背景。
3、基本思路是在圖片文件以二進制流的方式讀入到計算機中後,將該二進制流轉換為字元串,即「圖片字元串」,最後保存到XML文檔中。顯示時,則將XML文檔中的「圖片字元串」轉換為二進制流,並用可視組件(如web網頁中的 組件)進行顯示。
Ⅵ 如何將圖片轉換成二進制存儲
資源簡介圖片的常見存儲與讀取凡是有以下幾種:存儲圖片:以二進制的形式存儲圖片時,要把資料庫中的欄位設置為Image數據類型(SQL Server),存儲的數據是Byte[].1.參數是圖片路徑:返回Byte[]類型: public byte[] GetPictureData(string imagepath) { /**/////根據圖片文件的路徑使用文件流打開,並保存為byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重載方法 byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return byData; }2.參數類型是Image對象,返回Byte[]類型: public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { //將Image轉換成流數據,並保存為byte[] MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }好了,這樣通過上面的方法就可以把圖片轉換成Byte[]對象,然後就把這個對象保存到資料庫中去就實現了把圖片的二進制格式保存到資料庫中去了。下面我就談談如何把資料庫中的圖片讀取出來,實際上這是一個相反的過程。讀取圖片:把相應的欄位轉換成Byte[]即:Byte[] bt=(Byte[])XXXX1.參數是Byte[]類型,返回值是Image對象: public System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; }2.參數是Byte[] 類型,沒有返回值,這是針對asp.net中把圖片從輸出到網頁上(Response.BinaryWrite) public void WritePhoto(byte[] streamByte) { // Response.ContentType 的默認值為默認值為「text/html」 Response.ContentType = "image/GIF"; //圖片輸出的類型有: image/GIF image/JPEG Response.BinaryWrite(streamByte); }補充:針對Response.ContentType的值,除了針對圖片的類型外,還有其他的類型: Response.ContentType = "application/msword"; Response.ContentType = "application/x-shockwave-flash"; Response.ContentType = "application/vnd.ms-excel";另外可以針對不同的格式,用不同的輸出類型以適合不同的類型: switch (dataread("document_type")) { case "doc": Response.ContentType = "application/msword"; case "swf": Response.ContentType = "application/x-shockwave-flash"; case "xls": Response.ContentType = "application/vnd.ms-excel"; case "gif": Response.ContentType = "image/gif"; case "Jpg": Response.ContentType = "image/jpeg"; }立即獲得您的.
Ⅶ 怎麼將圖片轉換成二進制,存入資料庫,然後怎麼讀出來並顯示
1.將Image圖像文件存入到資料庫中
我們知道資料庫里的Image類型的數據是"二進制數據",因此必須將圖像文件轉換成位元組數組才能存入資料庫中.
要這里有關數據的操作略寫,我將一些代碼段寫成方法,方便直接調用.
//根據文件名(完全路徑)
public byte[] SetImageToByteArray(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
int streamLength = (int)fs.Length;
byte[] image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image;
}
//另外,在ASP.NET中通過FileUpload控制項得到的圖像文件可以通過以下方法
public byte[] SetImageToByteArray(FileUpload FileUpload1)
{
Stream stream = FileUpload1.PostedFile.InputStream;
byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
stream.Close();
return photo;
}
2.從SQL Server資料庫讀取Image類型的數據,並轉換成bytes[]或Image圖像文件
//要使用SqlDataReader要載入using System.Data.SqlClient命名空間
//將資料庫中的Image類型轉換成byte[]
public byte[] SetImage(SqlDataReader reader)
{
return (byte[])reader["Image"];//Image為資料庫中存放Image類型欄位
}
//將byte[]轉換成Image圖像類型
//載入以下命名空間using System.Drawing;/using System.IO;
using System.Data.SqlClient;*/
public Image SetByteToImage(byte[] mybyte)
{
Image image;
MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length);
image = Image.FromStream(mymemorystream);
return image;
}
Ⅷ python整數轉化為二進制
1、你可以自己寫函數採用 %2 的方式來算。
>>> binary = lambda n: '' if n==0 else binary(n/2) + str(n%2)>>> binary(5)'101'>>>
2、採用 python 自帶了方法 bin 函數,比如 bin(12345) 回返回字元串 '0b11000000111001', 這個時候在把0b去掉即可:
>>> bin(12345).replace('0b','')'11000000111001'
3、也可以採用字元串的 format 方法來獲取二進制:
>>> "{0:b}".format(12345)'11000000111001'>>>
Ⅸ 如何用python讀取圖片的16進制碼
你可以直接用open('test.bmp','rb') open函數打開,這樣得到的是二進制數據,然後你根據圖片格式的相應說明對二進制數據進行相應的轉換即可,或者使用圖像處理庫來做也是很方便的,比如opencv等等。