OK, thanks. It works after adding "from System.Drawing.Drawing2D import InterpolationMode".
Code: Select all
# *******************************************************************************************
# SharpCap script "Nine_zone.py"
# 2024/09/04 Jean-Francois
#
# Version: 2.1.0: Add pixel zoom
# Version: 2.0.0: Lot of correction. Now 8 and 16 bit MONO, Gamma setting
# Version: 1.0.0: First version
#
# The script works only with a SharpCap PRO version.
# *******************************************************************************************
import os
import math
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
import System.Drawing
import System.Windows.Forms
from System import Array, EventHandler
from System.Drawing import *
from System.Drawing.Drawing2D import InterpolationMode
from System.Windows.Forms import *
from System.Threading import Thread, ApartmentState, ParameterizedThreadStart
from SharpCap.Base import Interfaces
H = SharpCap.SelectedCamera.Controls.Resolution.CaptureArea.Height
W = SharpCap.SelectedCamera.Controls.Resolution.CaptureArea.Width
Boxsize = 100
bH = Boxsize
bW = Boxsize
Rect_1 = Rectangle(0, 0, bW, bH)
Rect_2 = Rectangle((W-bW)//2, 0, bW, bH)
Rect_3 = Rectangle(W - bW, 0, bW, bH)
Rect_4 = Rectangle(0, (H-bH)//2, bW, bH)
Rect_5 = Rectangle((W-bW)//2, (H-bH)//2, bW, bH)
Rect_6 = Rectangle(W - bW, (H-bH)//2, bW, bH)
Rect_7 = Rectangle(0, H - bH, bW, bH)
Rect_8 = Rectangle((W-bW)//2, H - bH, bW, bH)
Rect_9 = Rectangle(W - bW, H - bH, bW, bH)
stretch_gamma = 0.5
colour_space = SharpCap.SelectedCamera.Controls.ColourSpace.Value
zoom = 1
def Show_picture_box(bmp):
form_nine_zone.pictureBox1.Image = bmp
def framehandler(sender, args):
global stretch_gamma, colour_space, zoom
if (dumpdata):
try:
stretch = Interfaces.ReciprocalLevelStretch(0.0, stretch_gamma, 1.0, False)
if (colour_space == 'MONO16' or colour_space == 'RAW16'):
frame = args.Frame.ChangeBitDepth(8).DisplayTransform(stretch).GetFrameBitmap()
else:
frame = args.Frame.DisplayTransform(stretch).GetFrameBitmap()
bitmap = Bitmap(zoom*(3 * bW + 2), zoom*(3 * bH + 2))
gr = Graphics.FromImage(bitmap)
gr.InterpolationMode = InterpolationMode.NearestNeighbor
gr.ScaleTransform(zoom, zoom)
gr.DrawImage(frame, Rectangle( 0, 0, bW, bH), Rect_1, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( bW + 1, 0, bW, bH), Rect_2, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle(2*bW + 2, 0, bW, bH), Rect_3, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( 0, bH + 1, bW, bH), Rect_4, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( bW + 1, bH + 1, bW, bH), Rect_5, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle(2*bW + 2, bH + 1, bW, bH), Rect_6, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( 0, 2*bH + 2, bW, bH), Rect_7, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( bW + 1, 2*bH + 2, bW, bH), Rect_8, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle(2*bW + 2, 2*bH + 2, bW, bH), Rect_9, GraphicsUnit.Pixel)
form_nine_zone.BeginInvoke(lambda: Show_picture_box(bitmap))
frame.Dispose() # here frame is a bitmap
except:
print("Problem framehandler")
# *****************************************************************************
def Show_Nine_zone(self):
global dumpdata, colour_space, zoom
global bH, bW, Rect_1, Rect_2, Rect_3, Rect_4, Rect_5, Rect_6, Rect_7, Rect_8, Rect_9
Boxsize = int(self.textBoxSize.Text)
bH = Boxsize
bW = Boxsize
Rect_1 = Rectangle(0, 0, bW, bH)
Rect_2 = Rectangle((W-bW)//2, 0, bW, bH)
Rect_3 = Rectangle(W - bW, 0, bW, bH)
Rect_4 = Rectangle(0, (H-bH)//2, bW, bH)
Rect_5 = Rectangle((W-bW)//2, (H-bH)//2, bW, bH)
Rect_6 = Rectangle(W - bW, (H-bH)//2, bW, bH)
Rect_7 = Rectangle(0, H - bH, bW, bH)
Rect_8 = Rectangle((W-bW)//2, H - bH, bW, bH)
Rect_9 = Rectangle(W - bW, H - bH, bW, bH)
if (self.comboboxZoom.Text == "100%"):
zoom = 1
elif (self.comboboxZoom.Text == "200%"):
zoom = 2
elif (self.comboboxZoom.Text == "300%"):
zoom = 3
elif (self.comboboxZoom.Text == "400%"):
zoom = 4
elif (self.comboboxZoom.Text == "600%"):
zoom = 6
elif (self.comboboxZoom.Text == "800%"):
zoom = 8
self.pictureBox1.Size = System.Drawing.Size(zoom*(3*Boxsize + 2), zoom*(3*Boxsize + 2))
# if (3*Boxsize + 2 > 450):
# new_width = int((3*Boxsize + 2)/1.476)
# self.ClientSize = System.Drawing.Size(new_width, new_width + 75)
# self.trackBar1.Size = Size(self.ClientSize.Width - 10, 35)
# self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
# self.AutoScaleDimensions = SizeF(96, 96)
# if (3*Boxsize + 2 < 450):
# new_width = 300
# self.ClientSize = System.Drawing.Size(new_width, new_width + 75)
# self.trackBar1.Size = Size(self.ClientSize.Width - 10, 35)
# self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
# self.AutoScaleDimensions = SizeF(96, 96)
if (self.Nine_zone.Text == "Start"):
self.Nine_zone.Text = "Stop"
self.Nine_zone.BackColor = Color.Red
colour_space = SharpCap.SelectedCamera.Controls.ColourSpace.Value
dumpdata = True
SharpCap.SelectedCamera.FrameCaptured += framehandler
elif (self.Nine_zone.Text == "Stop"):
self.Nine_zone.Text = "Start"
self.Nine_zone.BackColor = Color.Gainsboro
colour_space = SharpCap.SelectedCamera.Controls.ColourSpace.Value
dumpdata = False
SharpCap.SelectedCamera.FrameCaptured -= framehandler
print()
print("Nine Zones:")
print("*****************")
# *****************************************************************************
class Nine_zoneWINMenuForm(Form):
def __init__(self):
self.SuspendLayout()
self.InitializeComponent()
self.setupCheckButtons()
self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
self.AutoScaleDimensions = SizeF(96, 96)
self.ResumeLayout()
def InitializeComponent(self):
self.Text = "Nine Zones"
self.ClientSize = System.Drawing.Size(400, 500) #340)
self.TopMost = True
def setupCheckButtons(self):
self.Nine_zone = Button()
self.Nine_zone.Text = "Start"
self.Nine_zone.Location = Point(20, 10)
self.Nine_zone.Click += self.Nine_zone_meas
self.Nine_zone.AutoSize = True
self.labelSize = Label()
self.labelSize.AutoSize = True
self.labelSize.Location = Point(110, 14)
self.labelSize.Name = "labelSize"
self.labelSize.Text = "Size:"
self.textBoxSize = TextBox()
self.textBoxSize.AutoSize = True
self.textBoxSize.Location = Point(142, 11)
self.textBoxSize.Name = "textBoxSize"
self.textBoxSize.Size = Size(35, 20)
self.textBoxSize.Text = str(Boxsize)
self.labelZoom = Label()
self.labelZoom.AutoSize = True
self.labelZoom.Location = Point(195, 14)
self.labelZoom.Name = "labelZoom"
self.labelZoom.Text = "Zoom:"
self.comboboxZoom = ComboBox()
self.comboboxZoom.Location = Point(240, 11)
self.comboboxZoom.Size = Size(60,10)
self.comboboxZoom.Items.Add("100%")
self.comboboxZoom.Items.Add("200%")
self.comboboxZoom.Items.Add("300%")
self.comboboxZoom.Items.Add("400%")
self.comboboxZoom.Items.Add("600%")
self.comboboxZoom.Items.Add("800%")
self.comboboxZoom.SelectedItem = "100%"
self.button_Exit = Button()
self.button_Exit.Text = "Exit"
self.button_Exit.Location = Point(310, 10)
self.button_Exit.Click += self.exit
self.button_Exit.AutoSize = True
self.pictureBox1 = System.Windows.Forms.PictureBox()
self.pictureBox1.Location = System.Drawing.Point(3, 70)
self.pictureBox1.Name = "pictureBox1"
#self.pictureBox1.Size = System.Drawing.Size(3*Boxsize + 2, 3*Boxsize + 2)
self.pictureBox1.TabIndex = 0
self.pictureBox1.TabStop = False
self.pictureBox1.BackColor = Color.Red
self.pictureBox1.AutoSize = True
self.trackBar1 = TrackBar()
self.trackBar1.Location = Point(8, 40)
self.trackBar1.Size = Size(self.ClientSize.Width - 10, 35)
self.trackBar1.Scroll += EventHandler(self.trackBar1_Scroll)
self.trackBar1.Minimum = 0
self.trackBar1.Maximum = 100
self.trackBar1.TickFrequency = 10
self.trackBar1.Value = 50
self.Controls.Add(self.Nine_zone)
self.Controls.Add(self.labelSize)
self.Controls.Add(self.textBoxSize)
self.Controls.Add(self.labelZoom)
self.Controls.Add(self.comboboxZoom)
self.Controls.Add(self.button_Exit)
self.Controls.Add(self.pictureBox1)
self.Controls.Add(self.trackBar1)
def Nine_zone_meas(self, sender, event):
th = Thread(ParameterizedThreadStart(Show_Nine_zone))
th.SetApartmentState(ApartmentState.STA)
th.Start(self)
def trackBar1_Scroll(self, sender, event):
global stretch_gamma
stretch_gamma = self.trackBar1.Value / 100
def exit(self, sender, event):
self.Close()
def BeforeClosing(self, sender, event):
print("Stop before closing script")
dumpdata = False
SharpCap.SelectedCamera.FrameCaptured -= framehandler
form_nine_zone = Nine_zoneWINMenuForm()
form_nine_zone.StartPosition = FormStartPosition.CenterScreen
form_nine_zone.TopMost = True
form_nine_zone.FormClosing += form_nine_zone.BeforeClosing
form_nine_zone.Show()
Left: the zoom in my script, right: the zoom in SharpCap window.