Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
python3-convert-jp4
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Elphel
python3-convert-jp4
Commits
4cf95fb9
Commit
4cf95fb9
authored
Jun 07, 2018
by
Oleg Dzhimiev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ batch conversion
parent
1213b32b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
173 additions
and
78 deletions
+173
-78
jp4-to-png.py
jp4-to-png.py
+8
-72
jp4.py
jp4.py
+6
-6
jp4_batch_convert.py
jp4_batch_convert.py
+39
-0
jp4_composite.py
jp4_composite.py
+120
-0
No files found.
jp4-to-png.py
View file @
4cf95fb9
#!/usr/bin/env python3
#!/usr/bin/env python3
import
jp4
from
jp4_composite
import
JP4_C
#import cv2
#import cv2
import
numpy
as
np
import
numpy
as
np
import
sys
import
sys
from
PIL
import
Image
,
ImageOps
from
PIL
import
Image
,
ImageOps
import
os
try
:
try
:
filename
=
sys
.
argv
[
1
]
filename
=
sys
.
argv
[
1
]
except
IndexError
:
except
IndexError
:
filename
=
"test.jp4"
filename
=
"test.jp4"
img
=
jp4
.
JP4
(
filename
)
img
=
JP4_C
(
filename
)
# print exif
#print(img.exif.toString())
#print(img.exif.toString())
#print(img.exif.data['Model'])
# deblock if JP4 (auto read from exif) skip if other
img
.
deblock
()
img
.
deblock
()
img
.
demosaic_bilinear
()
img
.
demosaic_bilinear
()
img
.
saturation
()
img
.
saturation
()
subs
=
img
.
extract_subframes
()
I
=
img
.
image
[
...
,[
2
,
1
,
0
]]
.
astype
(
np
.
uint8
)
for
i
in
range
(
len
(
subs
)):
res
=
Image
.
fromarray
(
I
)
#subs[i].save(os.path.join("../res",img.basename+"_"+str(img.subnames[i])+".png"))
#res.save("result.png")
subs
[
i
]
.
save
(
os
.
path
.
join
(
"../res"
,
img
.
basename
+
"_"
+
str
(
img
.
subnames
[
i
])
+
".jpeg"
))
# also works
#res.save("result.jpeg")
# for Eyesis4pi
exif
=
img
.
exif
mn
=
exif
.
MakerNote
#print(exif.MakerNote)
# common
# WOI_WIDTH
# FLIPH
# FLIPV
# PORTRAIT
# COMPOSITE
# individual
# HEIGHTx, 1..3
# FLIPHx, 1..3
# FLIPVx, 1..3
# BLANKx, only 1 and 2
if
mn
[
'COMPOSITE'
]:
W
=
mn
[
'WOI_WIDTH'
]
H
=
mn
[
'WOI_HEIGHT'
]
FH
=
mn
[
'FLIPH'
]
FV
=
mn
[
'FLIPV'
]
#if FV:
# I =
v_offset
=
0
for
i
in
range
(
1
,
4
):
try
:
height
=
mn
[
'HEIGHT'
+
str
(
i
)]
#fliph = mn['FLIPH'+str(i)]
flipv
=
mn
[
'FLIPV'
+
str
(
i
)]
except
KeyError
:
# break for '_9'
break
try
:
blank
=
mn
[
'BLANK'
+
str
(
i
)]
except
KeyError
:
blank
=
0
crop
=
res
.
crop
((
0
,
v_offset
,
W
,
v_offset
+
height
))
if
flipv
:
crop
=
ImageOps
.
flip
(
crop
)
# if eyesis4pi, 1&2 - rotate 90CW, 3 - 90CCW
if
i
<
3
:
crop
=
crop
.
rotate
(
-
90
,
expand
=
1
)
else
:
crop
=
crop
.
rotate
(
90
,
expand
=
1
)
crop
.
save
(
"result_"
+
str
(
i
)
+
".jpeg"
)
v_offset
+=
height
+
blank
jp4.py
View file @
4cf95fb9
...
@@ -28,7 +28,7 @@ def get_exif(filename):
...
@@ -28,7 +28,7 @@ def get_exif(filename):
# exif.MakerNote - parsed as dict
# exif.MakerNote - parsed as dict
return
exif
return
exif
#
clear from code
#
add upper row
def
naur
(
a
):
def
naur
(
a
):
b0
=
a
[
0
]
b0
=
a
[
0
]
b0
=
np
.
reshape
(
b0
,(
1
,
b0
.
shape
[
0
]))
b0
=
np
.
reshape
(
b0
,(
1
,
b0
.
shape
[
0
]))
...
@@ -36,7 +36,7 @@ def naur(a):
...
@@ -36,7 +36,7 @@ def naur(a):
b
=
np
.
concatenate
((
b0
,
b
),
axis
=
0
)
b
=
np
.
concatenate
((
b0
,
b
),
axis
=
0
)
return
b
return
b
#
clear from code
#
add bottom row
def
nabr
(
a
):
def
nabr
(
a
):
b0
=
a
[
-
1
]
b0
=
a
[
-
1
]
b0
=
np
.
reshape
(
b0
,(
1
,
b0
.
shape
[
0
]))
b0
=
np
.
reshape
(
b0
,(
1
,
b0
.
shape
[
0
]))
...
@@ -44,7 +44,7 @@ def nabr(a):
...
@@ -44,7 +44,7 @@ def nabr(a):
b
=
np
.
concatenate
((
b
,
b0
),
axis
=
0
)
b
=
np
.
concatenate
((
b
,
b0
),
axis
=
0
)
return
b
return
b
#
clear from code
#
add left column
def
nalc
(
a
):
def
nalc
(
a
):
b0
=
a
[::,
0
]
b0
=
a
[::,
0
]
b0
=
np
.
reshape
(
b0
,(
b0
.
shape
[
0
],
1
))
b0
=
np
.
reshape
(
b0
,(
b0
.
shape
[
0
],
1
))
...
@@ -52,7 +52,7 @@ def nalc(a):
...
@@ -52,7 +52,7 @@ def nalc(a):
b
=
np
.
concatenate
((
b0
,
b
),
axis
=
1
)
b
=
np
.
concatenate
((
b0
,
b
),
axis
=
1
)
return
b
return
b
#
clear from code
#
add right column
def
narc
(
a
):
def
narc
(
a
):
b0
=
a
[::,
-
1
]
b0
=
a
[::,
-
1
]
b0
=
np
.
reshape
(
b0
,(
b0
.
shape
[
0
],
1
))
b0
=
np
.
reshape
(
b0
,(
b0
.
shape
[
0
],
1
))
...
@@ -126,7 +126,7 @@ class JP4:
...
@@ -126,7 +126,7 @@ class JP4:
im_arr
=
np
.
fromstring
(
im
.
tobytes
(),
dtype
=
np
.
uint8
)
im_arr
=
np
.
fromstring
(
im
.
tobytes
(),
dtype
=
np
.
uint8
)
im_arr
=
im_arr
.
reshape
((
im
.
size
[
1
],
im
.
size
[
0
]))
im_arr
=
im_arr
.
reshape
((
im
.
size
[
1
],
im
.
size
[
0
]))
print
(
im_arr
.
shape
)
#
print(im_arr.shape)
self
.
image
=
im_arr
self
.
image
=
im_arr
self
.
h
,
self
.
w
=
self
.
image
.
shape
self
.
h
,
self
.
w
=
self
.
image
.
shape
...
...
jp4_batch_convert.py
0 → 100644
View file @
4cf95fb9
#!/usr/bin/env python3
from
jp4_composite
import
JP4_C
#import cv2
import
numpy
as
np
import
sys
from
PIL
import
Image
,
ImageOps
import
os
try
:
src
=
sys
.
argv
[
1
]
except
IndexError
:
src
=
"."
try
:
dst
=
sys
.
argv
[
2
]
except
IndexError
:
dst
=
"."
lst
=
os
.
listdir
(
src
)
lst
.
sort
()
for
f
in
lst
:
path
=
os
.
path
.
join
(
src
,
f
)
if
os
.
path
.
isfile
(
path
):
print
(
"Converting "
+
f
)
img
=
JP4_C
(
path
)
#print(img.exif.toString())
#print(img.exif.data['Model'])
img
.
deblock
()
img
.
demosaic_bilinear
()
img
.
saturation
()
subs
=
img
.
extract_subframes
()
for
i
in
range
(
len
(
subs
)):
#subs[i].save(os.path.join("../res",img.basename+"_"+str(img.subnames[i])+".png"))
subs
[
i
]
.
save
(
os
.
path
.
join
(
dst
,
img
.
basename
+
"_"
+
str
(
img
.
subnames
[
i
])
+
".jpeg"
))
jp4_composite.py
0 → 100644
View file @
4cf95fb9
import
re
import
os
from
PIL
import
Image
,
ImageOps
import
numpy
as
np
from
jp4
import
JP4
class
JP4_C
(
JP4
):
def
__init__
(
self
,
filename
):
JP4
.
__init__
(
self
,
filename
)
self
.
filename
=
filename
# filename includes path
# if multiple cameras the files will be indexed: basename_{i}.ext
self
.
model
=
"Unknown"
self
.
basename
=
self
.
__basename
(
filename
)
self
.
subnames
=
self
.
__subnames
()
# extract basename if part of indexed set
def
__basename
(
self
,
s
):
m
=
re
.
search
(
'_
\
d+
\
.'
,
s
)
# likely
if
m
:
basename
=
os
.
path
.
basename
(
s
[:
m
.
start
(
0
)])
# unlikely
else
:
basename
=
os
.
path
.
splittext
(
os
.
path
.
basename
(
s
))[
0
]
return
basename
# based on application name
def
__subnames
(
self
):
subnames
=
[
0
,
1
,
2
]
# model tag has camera name and its channel
model
=
self
.
exif
.
data
[
'Model'
]
m
=
re
.
search
(
'^EYESIS'
,
model
)
if
m
:
self
.
model
=
"EYESIS"
m
=
re
.
search
(
'CHN(
\
d+)$'
,
model
)
if
m
:
chn
=
int
(
m
.
group
(
1
))
if
chn
==
109
:
subnames
=
[
24
,
25
]
else
:
subnames
=
[
chn
-
101
,
chn
-
101
+
8
,
chn
-
101
+
16
]
return
subnames
# based on exif MakeNote
# return array of subframes - flipped and rotated
def
extract_subframes
(
self
):
# convert to PIL format and reorder
# TODO: change stacking in jp4.py to RGB
I
=
Image
.
fromarray
(
self
.
image
[
...
,[
2
,
1
,
0
]]
.
astype
(
np
.
uint8
))
exif
=
self
.
exif
mn
=
exif
.
MakerNote
subframes
=
[]
W
=
mn
[
'WOI_WIDTH'
]
H
=
mn
[
'WOI_HEIGHT'
]
FH
=
mn
[
'FLIPH'
]
FV
=
mn
[
'FLIPV'
]
v_offset
=
0
for
i
in
range
(
1
,
4
):
try
:
height
=
mn
[
'HEIGHT'
+
str
(
i
)]
#fliph = mn['FLIPH'+str(i)]
flipv
=
mn
[
'FLIPV'
+
str
(
i
)]
except
KeyError
:
# break for '_9'
break
# actual break for '_9'
if
height
==
0
:
break
try
:
blank
=
mn
[
'BLANK'
+
str
(
i
)]
except
KeyError
:
blank
=
0
crop
=
I
.
crop
((
0
,
v_offset
,
W
,
v_offset
+
height
))
if
flipv
:
crop
=
ImageOps
.
flip
(
crop
)
# if eyesis4pi, 1&2 - rotate 90CW, 3 - 90CCW
if
i
<
3
:
# special case
if
self
.
model
==
"EYESIS"
and
self
.
subnames
[
i
-
1
]
==
25
:
crop
=
crop
.
rotate
(
90
,
expand
=
1
)
else
:
crop
=
crop
.
rotate
(
-
90
,
expand
=
1
)
else
:
crop
=
crop
.
rotate
(
90
,
expand
=
1
)
subframes
.
append
(
crop
)
#crop.save("result_"+str(i)+".jpeg")
v_offset
+=
height
+
blank
return
subframes
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment