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的部分因人而异,可以只看循环体中转换的步骤,坐标的转换是一个简单的几何关系。

    最终的效果如下:

    》》》》

  • 相关阅读:
    从开发人员角度对软件测试的些许理解
    ObjectiveC的语法
    HttpModule与HttpHandler使用
    我为什么学习HASKELL?
    Linux下C语言编程环境Make命令和Makefile
    一个简单的验证框架
    程序员之路
    ObjectiveC语法之ObjectiveC语言和IOS系统(简介,语法,系统结构)
    Teamcity
    Python进阶 错误处理
  • 原文地址:https://www.cnblogs.com/littleby/p/9770949.html
Copyright © 2011-2022 走看看