-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage2array.py
128 lines (103 loc) · 4.31 KB
/
image2array.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu May 5 13:07:03 2022
@author: Lukas Schnittcher
usage: Beim ausführen der Datei wird ein GUI-Fenster geöffnet, mit dem es möglich ist
ein Grauwert-Bild (jpg / png) einzulesen um die Bildpixel dann als komplexe Werte
in einem 1D-Vektor in einem .txt-File zu speichern
Mit umkehrung
Feature quadratische Auflösung
"""
"""
from PySimpleGUI import Text, Input, Button, Exit, Window, WIN_CLOSED
from math import sqrt, log2
from numpy import resize, asarray, uint8
from PIL import Image
"""
from PySimpleGUI import Text, Button, Window, WIN_CLOSED, FileBrowse, Listbox, Popup
from math import sqrt, log2
from numpy import resize, asarray, uint8
from PIL import Image
from subprocess import Popen
import os
from sys import platform
def load_greyscale(path):
return Image.open(path).convert('L')
def resize_image(image, size):
if size == 0:
if(image.size[0] >= image.size[1]):
powerof2 = log2(image.size[1])
else:
powerof2 = log2(image.size[0])
return image.resize((2**int(powerof2), 2**int(powerof2)), Image.ANTIALIAS)
else:
return image.resize((size, size), Image.ANTIALIAS)
def image_to_array(image):
return resize(asarray(image.getdata(), dtype= uint8), (image.size[1], image.size[0]))
def write_array(array, path):
array1D = resize(array, (int(len(array)**2),))
with open(path, 'w') as f:
f.write(str(len(array1D)) + "\n")
for i in range(0, len(array1D)):
f.write(str(i) + "\t" + str(array1D[i]) + " " + "0\n")
f.close()
def array_to_image(array):
if(type(array.size) == int):
resolution = int(sqrt(len(array)))
array_2d = resize(array, (resolution, resolution))
return Image.fromarray(array_2d, mode='L')
else:
return Image.fromarray(array, mode='L')
def read_array(path):
array1D = []
with open(path, "r") as f:
lines = f.readlines()
lines.pop(0)
for line in lines:
value = round(float(line.split()[1]))
if(value < 0):
array1D.append(0)
elif(value > 255):
array1D.append(255)
else:
array1D.append(value)
f.close()
return resize(asarray(array1D, dtype = uint8), (len(array1D),))
#sg.theme('DarkAmber') # Keep things interesting for your users
layout = [[Text('Transformation eines (Grau-)Bilds (*.bmp, *.png, *.jpg) in eine Datendatei (*.txt) und umgekehrt'), Text('')],
[Text("Welches File?"), FileBrowse(key='path')],
[Text('Bei Transformation in eine Datendatei: Welche Auflösung (quadratisch)?'), Listbox([2**x for x in range(4,13)], size = (7,4), key='resolution')],
[Button('Transformation'),
# [Text(size=(40,1), key = 'Feedback_1')],
]]
window = Window('Bild-Daten-Transformation', layout)
image_types = ['png', 'peg', 'jpg', 'bmp']
while True: # The Event Loop
event, values = window.read()
if event == WIN_CLOSED or event == 'Exit':
break
if values['path'] != None:
path = values['path']
if path[-1:len(path)-4:-1] == 'txt':
array = read_array(path)
image = array_to_image(array)
path = path[:-4] + "_.png"
image.save(path)
Popup('Transformation von Datendatei zu Bild war erfolgreich.', keep_on_top=True, title="Erfolgreich")
elif path[-3:] in image_types:
size = (values['resolution'])
image = load_greyscale(path)
if(size):
image = resize_image(image, size[0])
else:
Popup('Bitte wähle zuerst eine Auflösung aus.', keep_on_top=True, title="Fehler")
continue
# image = resize_image(image, 0)
array = image_to_array(image)
path = path[:-3] + "txt"
write_array(array, path)
Popup('Transformation von Bild zu Datendatei war erfolgreich.', keep_on_top=True, title="Erfolgreich")
else:
Popup('Bitte wähle zuerst ein Bild oder eine Textdatei aus.', keep_on_top=True, title="Fehler")
continue
window.close()