Created
November 18, 2021 10:16
-
-
Save sloev/c09b6edf9fb560ef686477643f4f6c71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import csv | |
import numpy as np | |
img = cv2.imread("img.png") | |
data_file = open("data.data", "wb") | |
csv_file = open("data.csv", "w") | |
csv_writer = csv.writer(csv_file) | |
start_byte = 0 | |
for i in range(10): | |
retval, buf = cv2.imencode(".webp", | |
img, | |
[cv2.IMWRITE_WEBP_QUALITY, 100]) | |
end_byte = start_byte + len(buf) | |
data_file.write(buf) | |
csv_writer.writerow([start_byte, end_byte]) | |
start_byte=end_byte | |
data_file.close() | |
csv_file.close() | |
# read part: | |
data_file = open("data.data", "rb") | |
csv_file = open("data.csv", "r") | |
csv_reader = csv.reader(csv_file) | |
for (start_byte, end_byte) in csv_reader: | |
start_byte, end_byte = int(start_byte), int(end_byte) | |
data_file.seek(start_byte) | |
buf = data_file.read(end_byte-start_byte) | |
img = cv2.imdecode(np.frombuffer(buf, np.uint8), 1) | |
print(img) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment