日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Python自動化:適合新手練習的五個有趣又實用的Python腳本,幫你快速掌握編程技能!拿走不謝!

來源: 責編: 時間:2024-06-27 17:20:34 149觀看
導讀實踐永遠是掌握一門技術的最佳方法。本文我將分享5個有趣且實用的Python腳本。新手可以跟著做,這將有助于你將理論應用于實踐,并且幫助你快速掌握Python語法。通過你自己的努力創作出來的東西最后能產生實際作用,你也會

實踐永遠是掌握一門技術的最佳方法。本文我將分享5個有趣且實用的Python腳本。新手可以跟著做,這將有助于你將理論應用于實踐,并且幫助你快速掌握Python語法。通過你自己的努力創作出來的東西最后能產生實際作用,你也會有成就感,進一步提升你的興趣和學習的欲望。cUw28資訊網——每日最新資訊28at.com

好了,話不多說,我們直接開始吧!cUw28資訊網——每日最新資訊28at.com

恢復模糊的老照片

這個腳本將通過對 PIL、Matplotlib 以及 Numpy 幾個庫的運用,實現模糊老照片的恢復。這只是一個簡單的示例代碼,它執行基本的去噪和銳化操作。當然,在現在這個技術高速發達的時代,有很多便捷的工具可以實現這一目的,并且效果還會更好,比如機器學習和深度學習算法。因此,該腳本只是為了學習實踐的目的。cUw28資訊網——每日最新資訊28at.com

import numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageFilter# 加載圖片并將其轉換為灰階圖像def load_image(image_path):    img = Image.open(image_path)    return img.convert('L')# 對圖像進行去噪處理def denoise_image(image, weight=0.1):    img_array = np.asarray(image, dtype=np.float32)    out_array = img_array.copy()    out_array[1:-1, 1:-1] = img_array[1:-1, 1:-1] * (1 - 4 * weight) + /                            (img_array[:-2, 1:-1] + img_array[2:, 1:-1] +                              img_array[1:-1, :-2] + img_array[1:-1, 2:]) * weight    return Image.fromarray(np.uint8(out_array), 'L')# 對圖像進行銳化處理def sharpen_image(image, radius=2, percent=150):    return image.filter(ImageFilter.UnsharpMask(radius=radius, percent=percent, threshold=3))# 顯示圖片def display_image(image):    plt.imshow(image, cmap='gray')    plt.axis('off')    plt.show()    # 主程序def main():    # 替換成你自己的圖像路徑    image_path = r'material_sets/blurred_image.jpg'        # 加載圖像    image = load_image(image_path)    # 圖像去噪    denoised_image = denoise_image(image)    # 圖像銳化    sharpened_image = sharpen_image(denoised_image)        # 顯示原始圖像    print(f'Original image: {display_image(image)}')    # 顯示處理后的圖像    print(f'Processed image: {display_image(sharpened_image)}')    if __name__ == '__main__':    main()

圖片圖片cUw28資訊網——每日最新資訊28at.com

從實現效果來看幾乎沒有什么變化,不要在意結果,我們的目的是掌握實現過程。cUw28資訊網——每日最新資訊28at.com

以下是實現過程:cUw28資訊網——每日最新資訊28at.com

  • 加載圖像并將其轉換為灰階格式。
  • 使用一個簡單的加權平均算法對圖像進行去噪。如果想要更好的結果可以嘗試更復雜的算法。
  • 使用反銳化蒙版算法來提升照片的清晰度,突出細節。
  • 最后,展示原始和復原圖像。

2. 創建一個簡單的計算器

在這個腳本中,我們將使用Python自帶的圖形開發庫 tkinter 創建一個簡單的計算器,實現基本的加減乘除運算功能。cUw28資訊網——每日最新資訊28at.com

self.resut_value = tk.StringVar()    self.resut_value.set('0')        self.creat_widgets()    def creat_widgets(self):    # Result display    result_entry = tk.Entry(self,                             textvariable=self.resut_value,                            font=('Arial', 24),                            bd=20,                            justify='right')    result_entry.grid(row=0, column=0, columnspan=4, sticky='nsew')        # Number buttons    button_font = ('Arial', 14)    button_bg = '#ccc'    button_active_bg = '#aaa'    buttons = [        '7', '8', '9',        '4', '5', '6',        '1', '2', '3',        'Clear', '0', 'Delete'    ]    row_val = 1    col_val = 0    for button in buttons:        action = lambda x=button: self.on_button_click(x)        tk.Button(self, text=button, font=button_font,                   bg=button_bg, activebackground=button_active_bg,                   command=action).grid(row=row_val, column=col_val, sticky='nsew')        col_val += 1        if col_val > 2:            col_val = 0            row_val += 1                # Operator buttons    operators = ['+', '-', '*', '/', '=']    for i, operator in enumerate(operators):        action = lambda x=operator: self.on_operator_buttono_click(x)        if operator == '=':            tk.Button(self, text=operator, font=button_font,                   bg=button_bg, activebackground=button_active_bg,                   command=action).grid(row=i+1, column=0, columnspan=4, sticky='nsew')        else:            tk.Button(self, text=operator, font=button_font,                       bg=button_bg, activebackground=button_active_bg,                       command=action).grid(row=i+1, column=3, sticky='nsew')            # Configure row and columns to resize with window    for i in range(5):        self.grid_rowconfigure(i, weight=1)    for i in range(4):        self.grid_columnconfigure(i, weight=1)        def on_button_click(self, char):    if char == 'Clear':        self.resut_value.set('0')    elif char == 'Delete':        current_result = self.resut_value.get()        if len(current_result) > 1:            self.resut_value.set(current_result[:-1])        else:            self.resut_value.set('0')    else:        current_result = self.resut_value.get()        if current_result == '0':            self.resut_value.set(char)        else:            self.resut_value.set(current_result + char)            def on_operator_buttono_click(self, operator):    if operator == '=':        self.on_equal_butoon_click()    else:        current_result = self.resut_value.get()        if current_result[-1] in '+-*/':            self.resut_value.set(current_result[-1] + operator)        else:            self.resut_value.set(current_result + operator)            def on_equal_butoon_click(self):    try:        resut = eval(self.resut_value.get())        self.resut_value.set(str(resut))    except ZeroDivisionError:        self.resut_value.set('ZeroDivisionError!')    except Exception as e:        self.resut_value.set('Other Error!')

圖片圖片cUw28資訊網——每日最新資訊28at.com

3. PDF 轉圖片

該腳本可以將PDF的所有頁面轉換為圖片(一頁一張圖)。此外,執行該腳本前,請確保已經安裝了 PyMuPDF 庫。如果未安裝,請在終端窗口通過 pip install PyMuPDF 命令安裝:cUw28資訊網——每日最新資訊28at.com

import osimport fitzif __name__ == '__main__':    pdf_path = r'your/path/to/sample.pdf'    doc = fitz.open(pdf_path)        save_path = 'your/path/to/pdf-to-images'    # Making it if the save_path is not exist.    os.makedirs(save_path, exist_ok=True)    for page in doc:        pix = page.get_pixmap(alpha=False)        pix.save(f'{save_path}/{page.number}.png')            print('PDF convert to images successfully!')

4. PDF 轉 Word 文檔

同樣地,請確保你的環境已安裝了必要的庫 pdf2docx。如果未安裝,通過 pip install pdf2docx 命令安裝即可。下面這個簡單的示例腳本通過 pdf2docx 實現 PDF 轉 Word 文檔。請將輸入和輸出文件路徑替換成你自己的。cUw28資訊網——每日最新資訊28at.com

from pdf2docx import Converterdef convert_pdf_to_word(input_pdf, output_docx):    # Create a PDF converter object    pdf_converter = Converter(input_pdf)        # Convret the PDF to a docx file    pdf_converter.convert(output_docx)        # Close the converter to release resources    pdf_converter.close()    if __name__ == '__main__':    input_pdf = r'material_sets/12-SQL-cheat-sheet.pdf'    output_docx = r'material_sets/12-SQL-cheat-sheet.docx'        convert_pdf_to_word(input_pdf, output_docx)    print('The PDF file has been successfully converted to Word format!')

圖片圖片cUw28資訊網——每日最新資訊28at.com

原 PDF 文件cUw28資訊網——每日最新資訊28at.com

圖片圖片cUw28資訊網——每日最新資訊28at.com

轉換為 Word 文檔cUw28資訊網——每日最新資訊28at.com

圖片圖片cUw28資訊網——每日最新資訊28at.com

如果你細心觀察的話,轉換后,內容格式沒有發生任何變化。Nice!

本文鏈接:http://www.www897cc.com/showinfo-26-96999-0.htmlPython自動化:適合新手練習的五個有趣又實用的Python腳本,幫你快速掌握編程技能!拿走不謝!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 掌握這四種方法,多線程按序執行不再是問題

下一篇: 高并發場景下到底應該創建多少線程?

標簽:
  • 熱門焦點
  • 5月iOS設備性能榜:M1 M2依舊是榜單前五

    和上個月一樣,沒有新品發布的iOS設備性能榜的上榜設備并沒有什么更替,僅僅只有跑分變化而產生的排名變動,剛剛開始的蘋果WWDC2023,推出的產品也依舊是新款Mac Pro、新款Mac Stu
  • 5月安卓手機好評榜:魅族20 Pro奪冠

    性能榜和性價比榜之后,我們來看最后的安卓手機好評榜,數據來源安兔兔評測,收集時間2023年5月1日至5月31日,僅限國內市場。第一名:魅族20 Pro好評率:97.50%不得不感慨魅族老品牌還
  • 服務存儲設計模式:Cache-Aside模式

    Cache-Aside模式一種常用的緩存方式,通常是把數據從主存儲加載到KV緩存中,加速后續的訪問。在存在重復度的場景,Cache-Aside可以提升服務性能,降低底層存儲的壓力,缺點是緩存和底
  • 如何通過Python線程池實現異步編程?

    線程池的概念和基本原理線程池是一種并發處理機制,它可以在程序啟動時創建一組線程,并將它們置于等待任務的狀態。當任務到達時,線程池中的某個線程會被喚醒并執行任務,執行完任
  • .NET 程序的 GDI 句柄泄露的再反思

    一、背景1. 講故事上個月我寫過一篇 如何洞察 C# 程序的 GDI 句柄泄露 文章,當時用的是 GDIView + WinDbg 把問題搞定,前者用來定位泄露資源,后者用來定位泄露代碼,后面有朋友反
  • 阿里瓴羊One推出背后,零售企業迎數字化新解

    作者:劉曠近年來隨著數字經濟的高速發展,各式各樣的SaaS應用服務更是層出不窮,但本質上SaaS大多局限于單一業務流層面,對用戶核心關切的增長問題等則沒有提供更好的解法。在Saa
  • 華為Mate60標準版細節曝光:經典星環相機模組回歸

    這段時間以來,關于華為新旗艦的爆料日漸密集。據此前多方爆料,今年華為將開始恢復一年雙旗艦戰略,除上半年推出的P60系列外,往年下半年的Mate系列也將
  • iQOO 11S或7月上市:搭載“雞血版”驍龍8Gen2 史上最強5G Soc

    去年底,iQOO推出了“電競旗艦”iQOO 11系列,作為一款性能強機,iQOO 11不僅全球首發2K 144Hz E6全感屏,搭載了第二代驍龍8平臺及144Hz電競屏,同時在快充
  • 榮耀Magic4 至臻版 首創智慧隱私通話 強勁影音系統

    2022年第一季度臨近尾聲,在該季度內,許多品牌陸續發布自己的最新產品,讓大家從全新的角度來了解當今的手機技術。手機是電子設備中,更新迭代十分迅速的一款產品,基
Top 主站蜘蛛池模板: 增城市| 原平市| 松桃| 达拉特旗| 肥城市| 丽江市| 神池县| 陇南市| 郑州市| 阿克| 盐池县| 乐清市| 新化县| 宁河县| 旌德县| 罗田县| 新巴尔虎左旗| 焦作市| 改则县| 红桥区| 双辽市| 屏东县| 大荔县| 手游| 收藏| 佛学| 那曲县| 惠东县| 霞浦县| 安平县| 乌审旗| 蒲江县| 洛阳市| 满洲里市| 灵川县| 涿鹿县| 龙陵县| 霸州市| 龙门县| 崇州市| 阆中市|