暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

python批量给图片加图文水印+读取excel

染卷 2020-04-14
715

在python3.7环境实现给图片添加图片水印以及文字水印。另附python读取excel单元格内容。

利用本程序修改后可以实现,给商品sku图片批量添加水印的功能。excel操作仅测试了读取单元格内容的代码。

  1. #from PIL import Image

  2. from PIL import Image, ImageDraw, ImageFont

  3. ##pip install pillow

  4. #

  5. #import datetime

  6. import time



  7. def pic_add(textin1,textin2,savename):#第一行文本,,第二行文本需要保存的图片名字

  8.    #pip install pillow

  9.    #创建底图

  10.    target = Image.new('RGBA', (300, 300), (0, 0, 0, 0))

  11.    #打开头像

  12.    nike_image = Image.open("./image1.jpg")

  13.    #nike_image = nike_image.resize((300, 300))


  14.    #打开装饰,水印图片,这里用固定值了,有需要的可以设置为变量

  15.    hnu_image = Image.open("./ll.jpg")

  16.    # 分离透明通道

  17.    r,g,b,a = hnu_image.split()

  18.    # 将头像贴到底图

  19.    nike_image.convert("RGBA")

  20.    target.paste(nike_image, (0,0))

  21.    #将装饰贴到底图

  22.    hnu_image.convert("RGBA")

  23.    target.paste(hnu_image,(0,0), mask=a)


  24.    draw = ImageDraw.Draw(target)

  25.     #显示图片

  26.    #target.show()

  27.    #rawsocket.sendto(imcp_packet,(dst_addr,80))

  28.    #OSError: [WinError 10049] 在其上下文中,该请求的地址无效。


  29.    #选择文字字体和大小

  30.    setFont1 = ImageFont.truetype('C:/windows/Fonts/simsun.ttc', 30)

  31.    draw.text((40,40),textin1,font=setFont1,fill='#0000ff',direction=None)

  32.    setFont2 = ImageFont.truetype('C:/windows/Fonts/msyh.ttc', 60)

  33.    draw.text((40,100),textin2,font=setFont2,fill='#FF0000',direction=None)


  34.    setFont3 = ImageFont.truetype('C:/windows/Fonts/SIMYOU.TTF', 20)

  35.    #draw.text((40,200),str(datetime.datetime.now()),font=setFont3,fill='#FFFFFF',direction=None)

  36.    draw.text((40,200),str(time.strftime("%d/%m/%Y")),font=setFont3,fill='#FFFFFF',direction=None)

  37.    #target.show()


  38.    # 保存图片,只能保存为png格式!

  39.    target.save(savename)




  40. ##########################

  41. #

  42. #python读取excel操作开始

  43. #

  44. ##########################

  45. import openpyxl

  46. #pip install openpyxl

  47. #pip install pymodel


  48. # 打开excel文件,获取工作簿对象

  49. wb = openpyxl.load_workbook('商品示例.xlsx')


  50. # 从工作薄中获取一个表单(sheet)对象

  51. #sheets = wb.sheetnames

  52. #print(sheets, type(sheets))


  53. # 创建一个表单  代码无效??

  54. #mySheet = wb.create_sheet('Sheet1')

  55. #print(wb.sheetnames)


  56. # 获取指定的表单

  57. #sheet3 = wb.get_sheet_by_name('商品信息')

  58. # DeprecationWarning: Call to deprecated function get_sheet_by_name(Use wb[sheetname]).


  59. #sheet4 = wb['商品信息']


  60. #for sheet in wb:

  61. #    print(sheet.title)


  62. ws = wb.active  # 当前活跃的表单

  63. #print(ws) #<Worksheet "商品信息">

  64. #print(ws['A1']) # 获取A列的第一个对象   #<Cell '商品信息'.A1>

  65. #print(ws['A1'].value) #商品信息xxx


  66. #c = ws['B1']

  67. #print('Row {}, Column {} is {}'.format(c.row, c.column, c.value)) # 打印这个单元格对象所在的行列的数值和内容

  68. #print('Cell {} is {}\n'.format(c.coordinate, c.value)) # 获取单元格对象的所在列的行数和值

  69. #print(ws.cell(row=1, column=2)) # 获取第一行第二列的单元格

  70. #print(ws.cell(row=1, column=2).value)

  71. #print('{}行 {}列'.format(ws.max_row, ws.max_column)) #行和列的总数


  72. max_excel_row = int(format(ws.max_row))+1

  73. #print(max_excel_row)


  74. i =1

  75. j = 0

  76. for i in range(1,max_excel_row):

  77.    #print(i)

  78.    name = str(ws.cell(i,2).value)

  79.    guige_picname = str(ws.cell(i,11).value)

  80.    gongying_name = str(ws.cell(i,1).value)


  81.    if name != '' and  guige_picname !='None':

  82.        pic_name =  guige_picname + '.png'

  83.        print('正在写入第('+str(i)+'/'+str(max_excel_row-1)+')个商品:'+ name +' 的图片文件:'+ pic_name)

  84.        write_text1 = gongying_name

  85.        write_text2 = '[' + name +']'

  86.        pic_add(write_text1,write_text2,pic_name)

  87.        j = j+1


  88. print('写入有效商品图片个数:'+str(j))


本例中excel示例内容

基于互联网精神,在注明出处的前提下本站文章可自由转载!

本文链接:https://ranjuan.cn/python-watermark-excel/


文章转载自染卷,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论