Python实现图片指定位置加图片水印(附Pyinstaller打包exe)


Posted in Python onMarch 04, 2021

(一)功能实现效果:

选择文件的效果:

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

标记预加水印的位置:

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

(二)Python代码:

# -*l- coding:utf-8 *
import os, io, sys, re, time, json
from pandas import array
import matplotlib.backends.backend_tkagg
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
#from matplotlib.pyplot import imshow, ginput
from PIL import Image, ImageEnhance, ImageFilter
import wx
import numpy as np
import random
class DirDialog(wx.Frame):
 
  def __init__(self):
    """Constructor"""
    wx.Frame.__init__(self, None, -1, u"文件夹选择对话框")
    b = wx.Button(self, -1, u"请选择图片")
    self.Bind(wx.EVT_BUTTON, self.OnButton, b)

  def OnButton(self, event):
    
    # file_wildcard = "Paint files(*.paint)|*.paint|All files(*.*)|*.*"
    dlg = wx.FileDialog(self, u"选择文件夹", style=wx.DD_DEFAULT_STYLE)
    if dlg.ShowModal() == wx.ID_OK:
      jpegname = dlg.GetPath() # 文件夹路径
      print(jpegname)
      np.savez('dir.npz', k_a=str(jpegname))

    dlg.Destroy()


def sealmark(img_seal, img_new):
  try:
    data_a = np.load('dir.npz')#保存地址数据
    img_source = str(data_a['k_a']) # 原图片的地址
    im1 = Image.open(img_source)
    plt.imshow(im1)
    xy = plt.ginput(1)
    xo = xy[0][0]
    yo = xy[0][1]

    ks = im1.size[1] * 0.000478
    wm = Image.open(img_seal)
    wm = wm.resize((int(wm.width * ks), int(wm.height * ks))) # wm.with * k=280 * ks
    layer = Image.new('RGBA', im1.size, (0, 0, 0, 0))
    layer.paste(wm, (int(xo), int(yo)))
    newIm = Image.composite(layer, im1, layer)
    newIm.save(img_new)
    print(img_seal)

  except Exception as e:
    print(">>>>>>>>>>> sealMark EXCEPTION: " + str(e))
    return False
  else:
    return True


if __name__ == '__main__':
  frame = wx.App()
  app = DirDialog()
  app.Show()
  frame.MainLoop()
  sealmark("水印\水印" + str(random.randrange(10)) + ".png", "after_seal.jpg") #random.randrange(10):随机在水印库中挑选一张PNG水印图片

(三)Python打包成exe程序:

在终端里输入pyinstaller -F -w add_seal.py

C:\Users\Administrator\Desktop\seal>pyinstaller -F -w add_seal.py

(1)出现以下的打包成功信息:

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

(2)\dist\文件夹出现exe文件:

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

(3)把exe文件放在主工作区文件夹,并双击运行。

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

(4)成功!

(5)PS:给EXE文件加图标。

1、找一个ICO格式的图标文件:“redseal.ico”,放在项目文件夹内。

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

2、加redseal.ico,进行打包exe。

pyinstaller -F -w -i redseal.ico add_seal.py

3、移动exe的位置,就会出现图标。

Python实现图片指定位置加图片水印(附Pyinstaller打包exe)

到此这篇关于Python实现图片指定位置加图片水印(附:Pyinstaller打包成exe格式)的文章就介绍到这了,更多相关Python实现图片指定位置加图片水印(附:Pyinstaller打包成exe格式)内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Linux下Python获取IP地址的代码
Nov 30 Python
简单的Python的curses库使用教程
Apr 11 Python
Python 实现一个颜色色值转换的小工具
Dec 06 Python
Python 判断是否为质数或素数的实例
Oct 30 Python
浅析Python装饰器以及装饰器模式
May 28 Python
可能是最全面的 Python 字符串拼接总结【收藏】
Jul 09 Python
TensorFlow基于MNIST数据集实现车牌识别(初步演示版)
Aug 05 Python
django自带调试服务器的使用详解
Aug 29 Python
Window系统下Python如何安装OpenCV库
Mar 05 Python
在python中利用pycharm自定义代码块教程(三步搞定)
Apr 15 Python
Python生成随机验证码代码实例解析
Jun 09 Python
python中pow函数用法及功能说明
Dec 04 Python
python 指定源路径来解决import问题的操作
Mar 04 #Python
python源文件的字符编码知识点详解
Mar 04 #Python
python3判断IP地址的方法
Mar 04 #Python
Python解析m3u8拼接下载mp4视频文件的示例代码
Mar 03 #Python
python和opencv构建运动检测器的实现
Mar 03 #Python
Python3自带工具2to3.py 转换 Python2.x 代码到Python3的操作
Mar 03 #Python
详解python第三方库的安装、PyInstaller库、random库
Mar 03 #Python
You might like
php的list()的一步操作给一组变量进行赋值的使用
2011/05/18 PHP
php文件上传简单实现方法
2015/01/24 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
2019/11/24 PHP
thinkphp 框架数据库切换实现方法分析
2020/05/18 PHP
laravel开发环境homestead搭建过程详解
2020/07/03 PHP
JavaScript中的noscript元素属性位置及作用介绍
2013/04/11 Javascript
checkbox设置复选框的只读效果不让用户勾选
2013/08/12 Javascript
Javascript实现Web颜色值转换
2015/02/05 Javascript
JavaScript实现下拉列表框数据增加、删除、上下排序的方法
2015/08/11 Javascript
JavaScript判断数字是否为质数的方法汇总
2016/06/02 Javascript
HTML页面,测试JS对C函数的调用简单实例
2016/08/09 Javascript
jQuery实现简单的计时器功能实例分析
2017/08/29 jQuery
浅谈Vue.nextTick 的实现方法
2017/10/25 Javascript
浅谈vue.js导入css库(elementUi)的方法
2018/03/09 Javascript
vue页面离开后执行函数的实例
2018/03/13 Javascript
js中async函数结合promise的小案例浅析
2019/04/14 Javascript
简单了解微信小程序的目录结构
2019/07/01 Javascript
vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作
2020/07/22 Javascript
python选择排序算法的实现代码
2013/11/21 Python
Python 自动补全(vim)
2014/11/30 Python
Atom的python插件和常用插件说明
2018/07/08 Python
在Python中给Nan值更改为0的方法
2018/10/30 Python
对Tensorflow中Device实例的生成和管理详解
2020/02/04 Python
Python实现FTP文件定时自动下载的步骤
2020/12/19 Python
selenium与xpath之获取指定位置的元素的实现
2021/01/26 Python
recorder.js 基于Html5录音功能的实现
2020/05/26 HTML / CSS
GNC健安喜官方海外旗舰店:美国著名保健品牌
2017/01/04 全球购物
工程造价管理专业大专生求职信
2013/10/06 职场文书
自主招生自荐信指南
2014/02/04 职场文书
中学生自我评价范文
2014/02/08 职场文书
教师师德演讲稿
2014/05/06 职场文书
秋季运动会演讲稿
2014/09/16 职场文书
2016参观监狱警示教育活动心得体会
2016/01/15 职场文书
《司马光》教学反思
2016/02/22 职场文书
Nginx实现会话保持的两种方式
2022/03/18 Servers
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/07 其他游戏