I am doing an OpenCV project and I cant seem to find a way to send the frames using telepot module to my telegram. I've already setup the telegram bot.
------------Opencv processing------
cv2.imshow('Object detector', frame)
bot.sendPhoto(238460030, (frame,'rb'))
I get this error:
AttributeError: 'str' object has no attribute 'read'
If im not mistaken you need to take the photo first ,then you need to send the file with:
photo = ("img.jpeg",mode= 'rb')
Related
I'm trying to build a telegram bot for resizing image. I'm using Python for coding and using the pyTelegramBotAPI as the wrapper. The issue that I'm facing is that I'm not able to manipulate the image after I upload it on Telegram. I'm trying to use Pillow module for image manipulation. As fas as I understand as of now, the bot API is providing the "file ID" of the image instead of the image itself, due to which the Pillow module is not able to do anything about it. Here's the function that I've written so far:
# Handles all sent image files
#bot.message_handler(content_types=['photo'])
def image_resize(message):
sent_photo = message.photo[-1].file_id
bot.send_message(message.chat.id, "Enter the desired dimensions (WIDTHxHEIGHT), for example 300x150.")
#bot.message_handler(content_types=['text'])
def resize(message):
width, height = message.text.split("x")
im = Image.open(sent_photo)
resized_img = im.resize((width, height))
bot.send_photo(message.chat.id, resized_img)
The error that I'm getting:
im = Image.open(sent_photo)
File "/home/runner/imageResizertb/venv/lib/python3.8/site-packages/PIL/Image.py", line 3068, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'AgACAgUAAxkBAANyYnk0JGdGDY1_NbbYlfiYamTyrYMAAtmwMRvHg8lXDixv48nY6SYBAAMCAAN5AAMkBA'
Can anybody help me understanding how the API is handling the image file and how to manipulate it?
You are sending the File/Image ID to pillow. You'll need to save the file name somewhere or download the file into memory as such:
from io import BytesIO
#bot.message_handler(content_types=['text'])
def resize(message):
file = bot.get_file(sent_photo)
download_file = bot.download_file(file.file_path)
f = BytesIO(download_file)
im = Image.open(f)
resized_img = im.resize((width, height))
bot.send_photo(message.chat.id, resized_img)
I'm sorry, it may be a couple issues and not sure how to phrase the question properly. OK, tried a number of solutions over 2 days to no avail...thank you in advance for your help!
A photo is sent from an iPad app using POST API in the format of base64 (no meta data, just the base64 blob). I'm trying to simply decode and save locally.
I'm testing using Postman:
...com/api/register?first_name=John&photo=/9j/4AAQSkZJRgABAQAAAQAB...[base 64 image of about 400kb]
In Laravel, I am using Intervention
$jpg_url = "image-".time().".jpg";
$path = "/public/".$jpg_url;
$base=base64_decode($customer['photo']);
Image::make($base)->save($path);
and getting an "Unable to init from given binary data" error.
Here's what I don't quite understand and would appreciate a TIL5 explanation:
- When I save the POST from iPad directly into the DB with the following:
$photo = $customer->photo = $customer['photo']
The blob in mysql looks good, I can manually copy and paste it to a web decoder fine.
However, when I use postman, $photo has "+" in the base64 changed into spaces and the image doesn't render
Is this a datatype issue? I'm receiving a long blob that is trying to be converted into string? What is the best practice of receiving images from a mobile app?
I have a Cordova/PhoneGap app which uses the camera to capture a picture. I need to access this picture to send to my server.
For Android I simply use $cordovaFile.readAsDataURL(cordova.file.dataDirectory, fileName)
However; on iOS the filename does not come back as such. My return value for iOS shows the path as
assets-library://asset/asset.JPG?id=539425FE-43AA-48E7-9865-D76348208AC7&ext=JPG
How can I access and read this image? I'd prefer to stick with $cordovaFile but just want to get it to work.
I am using CordovaCameraPreview plugin and the best I can get back from the picture taker handler seems to be something formed similar to:
assets-library://asset/asset.JPG?id=990355E1-200A-4E35-AAA1-7D461E3999C8&ext=JPG
assets-library://asset/asset.JPG?id=C49FF0EB-CCCB-45B2-8577-B13868D8DB29&ext=JPG
How can I convert this to a filename and path that I can read with $cordovaFile? According to their documentation (http://ngcordova.com/docs/plugins/file/) it looks like I need to use one of their paths for File System Layout. It works fine on Android using cordova.file.dataDirectory but can't figure out how to access on iOS
I've also tried using window.resolveLocalFileSystemURL(path) but am getting undefined as my result.
Update
I feel like I'm getting closer... using window.resolveLocalFileSystemURL(path,resolveOnSuccess, resOnError); I get more information
name: "assets-library"
fullPath: "/asset/asset.JPG?id=711B4C9D-97D6-455A-BC43-C73059A5C3E8&ext=JPG"
name: "asset.JPG?id=711B4C9D-97D6-455A-BC43-C73059A5C3E8&ext=JPG"
nativeURL: "assets-library://asset/asset.JPG?id=711B4C9D-97D6-455A-BC43-C73059A5C3E8&ext=JPG"
It looks like I now need to use the fullPath but still can't figure out how to access with $cordovaFile
If I try to use $cordovaFile.readAsDataURL(cordova.file.dataDirectory, data.name) where data.name is asset.JPG?id=711B4C9D-97D6-455A-BC43-C73059A5C3E8&ext=JPG I get a file not found error
Update 2
I have tried using every single File System Layout available at http://ngcordova.com/docs/plugins/file/ and receive the same File Not Found error on each one. Still no clue how to access a file in assets-library using $cordovaFile
I had success with a simple substitution, like this
fullPath = fullPath.replace("assets-library://", "cdvfile://localhost/assets-library/")
It works in iOS 9.3
I was struggling with a similar issue for a while, but I figured it out today. I also thought that the FileSystem APIs didn't work with assets-libary:// URIs -- but that is not the case.
You should be able to get access to the File object (as well as the actual image name) by using the following code:
resolveLocalFileSystemURL('assets-library://asset/asset.JPG?id=711B4C9D-97D6-455A-BC43-C73059A5C3E8&ext=JPG', function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(event) {
console.log(event.target.result.byteLength);
};
console.log('Reading file: ' + file.name);
reader.readAsArrayBuffer(file);
});
});
As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in img tag to achieve streaming.
Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets.
The thing is, I am having some problems with encoding my data to PNG using OpenCV library that is already used in the project.
Firstly, some code:
websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix->rows * websocketBrokerStructure.matrix->cols * websocketBrokerStructure.matrix->step;
websocketBrokerStructure.imageDataSent = 0;
but I am getting strange error during execution of the second line:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
and I am a bit confused why I am getting this error from my code.
Also I am wondering if I understand it right: after invoking cvEncodeImage (where bgrImage is IplImage* with 3 channels - BGR) I just need to iterate through data member of my CvMatto get all of the png encoded data?
The cvEncodeImage function takes as its first parameter the extension of the image you want to encode. You are passing 0, which is the same thing as NULL. That's why you are getting the message NULL not valid.
You should probably use this:
websocketBrokerStructure.matrix = cvEncodeImage(".png", websocketBrokerStructure.bgrImageToSend, 0);
You can check out the documentation of cvEncodeImage here.
You can check out some examples of cvEncodeImage, or its C++ brother imencode here: encode_decode_test.cpp. They also show some parameters you can pass to cvEncodeImage in case you want to adjust them.
I am uploading Image on server using Alamofire only (without using any model class). I've tried multiple link for e.g upload image to server using Alamofire in every Link I stuck on this issue " Cannot invoke 'data' with an argument list of type '(using: String.Encoding)' " I do not understand what went wrong. kindly let me know below is my code:
Required parameters are
/*
name:test,
description:testing testing,
image:**filetype** ,
user_id:63,
add_by:63
*/
You should downcast the value as a string and not as anyobect.