溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用Python組合圖像和文本

發(fā)布時(shí)間:2022-01-26 09:26:49 來源:億速云 閱讀:143 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下怎么使用Python組合圖像和文本的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

先決條件

  1. 1000x600 像素的圖像。

  2. 一些演示圖標(biāo) 100x100 像素。

  3. 熟悉 Pipenv。

  4. 熟悉JupyterLab。

  5. 打開 安裝fonts/OpenSans-Bold.ttf.

入門

讓我們創(chuàng)建hello-img-layers目錄并安裝 Pillow。

# Make the `hello-img-layers` directory
$ mkdir hello-img-layers
$ cd hello-img-layers
# Create a folder to place your icons
$ mkdir icons

# Init the virtual environment
$ pipenv --three
$ pipenv install pillow
$ pipenv install --dev jupyterlab

在這個(gè)階段,將你的 1000x600 圖像添加到根目錄 ( hello-img-layers) 作為base_img.png(至少這是我使用的)并將你的 100x100 圖標(biāo)添加到hello-img-layers/icons/.# Startup the notebook server

$ pipenv run jupyter-lab
# ... Server is now running on http://localhost:8888/lab

創(chuàng)建筆記本

在http://localhost:8888/lab 上,選擇從啟動(dòng)器創(chuàng)建一個(gè)新的 Python 3 筆記本。

確保此筆記本保存在hello-img-layers/docs/<your-file-name>.

我們將創(chuàng)建四個(gè)單元來處理這個(gè)迷你項(xiàng)目的四個(gè)部分:

  1. 加載圖像。

  2. 創(chuàng)建目標(biāo)圖像并添加圖標(biāo)層。

  3. 創(chuàng)建文本層。

  4. 保存和顯示目標(biāo)圖像。

在圖像中加載

本節(jié)只是在 var 中加載圖像base_img(假如你遵循筆記本在docs文件夾中的目錄結(jié)構(gòu))。

from PIL import Image
# Concatenating an image
base_img = Image.open('../base_img.png')

創(chuàng)建目標(biāo)圖像并添加圖標(biāo)層

我們根據(jù)基本圖像的寬度和高度在此處創(chuàng)建目標(biāo)圖層,然后將該基本圖像粘貼回。

然后我們使用 glob 庫來獲取我們圖標(biāo)的所有路徑并將它們粘貼到基本圖像的頂部。

# Add in icons using a glob
import glob
from os.path import join, dirname, abspath

dst_img = Image.new('RGBA', (base_img.width, base_img.height))
dst_img.paste(base_img, (0, 0))

icons_dir = join(dirname(abspath("__file__")), '../icons')
icons = glob.glob(f"{icons_dir}/*")
for i, icon_path in enumerate(icons):
    icon = Image.open(icon_path)
    # @see https://stackoverflow.com/questions/5324647/how-to-merge-a-transparent-png-image-with-another-image-using-pil
    dst_img.paste(icon, (60 + (i * icon.width) + (i * 20), base_img.height - 100 - icon.height), icon)

創(chuàng)建文本層

該層將加載我們的Open Sans字體并將其繪制到圖像上。

max_text_width=25我這里選擇的是通過試驗(yàn)和錯(cuò)誤。對(duì)于不同大小的圖像,可能有一種更可重用的方法來處理此問題。

import os

# Add your own font in
font = ImageFont.truetype(os.path.join('fonts/OpenSans-Bold.ttf'), 58)

import textwrap
text = "Sample Text that is too long but let us write a bunch anyway"
print(dst_img.width)
max_text_width = 25
wrapped_text = textwrap.wrap(text, width=max_text_width)

# setup up a variable to invoke our text method
draw = ImageDraw.Draw(dst_img)

for i, line in enumerate(wrapped_text):
        # draw.text((0, height - (i * 20)), line, (0, 0, 0), font=font)
        draw.text((60, 100 + (i * 80)),line,(255,255,255),font=font)

保存和顯示目標(biāo)圖像

最后,我們將保存并顯示我們的基本圖像。

dst_img.save('../dest_img.png')

# Display the image inline
display(Image.open('../dest_img.png'))

以上就是“怎么使用Python組合圖像和文本”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI