Python: OpenCV: resize image fit to your screen

25 02 2019

OpenCV can display image but if your image bigger than your screen or your display window you can’t see all your image. this code will detect display size and resize your image to fit on it. come and see

# Created: 25/02/2019
# Copyright: (c) PNU 2019
#--------------------------------------------------------------------
import cv2
import ctypes

## get Screen Size
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)


def showImage(filename):
     W,H = screensize
     oriimg = cv2.imread(filename,cv2.CV_LOAD_IMAGE_COLOR)
     height, width, depth = oriimg.shape

     scaleWidth = float(W)/float(width)
     scaleHeight = float(H)/float(height)
     if scaleHeight>scaleWidth:
          imgScale = scaleWidth
     else:
          imgScale = scaleHeight

     newX,newY = oriimg.shape[1]*imgScale, oriimg.shape[0]*imgScale
     newimg = cv2.resize(oriimg,(int(newX),int(newY)))
     cv2.imshow("fit image to screen by CV2",newimg)
     cv2.waitKey(0)

if __name__ == '__main__':
     filename = 'bigImage.jpg'
     showImage(filename)

there are some windows taskbar so image can’t display full screen I think use 90% of scree size is good to display.
enjoy this code.

Advertisements




Install .whl package for Python on Windows

22 02 2019

Python need extension package to do many thing. Now package distribute in wheel file (.whl).The most popular way to install the new Python package or library is to use PIP. After install Python in your machine you need to install pip

OS: windows 10 [64bit]
Python: Python 2.7  [download link: www.python.org ]

install pip:

c:> cd python27
c:Python27>
c:Python27>python D:\Softwares\Python2.7-package\get-pip.py

pip

  • add python script path to the system variable path by open control panel and click little arrow after control panel and select All Control Panel Item

controlpanel

on left menu select Advanced system settings 

system

click Environment Variables… 

envivar

it show Environment Variables box. Select Path in System variables then click Edit…

variable

click at last row and type your Scripts in Python folder the click OK all

addpath

  • now you can install whl package by go to whl download file and type
pip install your whl file

pipinstall

Enjoy your python

download python package here

 





Python: OpenCV: resize image by width

6 07 2014

resize big image by width for example there are 5400×2700 pixels and want to display by 1000 pixel width. try this
import cv2
filename = 'bigimage.jpg'
W = 1000.
oriimg = cv2.imread(filename,cv2.CV_LOAD_IMAGE_COLOR)
height, width, depth = oriimg.shape
imgScale = W/width
newX,newY = oriimg.shape[1]*imgScale, oriimg.shape[0]*imgScale
newimg = cv2.resize(oriimg,(int(newX),int(newY)))
cv2.imshow("Show by CV2",newimg)
cv2.waitKey(0)

if image has width less than 1000 it will be enlarge image.





Python: OpenCV: Plot points on image

4 09 2013

Python: OpenCV: Plot points on image

20130904

use cv2 to plot points on image
point symbol like control point symbol

การพล๊อตข้อมูลจุดลงไปบนภาพ โดยกำหนดลักษณะของจุดเป็นแบบรูปจุดควบคุม

code:

import cv2
 if __name__ == '__main__':
 image067="""
    1,2725.816,1578.254
    2,2339.446,1381.908
    3,2263.594,1318.698
    4,2261.619,1222.698
    6,1941.855,1376.561
    7,1941.188,1288.783
    ...
    68,2357.619,1264.970
    """
 imageFile = r'ladybug067.jpg'
 imagePointList = data2List(image067)
 OriImage = cv2.imread(imageFile)

## plot point on image
 for i in imagePointList:
   pid,cx,cy = i
   cv2.circle(OriImage,(int(cx),int(cy)),10,(255,255,255),-11)
   cv2.circle(OriImage,(int(cx),int(cy)),11,(0,0,255),1) # draw circle
   cv2.ellipse(OriImage, (int(cx),int(cy)), (10,10), 0, 0, 90,(0,0,255),-1 )
   cv2.ellipse(OriImage, (int(cx),int(cy)), (10,10), 0, 180, 270,(0,0,255),-1 )
   cv2.circle(OriImage,(int(cx),int(cy)),1,(0,255,0),1) # draw center
   cv2.putText(OriImage,pid,(int(cx)+10,int(cy)-10),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(255,180,180))
 print 'plot points completed'
 outputfile = 'PlotPoint'+imageFile
 cv2.imwrite(outputfile,OriImage)

รูปร่างของจุดที่ได้

the symbol will like this cp





Python: pyQt: set MessageBox default button

8 03 2013

20130308

I use MessageBox in pyQt and want to change default button from Yes to No, let see how to do.

first I want to show the basic command to show message box

from PyQt4 import QtGui
reply = QtGui.QMessageBox.question(None,'Warning!!','Click [Yes] to do or [No] to skip',QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
 if reply == QtGui.QMessageBox.Yes:
      print 'Yes'
 else:
      print 'No'

output show in image the default button is highlight on Yes button

Image

when we want to change default from Yes to No button we can do like this

from PyQt4 import QtGui
reply = QtGui.QMessageBox.question(None,'Warning!!','Click [Yes] to do or [No] to skip',QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
     print 'Yes'
else:
     print 'No'

the “QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No” is key point first two are what button to show, this word meas show Yes and No button and after comma is what default button now I set No button and the result is
Image





Python: How to skip / break in for loop

21 01 2013

20130121

For loop in python working like this

for i in range(10):
   print i

output will be

0
1
2
3
4
5
6
7
8
9

if you want to exit loop when condition is true (when i = 5)

for i in range(10):
   if i == 5:
      break
   print i

output will be

0
1
2
3
4

if you want skip when condition is true (when i = 5) but you still want it continue loop

for i in range(10):
   if i == 5:
      continue
   print i

output will be

0
1
2
3
4
6
7
8
9




Python: install GDAL with Python 2.7 on Windows 7 64 bit

14 01 2013

20130114

It’s easy to install GDAL library for Python on Windows 7 64 bit

การติดตั้ง GDAL สำหรับ Python บนระบบปฎิบัติการ Windows 7  64 bit เดิมเคยคิดว่ายุ่งยากกลับไม่เป็นเช่นนั้น ติดตั้งได้ง่ายมากเลย

required: (dowload มาได้เลย)

  1. Python 2.7.3 for windows 64 bit [download link][download page]
  2. GDAL-1.9.2.windows 64bit for Python 2.7 [download link]

เมื่อโหลดมาครบแล้วก็เริ่มกันได้เลย

  • install Python 2.7 [if you already installed pass this ^_^]
  • install GDAL 1.9.2

เรียบร้อย ไม่ต้องตั้งค่าอื่นเห็นไหมง่ายมากเลยเทียบกับของเดิม

test: มาทดสอบกันว่าใช้งานได้หรือเปล่า

เปิด Python Shell หรือ Python Interpreter ขึ้นมา
พิมพ์ from osgeo import gdal ถ้าสามารถโหลดเข้ามาได้จะแสดงพร้อมสำหรับรับคำสั่งต่อไป
พิมพ์ dir(gdal) ถ้าสมบูรณ์ก็จะแสดงคำสั่งต่างๆ ออกมาดังภาพ
Image

พร้อมใช้งานแล้ว