缩放图片文件,打印GPS信息到照片上

本文介绍了一种使用Python批量处理图片的方法,包括图片压缩及在图片上添加文字标注的功能。通过对图片尺寸进行调整来实现压缩,并利用PIL库在图片上绘制精确定位的文字说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: cp936 -*-
#WriteTo:yilong594@hotmail.com
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import Image,ImageDraw
import os
import ImageFont
if not os.path.exists('.\\resize'):os.mkdir('.\\resize')
bakp="resize"
def resizepic( pathname , bakpath , rate ):
    'resize picture!'
    idx = pathname.rfind( '\\' )
    filename = pathname[idx+1:len(pathname)]
    print filename
    fileurl = bakpath + '\\' + filename
    try:
 #打开原始图片
     im = Image.open( pathname )
     outim = im.resize( ( int(im.size[0]*rate), int(im.size[1]*rate ) ))
     outim.save( fileurl , "JPEG" )
    except IOError:
       print 'Could not create new picture:' , pathname
        
count = 0
thefile = open('result.txt', 'rb')
while True:
    buffer = thefile.read(8192*1024)
    if not buffer:
        break
    count += buffer.count('\n')
thefile.close( )
print count
f=open('result.txt')
row=[]
for line in f.readlines():
       columnline=line.split()
       column=[]
       for field in columnline:
              column.append(field)
       row.append(column)
f.close()
for b in range(0,count):
      resizepic(row[b][0],bakp,0.4)
      lastchar=u'°'
      try:
          float(row[b][2])
      except:
          lastchar=u''
      row[b][2]=row[b][2].replace('\'', u'\u2032')
      row[b][3]=row[b][3].replace('\'', u'\u2032')
      row[b][2]=row[b][2].replace('\"', u'\u2033')
      row[b][3]=row[b][3].replace('\"', u'\u2033')
      s1=u'\u7eac\u5ea6:'
      s2=u'\u7ecf\u5ea6:'
      s3=u'\u6d77\u62d4:'  
      L1=s1+str(row[b][2])+lastchar
      L2=s2+str(row[b][3])+lastchar
      L3=s3+str(row[b][1])+'m'
      im=Image.open(bakp+"\\"+row[b][0])
      fntsize=im.size[0]/20
      fontlocate=(max(len(L1),len(L2),len(L3))-3)+3*2
      fnt=ImageFont.truetype('simsun.ttc',fntsize)
      draw=ImageDraw.Draw(im)
      height1=10
      height2=height1+fntsize
      height3=height1+fntsize*2
      fntlc=im.size[0]-int(fntsize*fontlocate*0.53)
      draw.text((fntlc,height1),L1,font=fnt,fill='#ff0000')
      draw.text((fntlc,height2),L2,font=fnt,fill='#ff0000')
      draw.text((fntlc,height3),L3,font=fnt,fill='#ff0000')
      im.save(bakp+"\\"+os.path.splitext(row[b][0])[0]+'.jpg')
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值