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 相关文章推荐
pycharm 使用心得(三)Hello world!
Jun 05 Python
Python编码类型转换方法详解
Jul 01 Python
python中类和实例如何绑定属性与方法示例详解
Aug 18 Python
python 实现对文件夹内的文件排序编号
Apr 12 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
Apr 25 Python
Python获取命令实时输出-原样彩色输出并返回输出结果的示例
Jul 11 Python
Django用户认证系统 组与权限解析
Aug 02 Python
python代码实现逻辑回归logistic原理
Aug 07 Python
Pytorch 实现计算分类器准确率(总分类及子分类)
Jan 18 Python
python调用API接口实现登陆短信验证
May 10 Python
Win10下配置tensorflow-gpu的详细教程(无VS2015/2017)
Jul 14 Python
python 实现一个图形界面的汇率计算器
Nov 09 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 include的妙用,实现路径加密
2008/07/29 PHP
Apache实现Web Server负载均衡详解(不考虑Session版)
2013/07/05 PHP
ThinkPHP控制器间实现相互调用的方法
2014/10/31 PHP
PHP数组和explode函数示例总结
2015/05/08 PHP
php实现的验证码文件类实例
2015/06/18 PHP
PHP异常处理Exception类
2015/12/11 PHP
用js实现随机返回数组的一个元素
2007/08/13 Javascript
查看源码的工具 学习jQuery源码不错的工具
2011/12/26 Javascript
JS自调用匿名函数具体实现
2014/02/11 Javascript
jQuery获得页面元素的绝对/相对位置即绝对X,Y坐标
2014/03/06 Javascript
jQuery源码解读之removeClass()方法分析
2015/02/20 Javascript
js-FCC算法-No repeats please字符串的全排列(详解)
2017/05/02 Javascript
nodejs操作mongodb的填删改查模块的制作及引入实例
2018/01/02 NodeJs
vue组件的写法汇总
2018/04/12 Javascript
webpack手动配置React开发环境的步骤
2018/07/02 Javascript
javascrpt密码强度校验函数详解
2020/03/18 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
[07:12]2014DOTA2西雅图国际邀请赛 黑马Liquid专题采访
2014/07/12 DOTA
简介Python的collections模块中defaultdict类型的用法
2016/07/07 Python
Python中文件I/O高效操作处理的技巧分享
2017/02/04 Python
Python3安装Scrapy的方法步骤
2017/11/23 Python
Python发送邮件测试报告操作实例详解
2018/12/08 Python
python如何将两个txt文件内容合并
2019/10/18 Python
Python气泡提示与标签的实现
2020/04/01 Python
pytorch __init__、forward与__call__的用法小结
2021/02/27 Python
迪梵英国官方网站:Darphin英国
2017/12/06 全球购物
澳大利亚拥有最好的家具和家居用品在线目的地:Nestz
2019/02/23 全球购物
社会公德演讲稿
2014/05/20 职场文书
2015年班级工作总结范文
2015/04/03 职场文书
2015年学校食堂工作总结
2015/04/22 职场文书
2015年司法所工作总结
2015/04/27 职场文书
离婚起诉状范本
2015/05/19 职场文书
负责培养人意见
2015/06/05 职场文书
2016年全国爱眼日宣传教育活动总结
2016/04/05 职场文书
Node-Red实现MySQL数据库连接的方法
2021/08/07 MySQL
css中z-index: 0和z-index: auto的区别
2021/08/23 HTML / CSS