I'm pretty new with React and Python Flask. I'm trying to read a QR Image from React Front-end, and pass it to Python Flask back end to handle decoding the image. I got the blob from React: blob:http://localhost:3000/da6af34a-b103-4212-884b-4aec8719574d. I'm trying to pass this string to python and read it; however, I'm not sure how I can read and image with blob. Any idea what I should do? Any input is appreciated. Thank you.
#api.route('/api/read_qr_code/<srcQrImg>', methods=["GET"])
def read_qr(srcQrImg):
print(srcQrImg)
img = Image.open(srcQrImg)
output = pyzbar.decode(img)
qr_data = str(output[0].data).lstrip("b'").rstrip("'")
qr_data = json.loads(qr_data)
I am building a website, and I am trying to extract one image from a video directly using the link provide by a user.
The things is that all need to be done in memory: download the video in memory (using requests for example), extract an image and I upload it on my aws bucket
I have searched for a solution and found cv2. I was able locally to extract one image using:
vcap = cv2.VideoCapture(path_to_vid)
res, thumb_buf = cv2.imencode('.png', im_ar)
bt = thumb_buf.tostring()
The issue is, after some research, reading and decoding from bytes or content of response is not supported, so I am back to the beginning.
Ideally I wanted something like this:
r = requests.get(url)
vcap = cv2.VideoCapture(io.BytesIO(r.content))
res, thumb_buf = cv2.imencode('.png', im_ar)
bt = thumb_buf.tostring()
I'm trying to save images from the Spotify API
I get album art in the form of a link:
https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce
I think it's a jpg file.
I run into errors in trying to display or save this in python.
I'm not even sure how I'm meant to format something like:
Do I need str around the link?
str(https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce)
Or should I create a new variable e.g.
image_path = 'https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce'
And then:
im1 = im1.save(image_path)
Your second suggestion should work with an addition of actually downloading the image using urllib.request:
import urllib.request
image_path = 'https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce'
urllib.request.urlretrieve(image_path, "image.jpg")
import requests
from PIL import Image
url_shoes_for_choice = [
"https://content.adidas.co.in/static/Product-CM7531/Unisex_OUTDOOR_SANDALS_CM7531_1.jpg",
"https://cdn.shopify.com/s/files/1/0080/1374/2161/products/product-image-897958210_640x.jpg?v=1571713841",
"https://cdn.chamaripashoes.com/media/catalog/product/cache/9/image/9df78eab33525d08d6e5fb8d27136e95/1/_/1_8_3.jpg",
"https://ae01.alicdn.com/kf/HTB1EyKjaI_vK1Rjy0Foq6xIxVXah.jpg_q50.jpg",
"https://www.converse.com/dw/image/v2/BCZC_PRD/on/demandware.static/-/Sites-cnv-master-catalog/default/dwb9eb8c43/images/a_107/167708C_A_107X1.jpg"
]
def img():
for url in url_shoes_for_choice:
image = requests.get(url, stream=True).raw
out = Image.open(image)
out.save('image/image.jpg', 'jpg')
if __name__=="__main__":
img()
Error:
OSError: cannot identify image file <_io.BytesIO object at 0x7fa185c52d58>
The problem is that one of the images is making issues with the byte data returned by the requests.get(url, stream=True).raw, I'm not sure but I guess the data of the 3rd image is invalid byte data so instead of getting the raw data we can just fetch the content and then by using BytesIO we can fix the byte data.
I fixed one more thing from your original code, I added numbering to your images so each can be saved with different name.
from io import BytesIO
def img():
for count, url in enumerate(url_shoes_for_choice):
image = requests.get(url, stream=True)
with BytesIO(image.content) as f:
with Image.open(f) as out:
# out.show() # See the images
out.save('image/image{}.jpg'.format(count))
(Though this works fine but I'm not sure what was the main issue. If anyone knows exactly what is the issue please comment and explain.)
I opened the first link in my browser and saved the image. It's actually a webp file.
$ file Unisex_OUTDOOR_SANDALS_CM7531_1.webp
Unisex_OUTDOOR_SANDALS_CM7531_1.webp: RIFF (little-endian) data, Web/P image, VP8 encoding, 500x500, Scaling: [none]x[none], YUV color, decoders should clamp
You explicitly tell the image library that it should expect a jpg. When you remove that parameter and let it figure it out on its own using out.save('image/image.jpg') the first image successfully downloads for me.
The first two images work this way if you make sure to save each under a different name:
def img():
i = 0
for url in url_shoes_for_choice:
i+=1
image = requests.get(url, stream=True).raw
out = Image.open(image)
out.save('image{}.jpg'.format(i))
the third is a valid jpeg file, as well as the fourth, but using the JFIF standard 1.01 which I hear the first time of. I'm pretty sure you'll have to figure out support for different such filetypes.
It is worth noting that if I download the images in chrome and open those with python, nothing fails. So chrome might be adding information to the file.
The documentation of PIL/pillow explains here that you need a new enough version for animated images, but that is not your problem.
Support for animated WebP files will only be enabled if the system
WebP library is v0.5.0 or later. You can check webp animation support
at runtime by calling features.check(“webp_anim”).
When I display the EXIF data in the Mac app "Preview", I see the absolute number of the image called "Image Number". I assume this is the XXX image that my camera ever took. I would like to get this data in my python code.
"More Info" Window of "Preview"
I have already successfully exported this number from a RAW image with the package exifread by using "MakerNote TotalShutterReleases". But this does not work with JPEG.
import exifread
with open(file_path, 'rb') as img:
tags = exifread.process_file(img)
img_number = tags["MakerNote TotalShutterReleases"]
For an RAW image I get that I want but for JPG:
KeyError: 'MakerNote TotalShutterReleases'
Unfortunately I couldn't find another suitable tag in the list of all tags. Where is this information stored? Why can Preview display this?