Rotate the picture, fill in the spare part black, and convert the original coordinates
#load the necessary modulesImportOS fromPILImportImage fromPILImportImagedrawImportSYSImportxlrdImportNumPyImportMathImportXlsxwriter#inputExcelfile=xlrd.open_workbook (R'. \ tag information. xlsx') Sheet=excelfile.sheet_by_index (0)#OutputWorkbook = Xlsxwriter. Workbook ('./new/flag information after rotation. xlsx')#Create a workbookWorksheet = Workbook.add_worksheet ()#Create a worksheetPrint(sheet.nrows) forIinchRange (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]) #Calculate cosine valueCosa= (y3-y1)/numpy.sqrt (Numpy.power (x1-x3,2) +numpy.power (y1-y3,2)) #calculation requires an optional radianRad=numpy.abs (Numpy.arccos (cosa))#calculate the angle that needs to be rotatedalpha=rad*180/Math.PI#determine the direction of rotation #and calculates the rotated coordinates. ifx3<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]) #The title of the box is rotated, which is used to verify #Drawobject=imagedraw.draw (Pic_fole_head) #Drawobject.line ([Xx1,yy1,xx2,yy2,xx4,yy4,xx3,yy3,xx1,yy1], (0,5,255), 5) #Save New resultsworksheet.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,One by one, videoid+"_"+frameid+"_"+str (i+1) +". PNG") #pic_fole_head.show ()Pic_fole_head.save ("./new/"+videoid+"_"+frameid+"_"+str (i+1) +". jpg") Workbook.close ()
The purpose of this procedure is to put the title of the marker in the vertical direction
The part that loads the xlsx varies from person to person, and can only look at the steps of conversion in the loop body, and the transformation of coordinates is a simple geometric relation.
The final effect is as follows:
》》》》
Rotate the picture, fill in the spare part black, and convert the original coordinates