zoukankan      html  css  js  c++  java
  • 旋转图片,空余部分补黑色,并将原坐标转换

    旋转图片,空余部分补黑色,并将原坐标转换

    #载入必要的模块
    import os
    from PIL import Image
    from PIL import ImageDraw
    import sys
    import xlrd
    import numpy
    import math
    import xlsxwriter
    
    #输入
    ExcelFile=xlrd.open_workbook(r'.标记信息.xlsx')
    sheet=ExcelFile.sheet_by_index(0)
    #输出
    workbook = xlsxwriter.Workbook('./new/旋转后的标记信息.xlsx') #创建工作簿
    worksheet = workbook.add_worksheet() #创建工作表
    
    
    
    print(sheet.nrows)
    
    for i in range(0,sheet.nrows):
        videoid=str(int(sheet.cell_value(i,0)))
        frameid=str(int(sheet.cell_value(i,1)))
        x1=int(sheet.cell_value(i,2))
        x2=int(sheet.cell_value(i,4))
        x3=int(sheet.cell_value(i,6))
        x4=int(sheet.cell_value(i,8))
        y1=int(sheet.cell_value(i,3))
        y2=int(sheet.cell_value(i,5))
        y3=int(sheet.cell_value(i,7))
        y4=int(sheet.cell_value(i,9))
        bookname=sheet.cell_value(i,10)
    
        #print([x1,y1,x2,y2,x4,y4,x3,y3,x1,y1])
    
    
        pic_fole_head =  Image.open(videoid+"_"+frameid+".jpg")
        width,height=pic_fole_head.size
        #print([width,height])
    
        #计算余弦值
        cosa=(y3-y1)/numpy.sqrt(numpy.power(x1-x3,2)+numpy.power(y1-y3,2))
        #计算需要选装的弧度
        rad=numpy.abs(numpy.arccos(cosa))
        #计算需要旋转的角度
        alpha=rad*180/math.pi
        #判断旋转的方向
        #并且计算旋转后的坐标
        if x3<x1:
            pic_fole_head=pic_fole_head.rotate(alpha,0,1)
            yy1=y1*numpy.cos(rad)+(width-x1)*numpy.sin(rad)
            xx1=x1*numpy.cos(rad)+y1*numpy.sin(rad)
            yy2=y2*numpy.cos(rad)+(width-x2)*numpy.sin(rad)
            xx2=x2*numpy.cos(rad)+y1*numpy.sin(rad)
            yy3=y3*numpy.cos(rad)+(width-x3)*numpy.sin(rad)
            xx3=x3*numpy.cos(rad)+y3*numpy.sin(rad)
            yy4=y4*numpy.cos(rad)+(width-x4)*numpy.sin(rad)
            xx4=x4*numpy.cos(rad)+y4*numpy.sin(rad)
        else:
            alpha=-alpha
            pic_fole_head=pic_fole_head.rotate(alpha,0,1)
            yy1=y1*numpy.cos(rad)+x1*numpy.sin(rad)
            xx1=x1*numpy.cos(rad)+(height-y1)*numpy.sin(rad)
            yy2=y2*numpy.cos(rad)+x2*numpy.sin(rad)
            xx2=x2*numpy.cos(rad)+(height-y2)*numpy.sin(rad)
            yy3=y3*numpy.cos(rad)+x3*numpy.sin(rad)
            xx3=x3*numpy.cos(rad)+(height-y3)*numpy.sin(rad)
            yy4=y4*numpy.cos(rad)+x4*numpy.sin(rad)
            xx4=x4*numpy.cos(rad)+(height-y4)*numpy.sin(rad)
    
        #print([xx1,yy1,xx2,yy2,xx4,yy4,xx3,yy3,xx1,yy1])
        #框出旋转后的书名,测试时用来验证
        #drawObject=ImageDraw.Draw(pic_fole_head)
        #drawObject.line([xx1,yy1,xx2,yy2,xx4,yy4,xx3,yy3,xx1,yy1],(0,5,255),5)
        
        #保存新结果
        worksheet.write(i, 0, int(videoid))
        worksheet.write(i, 1, int(frameid))
        worksheet.write(i, 2, int(xx1))
        worksheet.write(i, 3, int(yy1))
        worksheet.write(i, 4, int(xx2))
        worksheet.write(i, 5, int(yy2))
        worksheet.write(i, 6, int(xx3))
        worksheet.write(i, 7, int(yy3))
        worksheet.write(i, 8, int(xx4))
        worksheet.write(i, 9, int(yy4))
        worksheet.write(i, 10, bookname)
        worksheet.write(i, 11, videoid+"_"+frameid+"_"+str(i+1)+".png")
        #pic_fole_head.show()
    
        pic_fole_head.save("./new/"+videoid+"_"+frameid+"_"+str(i+1)+".jpg")
    workbook.close()

    本程序目的是:将标记的书名放到垂直方向

    载入xlsx的部分因人而异,可以只看循环体中转换的步骤,坐标的转换是一个简单的几何关系。

    最终的效果如下:

    》》》》

  • 相关阅读:
    2016.6.13 php与MySQL数据库交互之数据库中的商品信息展示
    2016.6.12 计算机网络复习重点第二章之信道复用技术
    2016.6.10 计算机网络复习要点第二章物理层
    2016.6.11 ASP提交数据到SQL server数据乱码解决方法
    2016.6.6 计算机网络考试要点第一章之网络体系结构
    2016.6.5 计算机网络考试要点第一章之计算机网络性能
    tornado之Hello world
    Python执行show slave status输出的两个格式
    Python logging模块
    Python正则表达式指南(转)
  • 原文地址:https://www.cnblogs.com/littleby/p/9770949.html
Copyright © 2011-2022 走看看