-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processor.py
More file actions
213 lines (174 loc) · 7.01 KB
/
image_processor.py
File metadata and controls
213 lines (174 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Processador de imagem para TopStart Thermal
Responsável por ajustar, recortar e preparar imagens para impressão térmica
"""
from PIL import Image, ImageOps
import numpy as np
class ImageProcessor:
def __init__(self, target_width_px=464, pixels_per_mm=8):
"""
Inicializa o processador de imagem
Args:
target_width_px: Largura alvo em pixels (464 para 58mm)
pixels_per_mm: Pixels por milímetro (8 para 203 DPI)
"""
self.target_width_px = target_width_px
self.pixels_per_mm = pixels_per_mm
def resize_to_width(self, image):
"""
Redimensiona proporcionalmente para largura máxima de 464px, centralizando sempre.
Args:
image: PIL Image
Returns:
PIL Image centralizada
"""
if image.mode not in ('RGB', 'L'):
image = image.convert('RGB')
print(f"[DEBUG] Imagem original: {image.width}x{image.height}px")
print(f"[DEBUG] Largura alvo: {self.target_width_px}px")
# Se a imagem for mais larga, reduz proporcionalmente
if image.width > self.target_width_px:
scale = self.target_width_px / image.width
new_width = self.target_width_px
new_height = int(image.height * scale)
print(f"[DEBUG] Reduzindo para: {new_width}x{new_height}px")
resized = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
# Se for menor, mantém tamanho e centraliza
elif image.width < self.target_width_px:
new_height = image.height
new_img = Image.new('RGB', (self.target_width_px, new_height), 'white')
offset_x = int((self.target_width_px - image.width) / 2)
print(f"[DEBUG] Centralizando com offset X: {offset_x}px")
new_img.paste(image, (offset_x, 0)) # Centraliza
resized = new_img
else:
print(f"[DEBUG] Largura já está correta")
resized = image
print(f"[DEBUG] Imagem final: {resized.width}x{resized.height}px")
return resized
def remove_top_margin(self, image, threshold=250):
"""
Remove margem branca superior da imagem
Args:
image: PIL Image
threshold: Valor de luminosidade para considerar como branco (0-255)
Returns:
PIL Image com margem superior removida
"""
# Converter para grayscale para análise
if image.mode != 'L':
gray_image = image.convert('L')
else:
gray_image = image
# Converter para array numpy
img_array = np.array(gray_image)
# Encontrar primeira linha com pixels escuros
top_line = 0
for y in range(img_array.shape[0]):
# Contar pixels escuros na linha
dark_pixels = np.sum(img_array[y] < threshold)
# Se encontrar pixels escuros suficientes (>1% da largura)
if dark_pixels > (img_array.shape[1] * 0.01):
top_line = y
break
# Se encontrou conteúdo, recortar
if top_line > 0:
# Recortar a imagem original (não a grayscale)
cropped = image.crop((0, top_line, image.width, image.height))
return cropped
return image
def apply_offset(self, image, offset_mm):
"""
Aplica offset vertical manual
Args:
image: PIL Image
offset_mm: Offset em milímetros (positivo = para baixo, negativo = para cima)
Returns:
PIL Image com offset aplicado
"""
offset_px = int(offset_mm * self.pixels_per_mm)
if offset_px == 0:
return image
# Criar nova imagem com espaço para offset
if offset_px > 0:
# Adicionar espaço branco no topo
new_height = image.height + offset_px
new_image = Image.new('RGB', (image.width, new_height), 'white')
new_image.paste(image, (0, offset_px))
return new_image
else:
# Remover pixels do topo
offset_px = abs(offset_px)
if offset_px < image.height:
return image.crop((0, offset_px, image.width, image.height))
else:
return image
def convert_to_monochrome(self, image, method='threshold'):
"""
Converte imagem para monocromático para impressão térmica
Args:
image: PIL Image
method: 'threshold' ou 'dither'
Returns:
PIL Image em modo '1' (preto e branco puro)
"""
# Converter para grayscale primeiro
if image.mode != 'L':
gray = image.convert('L')
else:
gray = image
if method == 'threshold':
# Threshold simples em 50%
mono = gray.point(lambda x: 0 if x < 128 else 255, '1')
elif method == 'dither':
# Dithering (Floyd-Steinberg)
mono = gray.convert('1')
else:
mono = gray.convert('1')
return mono
def detect_content_height(self, image, threshold=250):
"""
Detecta a altura real do conteúdo (ignorando margens brancas)
Args:
image: PIL Image
threshold: Valor de luminosidade para considerar como branco
Returns:
Altura do conteúdo em pixels
"""
# Converter para grayscale
if image.mode != 'L':
gray_image = image.convert('L')
else:
gray_image = image
img_array = np.array(gray_image)
# Encontrar última linha com conteúdo
bottom_line = image.height
for y in range(img_array.shape[0] - 1, -1, -1):
dark_pixels = np.sum(img_array[y] < threshold)
if dark_pixels > (img_array.shape[1] * 0.01):
bottom_line = y + 1
break
# Encontrar primeira linha com conteúdo
top_line = 0
for y in range(img_array.shape[0]):
dark_pixels = np.sum(img_array[y] < threshold)
if dark_pixels > (img_array.shape[1] * 0.01):
top_line = y
break
return bottom_line - top_line
def auto_crop_content(self, image):
"""
Recorta automaticamente para manter apenas o conteúdo
Args:
image: PIL Image
Returns:
PIL Image recortada
"""
# Remover margem superior
image = self.remove_top_margin(image)
# Detectar altura do conteúdo
content_height = self.detect_content_height(image)
# Recortar para altura do conteúdo
if content_height < image.height:
image = image.crop((0, 0, image.width, content_height))
return image