Skip to content

Instantly share code, notes, and snippets.

@DiKorsch
Last active November 22, 2022 13:31
Show Gist options
  • Save DiKorsch/4ff2b550d135e24c80c47677f8e3b825 to your computer and use it in GitHub Desktop.
Save DiKorsch/4ff2b550d135e24c80c47677f8e3b825 to your computer and use it in GitHub Desktop.
Image Upload/Download Service
import os
IMAGE_SERVICE_URL = "https://api.inf-cv.uni-jena.de/imserver/"
IMAGE_SERVICE_SECRET = os.environ.get("IMAGE_SERVICE_SECRET", "secret_needed!")
IMAGE_SERVICE_SECRET_KEY = "Upload-Secret"
import qrcode
import numpy as np
import image_service
import config
import PIL
from argparse import ArgumentParser
from matplotlib import pyplot as plt
from skimage.transform import rescale
parser = ArgumentParser()
parser.add_argument("impath")
parser.add_argument("--logo")
def pil2arr(im):
return np.array(im)
def blend(im0, im1):
assert im0.shape[:-1] == im1.shape[:-1]
alpha = im1[..., -1:] / 255.0
return np.uint8(im0[..., :3] * (1-alpha) + im1[..., :3] * alpha)
def main(args):
im = PIL.Image.open(args.impath)
resp = image_service.send(pil2arr(im))
if resp.status_code != 200:
print(f"Failed to upload image: {resp.status_code, resp.reason}")
exit(1)
uuid = resp.content.decode()
# alternative URLs
URL = f"{config.IMAGE_SERVICE_URL}download/{uuid}"
# URL = f"{config.IMAGE_SERVICE_URL}show/{uuid}"
QR = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_H
)
QR.add_data(URL)
img = pil2arr(QR.make_image().convert("RGB"))
img = rescale(img, (2, 2, 1), preserve_range=True).astype(np.uint8)
if args.logo:
with PIL.Image.open(args.logo) as logo:
logo = pil2arr(logo)
# taking base width
basewidth = int(img.shape[1] / 2)
# adjust image size
scale = basewidth / logo.shape[1]
logo = rescale(logo, (scale, scale, 1), preserve_range=True).astype(np.uint8)
y0 = (img.shape[0] - logo.shape[0]) // 2
x0 = (img.shape[1] - logo.shape[1]) // 2
y1 = y0 + logo.shape[0]
x1 = x0 + logo.shape[1]
img[y0:y1, x0:x1, :] = blend(img[y0:y1, x0:x1], logo)
plt.imshow(img)
plt.show()
main(parser.parse_args())
import io
import requests
import config
from urllib3.exceptions import InsecureRequestWarning
# Suppress only the single warning from urllib3 needed.
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
from PIL import Image
def send(image, fmt="JPEG"):
# we need to swap channels, if we use opencv image loading
im = Image.fromarray(image[..., ::-1], mode="RGB")
imgByteArr = io.BytesIO()
im.save(imgByteArr, format=fmt)
mime_type = Image.MIME.get(fmt)
return requests.post(
config.IMAGE_SERVICE_URL,
data=imgByteArr.getvalue(),
headers={
"Content-Type": mime_type,
config.IMAGE_SERVICE_SECRET_KEY: config.IMAGE_SERVICE_SECRET
},
verify=False,
)
qrcode
requests
pillow
scikit-image
matplotlib
pyqt5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment