Skip to content

Commit

Permalink
PGP: lab2 add converter
Browse files Browse the repository at this point in the history
  • Loading branch information
tutkarma committed Oct 12, 2019
1 parent c34db96 commit 9112ef2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions PGP/lab2/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import argparse

from PIL import Image


def bin_to_image(src, dest):
with open(src, 'rb') as f:
arr = f.read()

w = int.from_bytes(arr[:4], byteorder='little')
h = int.from_bytes(arr[4:8], byteorder='little')

img = Image.frombytes('RGBA', (w, h), arr[8:], 'raw')
img.save(dest, 'PNG')


def image_to_bin(src, dest):
res = b''

with Image.open(src) as img:
w, h = map(lambda x: x.to_bytes(4, byteorder='little'), img.size)
res += w + h

for pixel in img.convert('RGBA').getdata():
r, g, b, alpha = map(lambda x: x.to_bytes(1, byteorder='little'), pixel)
res += r + g + b + alpha

with open(dest, 'wb') as f:
f.write(res)


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--encode', action='store_true', help='Convert image to bin')
parser.add_argument('--decode', action='store_true', help='Convert bin to image')
parser.add_argument('--src', required=True)
parser.add_argument('--dest', required=True)
args = parser.parse_args()

if args.encode and args.decode:
print('Choose encode or decode')
elif args.decode:
bin_to_image(args.src, args.dest)
elif args.encode:
image_to_bin(args.src, args.dest)
Binary file added PGP/lab2/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9112ef2

Please sign in to comment.