Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 11b21fe

Browse files
author
LAKESIDE\LindaT18
committed
resized image according to scaling_factor
1 parent 95ddb39 commit 11b21fe

2 files changed

Lines changed: 47 additions & 34 deletions

File tree

PythonGUI_apps/Image_analysis/Image_analysis.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ def __init__(self):
3535
self.roi = self.imv.roi
3636
self.roi.translateSnap = True
3737
self.roi.scaleSnap = True
38-
self.calc_scaling_factor() #initialize scaling_factor
39-
self.roi.snapSize = self.scaling_factor #roi snaps to multiples of scaling_factor
40-
self.roi.sigRegionChanged.connect(self.line_profile_update_plot)
38+
self.update_scaling_factor() #initialize scaling_factor
39+
4140
self.roi_plot = self.imv.getRoiPlot().getPlotItem()
4241

4342
self.ui.image_groupBox.layout().addWidget(self.imv)
4443

4544
#set up ui signals
45+
self.roi.sigRegionChanged.connect(self.line_profile_update_plot)
4646
self.ui.load_image_pushButton.clicked.connect(self.load_image)
4747
self.ui.custom_pixel_size_checkBox.stateChanged.connect(self.switch_custom_pixel_size)
48+
self.ui.update_scaling_factor_pushButton.clicked.connect(self.update_scaling_factor)
4849

50+
self.num_plots = 0
4951
self.show()
5052

5153
def load_image(self):
@@ -54,28 +56,30 @@ def load_image(self):
5456
"""
5557
try:
5658
file = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', os.getcwd())
57-
image = Image.open(file[0])
58-
image = image.rotate(-90, expand=True)
59-
image_array = np.asarray(image)
60-
try:
61-
width = image_array.shape[0]
62-
height = image_array.shape[1]
63-
x_vals = np.arange(width)
64-
self.imv.setImage(img=image_array, xvals= x_vals)
65-
self.calc_scaling_factor()
66-
self.roi.setPos((0,0))
67-
self.roi.setSize([width, height*self.scaling_factor]) #set line roi
68-
self.line_profile_update_plot()
69-
except:
70-
pass
59+
self.original_image = Image.open(file[0])
60+
self.original_image = self.original_image.rotate(-90, expand=True)
61+
self.resize_to_scaling_factor(self.original_image)
7162
except Exception as err:
7263
print(format(err))
7364

65+
def resize_to_scaling_factor(self, image):
66+
image = image.resize((round(image.size[0]*self.scaling_factor), round(image.size[1]*self.scaling_factor)))
67+
image_array = np.asarray(image)
68+
width = image_array.shape[0]
69+
height = image_array.shape[1]
70+
try:
71+
x_vals = np.arange(width)
72+
self.imv.setImage(img=image_array, xvals= x_vals)
73+
self.roi.setPos((0,0))
74+
self.roi.setSize([width, height * self.scaling_factor]) #set line roi
75+
self.line_profile_update_plot()
76+
except:
77+
pass
78+
79+
7480
def line_profile_update_plot(self):
7581
""" Handle line profile for intensity sum viewbox """
76-
#if hasattr(self, "intensity_sums"):
7782
self.roi_plot.clear()
78-
7983
image = self.imv.getProcessedImage()
8084

8185
# Extract image data from ROI
@@ -84,8 +88,7 @@ def line_profile_update_plot(self):
8488
if data is None:
8589
return
8690

87-
self.calc_scaling_factor()
88-
x_values = coords[1][0] * self.scaling_factor #adjust x-axis roi plot
91+
x_values = coords[1][0]
8992

9093
#calculate sums along columns in region
9194
if len(data.shape) == 2: #if grayscale, average intensities
@@ -108,7 +111,7 @@ def line_profile_update_plot(self):
108111
except:
109112
pass
110113

111-
def calc_scaling_factor(self):
114+
def update_scaling_factor(self):
112115
"""
113116
Calculate scaling factor
114117
"""
@@ -117,6 +120,9 @@ def calc_scaling_factor(self):
117120
else:
118121
pixel_size = 7.4
119122
self.scaling_factor = pixel_size/int(self.ui.magnification_comboBox.currentText())
123+
self.roi.snapSize = self.scaling_factor
124+
if hasattr(self, "original_image"):
125+
self.resize_to_scaling_factor(self.original_image)
120126

121127
def switch_custom_pixel_size(self):
122128
checked = self.ui.custom_pixel_size_checkBox.isChecked()

PythonGUI_apps/Image_analysis/image_analysis_gui.ui

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>1050</width>
9+
<width>1059</width>
1010
<height>743</height>
1111
</rect>
1212
</property>
@@ -51,10 +51,10 @@
5151
</item>
5252
<item row="0" column="0">
5353
<layout class="QGridLayout" name="gridLayout">
54-
<item row="0" column="0" colspan="2">
55-
<widget class="QPushButton" name="load_image_pushButton">
54+
<item row="1" column="2">
55+
<widget class="QCheckBox" name="custom_pixel_size_checkBox">
5656
<property name="text">
57-
<string>Load image</string>
57+
<string>Custom pixel size (um)</string>
5858
</property>
5959
</widget>
6060
</item>
@@ -77,24 +77,31 @@
7777
</item>
7878
</widget>
7979
</item>
80+
<item row="1" column="3">
81+
<widget class="QDoubleSpinBox" name="custom_pixel_size_spinBox">
82+
<property name="enabled">
83+
<bool>false</bool>
84+
</property>
85+
</widget>
86+
</item>
8087
<item row="0" column="2">
8188
<widget class="QLabel" name="label_7">
8289
<property name="text">
8390
<string>Magnification</string>
8491
</property>
8592
</widget>
8693
</item>
87-
<item row="1" column="2">
88-
<widget class="QCheckBox" name="custom_pixel_size_checkBox">
94+
<item row="0" column="0" colspan="2">
95+
<widget class="QPushButton" name="load_image_pushButton">
8996
<property name="text">
90-
<string>Custom pixel size (um)</string>
97+
<string>Load image</string>
9198
</property>
9299
</widget>
93100
</item>
94-
<item row="1" column="3">
95-
<widget class="QDoubleSpinBox" name="custom_pixel_size_spinBox">
96-
<property name="enabled">
97-
<bool>false</bool>
101+
<item row="2" column="2" colspan="2">
102+
<widget class="QPushButton" name="update_scaling_factor_pushButton">
103+
<property name="text">
104+
<string>Update scaling factor</string>
98105
</property>
99106
</widget>
100107
</item>
@@ -110,7 +117,7 @@
110117
<rect>
111118
<x>0</x>
112119
<y>0</y>
113-
<width>1050</width>
120+
<width>1059</width>
114121
<height>31</height>
115122
</rect>
116123
</property>

0 commit comments

Comments
 (0)