欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 實現(xiàn)給圖片加文字或logo水印

 更新時間:2021年11月22日 09:26:14   作者:劍客阿良_ALiang  
本文主要為大家介紹了給圖片添加文字或者logo圖片水印的python工具,從而打造你的專屬圖片。代碼簡潔易懂,感興趣的小伙伴可以了解一下

前言

本文提供給圖片添加文字或者logo圖片水印的python工具,打造專屬圖片。

環(huán)境依賴

ffmpeg環(huán)境安裝,ffmpy安裝:

pip install ffmpy -i https://pypi.douban.com/simple

代碼

上代碼。

#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author  : 劍客阿良_ALiang
@file   : image_add_watermark_tool.py
@ide    : PyCharm
@time   : 2021-11-20 14:18:13
"""
import os
import uuid
from ffmpy import FFmpeg
 
 
# 圖片加文字水印
def image_add_text(
        image_path: str,
        output_dir: str,
        text: str,
        font_name: str,
        font_size=100,
        position=(0, 0),
        font_color='blue',
        box=1,
        box_color='white'):
    ext = _check_format(image_path)
    result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
    ff = FFmpeg(
        inputs={
            image_path: None},
        outputs={
            result: '-vf drawtext=\"fontsize={}:fontfile={}:text=\'{}\':x={}:y={}:fontcolor={}:box={}:boxcolor={}\"'.format(
                font_size,
                font_name,
                text,
                position[0],
                position[1],
                font_color,
                box,
                box_color)})
    print(ff.cmd)
    ff.run()
    return result
 
 
# 圖片添加logo
def image_add_logo(
        image_path: str,
        output_dir: str,
        logo_path: str,
        position=(0, 0)):
    ext = _check_format(image_path)
    result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
    filter_cmd = '-vf \"movie=\'{}\' [wm];[in] [wm]overlay={}:{} [out]\"'
    ff = FFmpeg(
        inputs={
            image_path: None}, outputs={
            result: filter_cmd.format(logo_path, position[0], position[1])})
    print(ff.cmd)
    ff.run()
    return result
 
 
def _check_format(image_path: str):
    ext = os.path.basename(image_path).strip().split('.')[-1]
    if ext not in ['png', 'jpg']:
        raise Exception('format error')
    return ext
 

代碼說明

1、image_add_text方法給圖片添加文字水印方法,主要參數(shù)為:圖片路徑、輸出目錄、

水印文字、字體名稱、字體大小(默認100)、文字左上角坐標(biāo)(默認0:0)、文字顏色(默認藍色)、是否需要背景(默認需要為1,不需要為0)、背景色(默認白色)。

2、image_add_logo方法給圖片添加logo,主要參數(shù)為:圖片路徑、輸出目錄、logo圖片地址、文字左上角坐標(biāo)(默認0:0)。

3、注意logo地址如果有類似C:/這種windows盤的路徑情況,需要對":"進行轉(zhuǎn)義。后面驗證的時候,可以看看我給的參數(shù)。

4、文件名為了避免重復(fù),采用了uuid作為文件名。

5、圖片后綴格式校驗只有兩種,按需添加即可。

驗證一下

準(zhǔn)備的素材如下,分別為圖片與logo圖

驗證代碼

if __name__ == '__main__':
    print(
        image_add_text(
            'C:/Users/huyi/Desktop/1.jpg',
            'C:/Users/huyi/Desktop/', '劍客阿良_ALiang', '微軟雅黑', box=0))
    print(
        image_add_logo(
            'C:/Users/huyi/Desktop/1.jpg',
            'C:/Users/huyi/Desktop/', 'C\\:/Users/huyi/Desktop/logo.png', (30, 10)))

注意我給出的logo地址路徑有什么不同。

執(zhí)行結(jié)果

PyDev console: starting.
Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32
runfile('D:/spyder/csdn/image_add_watermark_tool.py', wdir='D:/spyder/csdn')
ffmpeg -i C:/Users/huyi/Desktop/1.jpg -vf drawtext=fontsize=100:fontfile=微軟雅黑:text='劍客阿良_ALiang':x=0:y=0:fontcolor=blue:box=0:boxcolor=white C:/Users/huyi/Desktop/2226d7e0-5166-4ffd-ba95-9ca7d9d6f72d.jpg
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, image2, from 'C:/Users/huyi/Desktop/1.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 181106 kb/s
    Stream #0:0: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 3840x2160, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
Fontconfig error: Cannot load default config file
[Parsed_drawtext_0 @ 0000014152ea5400] Using "C:/Windows/fonts/msyh.ttc"
Output #0, image2, to 'C:/Users/huyi/Desktop/2226d7e0-5166-4ffd-ba95-9ca7d9d6f72d.jpg':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: mjpeg, yuvj444p(pc), 3840x2160, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.91.100 mjpeg
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
frame=    1 fps=0.0 q=11.5 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.158x    
video:634kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
C:/Users/huyi/Desktop/2226d7e0-5166-4ffd-ba95-9ca7d9d6f72d.jpg
ffmpeg -i C:/Users/huyi/Desktop/1.jpg -vf "movie='C\:/Users/huyi/Desktop/logo.png' [wm];[in] [wm]overlay=30:10 [out]" C:/Users/huyi/Desktop/c626411e-531f-4dab-9a78-8103d92bbf1d.jpg
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, image2, from 'C:/Users/huyi/Desktop/1.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 181106 kb/s
    Stream #0:0: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 3840x2160, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 000001d6cc29ad80] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'C:/Users/huyi/Desktop/c626411e-531f-4dab-9a78-8103d92bbf1d.jpg':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: mjpeg, yuvj420p(pc), 3840x2160, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.91.100 mjpeg
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
[Parsed_movie_0 @ 000001d6cc40bd80] EOF timestamp not reliable
frame=    1 fps=0.0 q=11.4 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.176x    
video:455kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
C:/Users/huyi/Desktop/c626411e-531f-4dab-9a78-8103d92bbf1d.jpg

加文字水印效果

加logo效果

以上就是Python 實現(xiàn)給圖片加文字或logo水印的詳細內(nèi)容,更多關(guān)于Python 圖片水印的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • sklearn-SVC實現(xiàn)與類參數(shù)詳解

    sklearn-SVC實現(xiàn)與類參數(shù)詳解

    今天小編就為大家分享一篇sklearn-SVC實現(xiàn)與類參數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python使用add_subplot與subplot畫子圖操作示例

    Python使用add_subplot與subplot畫子圖操作示例

    這篇文章主要介紹了Python使用add_subplot與subplot畫子圖操作,涉及Python使用matplotlib模塊進行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • Python常用數(shù)據(jù)分析模塊原理解析

    Python常用數(shù)據(jù)分析模塊原理解析

    這篇文章主要介紹了Python常用數(shù)據(jù)分析模塊原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • python switch 實現(xiàn)多分支選擇功能

    python switch 實現(xiàn)多分支選擇功能

    這篇文章主要介紹了python switch 實現(xiàn)多分支選擇功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Python查找函數(shù)f(x)=0根的解決方法

    Python查找函數(shù)f(x)=0根的解決方法

    這篇文章主要介紹了Python查找函數(shù)f(x)=0根的解決方法,涉及Python數(shù)學(xué)運算函數(shù)求解的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Django rest framework基本介紹與代碼示例

    Django rest framework基本介紹與代碼示例

    這篇文章主要介紹了Django rest framework基本介紹與代碼示例,簡單敘述了rest framework的一些用處,可選擇的相關(guān)軟件包,然后分享了一個簡單的模型支持的API的例子,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python中日期和時間的用法超強總結(jié)

    Python中日期和時間的用法超強總結(jié)

    時間無疑是生活各個方面中最關(guān)鍵的因素之一,因此,記錄和跟蹤時間變得非常重要。在?Python?中,可以通過其內(nèi)置庫跟蹤日期和時間。今天我們來介紹關(guān)于?Python?中的日期和時間,一起來了解如何使用time和datetime模塊查找和修改日期和時間
    2022-10-10
  • python excel多行合并的方法

    python excel多行合并的方法

    這篇文章主要介紹了python excel多行合并的方法,幫助大家更好的利用python處理excel表格,感興趣的朋友可以了解下
    2020-12-12
  • matplotlib畫圖之修改坐標(biāo)軸刻度問題

    matplotlib畫圖之修改坐標(biāo)軸刻度問題

    這篇文章主要介紹了matplotlib畫圖之修改坐標(biāo)軸刻度問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python判斷字符串與大小寫轉(zhuǎn)換

    Python判斷字符串與大小寫轉(zhuǎn)換

    這篇文章主要介紹了Python判斷字符串與大小寫轉(zhuǎn)換的方法,有需要的小伙伴可以參考下。
    2015-06-06

最新評論