python版本为2.7 32位
其他版本信息如下图
运行结果如下:
目前想实现的功能就是将图片能先按照当前屏幕的尺寸缩放,在全屏显示在界面中,因为后续还需要在图片上画直线并获取直线两点的色彩信息,所以采用了opencv的方式,求大神告知代码中哪里错了,怎么改才能让图片也全屏显示(并不要求和界面尺寸大小一致)。
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Hello,wxPython! program."""
import wx
import cv2
import numpy as np
class Frame(wx.Frame):
def __init__(self,image,parent=None,id=-1,pos=wx.DefaultPosition,title="Hello,wxPython!"):
# 转换为bmp,估计是方便处理
# temp = image.ConvertToBitmap()
self.imgSize = [image.shape[0],image.shape[1]]
# self.orginal_image = wx.ImageFromBuffer(self.imgSize[0],self.imgSize[1],self.img_Displayed_gray).ConvertToBitmap()
# 获取图片大小,同时作为Frame的大小
# size = self.orginal_image.GetWidth(),self.orginal_image.GetHeight()
wx.Frame.__init__(self,parent,id,title,pos)
# panel = wx.Panel(self,-1)
# wx.StaticBitmap(parent=self,bitmap=temp)
# wx.StaticBitmap(parent=panel,bitmap=self.temp)
self.dc = wx.ClientDC(self)
ratio = self.CalculateFitRatio(image)
# self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT,self.OnPaint)
# def __init__(self,image):
# wx.Frame.__init__(self)
# self.photo = image.ConvertToBitmap()
# self.Bind(wx.EVT_PAINT,self.OnPaint)
self.resize_image = cv2.resize(image,(int(self.imgSize[1]*ratio),int(self.imgSize[0]*ratio)),interpolation=cv2.INTER_CUBIC)
self.img_Displayed_gray = cv2.cvtColor(self.resize_image, cv2.COLOR_BGR2RGB)
self.displayedBitmap = wx.ImageFromBuffer(int(self.imgSize[1]*ratio),int(self.imgSize[0]*ratio),self.img_Displayed_gray).ConvertToBitmap()
print(int(self.imgSize[1]*ratio),int(self.imgSize[0]*ratio),"-pictrue")
def OnPaint(self,event):
self.dc = wx.PaintDC(self)
brush = wx.Brush("red")
self.dc.SetBrush(brush)
self.dc.Clear()
pen = wx.Pen("red")
# self.dc.SetPen(pen)
# self.dc.SetPen(pen)
self.dc.DrawBitmap(self.displayedBitmap,0,0)
# def OnSize(self,image):
def CalculateFitRatio(self,img_cv):
ratio = 1.0
width_Window = self.Size[0]
height_Window = self.Size[1]
width_img = img_cv.shape[1]
height_img = img_cv.shape[0]
print(width_Window,height_Window,"-window")
# print width_Window,height_Window,width_img,height_img
# print width_Window,height_Window,width_img,height_img
# print float(width_Window)/width_img,float(height_Window)/height_img
if width_img < width_Window and height_img < height_Window:
ratio = 1.0
elif width_img > width_Window and height_img < height_Window: ##比window宽, 没window高
ratio = float(width_Window) /width_img
elif width_img < width_Window and height_img > height_Window: ##没window宽, 比wimdow高
ratio = float(height_Window) /height_img
##长宽都比window大:
elif float(width_Window)/width_img > float(height_Window)/height_img: ##宽多得多
ratio =float(height_Window) /height_img#float(width_Window) /width_img
elif float(width_Window)/width_img <= float(height_Window)/height_img: ##高多得多
ratio = float(width_Window) /width_img #float(height_Window) /height_img
# print height_Window,height_img,ratio
else:
pass
# print ratio
return ratio
class App(wx.App):
def OnInit(self):
image = cv2.imread("IMG_2_1_4608X3456_src.jpg",-1)
self.frame = Frame(image)
self.frame.ShowFullScreen(True)
self. (self.frame)
return True
def main():
app = App()
app.MainLoop()
if __name__ == "__main__":
main()