python如何對圖片或文件的操作
一. base64 與圖片的相互轉(zhuǎn)換
1. base64 轉(zhuǎn)圖片
import base64 from io import BytesIO from PIL import Image # base64 編碼的圖像數(shù)據(jù)(示例) base64_data = "iVBn9DHASKJDjDsdSADSf8lgg==" # 將 base64 編碼的字符串解碼為二進(jìn)制數(shù)據(jù) binary_data = base64.b64decode(base64_data) # 將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為 BytesIO 對象 bytes_io = BytesIO(binary_data) # 使用 PIL(Pillow)加載圖像 image = Image.open(bytes_io) # 保存圖像 image.save("output_image.png") print("圖像保存成功")
2.圖片轉(zhuǎn) base64
import base64 def file_to_base64(file_path): with open(file_path, "rb") as file: # 讀取文件內(nèi)容 file_data = file.read() # 使用base64編碼 base64_encoded = base64.b64encode(file_data) # 將bytes對象轉(zhuǎn)換為字符串 base64_string = base64_encoded.decode("utf-8") return base64_string # 示例:將圖片轉(zhuǎn)換為Base64編碼 image_path = "D:/output_image.png" base64_data = file_to_base64(image_path) # 打印Base64編碼 print(base64_data)
二.圖片和像素點的操作
1. 讀取圖片的像素點矩陣, 寫入到 json 文件中
jpg 一般為3通道, png 一般為 4通道
""" 讀取圖片像素點矩陣, 寫入到j(luò)son文件 """ from PIL import Image import numpy as np import json # 打開圖片 img = Image.open('D:/input_image.png') # 獲取圖片大小和格式 print(img.size, img.format) # 顯示圖片 # img.show() # 獲取像素數(shù)據(jù) pixels = np.array(img) # 將像素點轉(zhuǎn)為 list 寫入文件 file_object = open('D:/why.json', 'w', encoding="utf8") json.dump(pixels.tolist(), file_object, ensure_ascii=False)
2.讀取像素矩陣, 生成圖片
""" 讀取像素點矩陣, 生成圖片 """ import json import numpy as np import cv2 file_object = open("D:/why.json", "r+", encoding='utf8') file_data_str = file_object.read() file_object.close() matrix_data = json.loads(file_data_str) image = np.array(matrix_data, dtype=np.uint8) print(image.shape) cv2.imwrite("D:/output_image2.jpg", image)
三.本地文件和二進(jìn)制的互相轉(zhuǎn)換
1.本地文件轉(zhuǎn)二進(jìn)制
# 本地文件轉(zhuǎn)二進(jìn)制 def file_binary(path): with open(path, 'rb') as file: binary_data = file.read() return binary_data file_path = "D:/input_image.png" binary = file_binary(file_path)
2.二進(jìn)制轉(zhuǎn)本地文件
# 本地文件轉(zhuǎn)二進(jìn)制 def file_binary(path): with open(path, 'rb') as file: binary_data = file.read() return binary_data # 二進(jìn)制寫入到本地文件 def save_binary_file(path, binary_data): # 將二進(jìn)制數(shù)據(jù)寫入文件 with open(path, "wb") as file: file.write(binary_data) file_path = "D:/input_image.png" binary = file_binary(file_path) to_file_path = "D:/output_image.png" save_binary_file(to_file_path, binary)
四.計算文件的 md5
1.計算本地文件的 md5
import hashlib # 計算本地文件的 md5 值 def calculate_md5(path): # 打開圖像文件 with open(path, "rb") as f: # 讀取圖像數(shù)據(jù) binary_data = f.read() # 計算 MD5 值 md5_hash = hashlib.md5(binary_data) md5_value = md5_hash.hexdigest() return md5_value # 圖像文件路徑 file_path = "D:/output_image.png" # 獲取圖像的 MD5 值 md5 = calculate_md5(file_path) print(md5)
2.計算數(shù)據(jù)二進(jìn)制的 md5
跟計算本地文件的md5差不多, 只不過傳入的是二進(jìn)制數(shù)據(jù)而已, 二進(jìn)制數(shù)據(jù)除了是從文件中直接讀取的, 也有可能是從數(shù)據(jù)庫中獲取, 比如 mongo, 所以也要注意一下編碼問題。
import hashlib # 本地文件轉(zhuǎn)二進(jìn)制 def file_binary(path): with open(path, 'rb') as file: binary_data = file.read() return binary_data # 計算二進(jìn)制數(shù)據(jù)的 md5 def calculate_md5(binary_data): # 檢查是否是 Unicode 字符串 # (因為二進(jìn)制數(shù)據(jù)除了是從文件中直接讀取的, 也有可能是從數(shù)據(jù)庫中獲取, 比如 mongo) if isinstance(binary_data, str): # 將 Unicode 字符串編碼為字節(jié)對象 binary_data = binary_data.encode('utf-8') md5_hash = hashlib.md5(binary_data) return md5_hash.hexdigest() file_path = "D:/input_image.png" binary = file_binary(file_path) # 計算 MD5 值 md5 = calculate_md5(binary) print(md5)
3.windows 獲取本地文件的 md5
certutil -hashfile test.pdf MD5
4.linux 獲取本地文件的 md5
md5sum test.pdf
五.下載網(wǎng)絡(luò)文件到本地
常用的一種爬蟲方法,任何類型的文件都可以
import urllib.request import urllib.parse url = "https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" urllib.request.urlretrieve(url, "D:/abc.jpg")
六.循環(huán)建立多層文件夾
直接建立多層文件夾, 如果已經(jīng)存在的話, 則忽視
def create_folder_if_not_exists(folder_path): # 檢查文件夾是否存在 if not os.path.exists(folder_path): # 如果不存在,創(chuàng)建文件夾 os.makedirs(folder_path)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Python在導(dǎo)入文件時的FileNotFoundError問題
這篇文章主要介紹了解決Python在導(dǎo)入文件時的FileNotFoundError問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04使用Python實現(xiàn)圖像標(biāo)記點的坐標(biāo)輸出功能
這篇文章主要介紹了使用Python實現(xiàn)圖像標(biāo)記點的坐標(biāo)輸出功能,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-08-08python基于pygame實現(xiàn)響應(yīng)游戲中事件的方法(附源碼)
這篇文章主要介紹了python基于pygame實現(xiàn)響應(yīng)游戲中事件的方法,實例分析了Python基于pygame針對鍵盤及鼠標(biāo)事件的響應(yīng)方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11