99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀神操作!居然有人用 Python 在 Excel 中畫畫(CDA干貨分享)
神操作!居然有人用 Python 在 Excel 中畫畫(CDA干貨分享)
2022-01-04
收藏

作者:豆豆

來源:Python

十字繡大家都知道吧,今天咱們來玩?zhèn)€電子版的十字繡。

用 Python 讀取圖片的像素值,然后輸出到 Excel 表格中,最終形成一幅像素畫,也就是電子版的十字繡了。

準(zhǔn)備

既然要讀取圖片,那就需要用到 Pillow 庫,操作 Excel 需要用到 openpyxl 庫,先把這兩個庫安裝好。

$ pip3 install openpyxl
$ pip3 install Pillow

色值轉(zhuǎn)換

從圖片讀取的像素塊色值是 RGB 值,而 openpyxl 向 Excel cell 內(nèi)填充顏色是十六進(jìn)制色值,因此咱們先寫一個 RGB 和十六進(jìn)制色值轉(zhuǎn)換的一個函數(shù)。

def rgb_to_hex(rgb):
    rgb = rgb.split(',')
    color = '' for i in RGB:
        num = int(i)
        color += str(hex(num))[-2:].replace('x', '0').upper() return color

圖片轉(zhuǎn)換

有了色值轉(zhuǎn)換函數(shù),接下來要做的操作就是逐行讀取圖片的 RGB 色值,之后將 RGB 色值轉(zhuǎn)換為十六進(jìn)制色值填充到 Excel 的 cell 中即可。

def img2excel(img_path, excel_path): img_src = Image.open(img_path)  # 圖片寬高 img_width = img_src.size[0] img_height = img_src.size[1] str_strlist = img_src.load() wb = openpyxl.Workbook() wb.save(excel_path) wb = openpyxl.load_workbook(excel_path) cell_width, cell_height = 1.0, 1.0 sheet = wb["Sheet"] for w in range(img_width): for h in range(img_height): data = str_strlist[w, h] color = str(data).replace("(", "").replace(")", "") color = rgb_to_hex(color)  # 設(shè)置填充顏色為 color fille = PatternFill("solid", fgColor=color) sheet.cell(h + 1, w + 1).fill = fille for i in range(1, sheet.max_row + 1): sheet.row_dimensions[i].height = cell_height for i in range(1, sheet.max_column + 1): sheet.column_dimensions[get_column_letter(i)].width = cell_width wb.save(excel_path) img_src.close() 

最后再來個入口函數(shù),就大功告成啦~

if __name__ == '__main__':
    img_path = '/Users/xyz/Documents/tmp/03.png' excel_path = '/Users/xyz/Documents/tmp/3.xlsx' img2excel(img_path, excel_path)

驚艷時刻

激動的心,顫抖的手,來看下最終效果咋樣。

神操作!居然有人用 Python 在 Excel 中畫畫

是不是覺得有那么一絲絲韻味呢...

總結(jié)

今天派森醬帶大家一起實現(xiàn)了 Excel 像素畫,小伙伴們可以發(fā)揮自己的想象,比如把女神的頭像藏進(jìn) Excel 中然后發(fā)她,你猜女神會不會被驚艷到呢。

對此你還有什么好玩的想法,可以在評論區(qū)和其他小伙伴一起交流哦~

神操作!居然有人用 Python 在 Excel 中畫畫

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }