QRCode generated in python - python

I wrote some code to generate qrcode in python but at run time it shows this error:
Import "qrcode" could not be resolved Pylance(reportMissingImports)
I am using the qrcode library but I am having some problems.

Installation is pretty straight forward via pip install. Run the following command to install python-qrcode and pillow.
pip install qrcode[pil]
Once you are done, continue installing OpenCV-Python with the following command:
pip install opencv-python
If you intend to detect multiple QR codes in a single image, make sure that the opencv-python version is at least 4.3.0. Older versions do not come with the multi detection functionalities.
Add the following import declaration at the top of your Python file.
import qrcode
from PIL import Image
For basic usage, you can simply run the make() function to generate a QR Code based on your input text:
img = qrcode.make('Your input text')
You can utilize the QRCode class, which comes with a lot more controls and properties.
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
The next step is to call the add_data() function. Pass in the input text of your choice. Continue by appending the following code to generate a QR code with a white background and black fill.
qr.add_data('https://medium.com/#ngwaifoong92')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
You can save it in as an image file as follow:
img.save("sample.png")
Finally you can read this paper.

Related

How do you insert an image into a Python program?

I'm trying to insert an image into a Python program, but I just can't figure out how to link the image to code so I can view it once the code is run. I am new to python, I have version 3.9.7 on a Mac computer.
Thanks in advance to anyone who can give me a hand in solving the problem.
Greetings, Gaia.
You can use OpenCV for this
https://learnopencv.com/read-display-and-write-an-image-using-opencv/
You need to install the library first, running the following command on your terminal pip install opencv-python
# import the cv2 library
import cv2
# The function cv2.imread() is used to read an image.
img_grayscale = cv2.imread('test.jpg',0)
# The function cv2.imshow() is used to display an image in a window.
cv2.imshow('graycsale image',img_grayscale)
# waitKey() waits for a key press to close the window and 0 specifies indefinite loop
cv2.waitKey(0)
# cv2.destroyAllWindows() simply destroys all the windows we created.
cv2.destroyAllWindows()
# The function cv2.imwrite() is used to write an image.
cv2.imwrite('grayscale.jpg',img_grayscale)
You can use theses modules:
opencv-python:
import cv2
Pillow:
from PIL import Image
scikit-image
import skimage

Hello, i want to perform OCR on image i tried the code

I am performing OCR on an imenter image description hereage, for that i installed pytesseract library, code i have used is requiring one more library "Image", what would be the cmd code in windows for installation that package. I have tried --> "pip install image" that's not working, or is this code right
code is this
import image
import pytesseract
print pytesseract.image_to_string(image.open('ocr.jpg'))
May be the error is appearing because you used lower case image.open while in documentation its Image.open
Try this:
import Image
import pytesseract
# Simple image to string
print(pytesseract.image_to_string(Image.open('ocr.jpg')))

I am not able to import an image into python using the import image way

Before you ask, I did try putting an image in it, and it didn't work like look at this.
import Image
Image = Image.open('clerky.jpeg')
Image.show()
and this code above comes up with this error below.
import Image
ImportError: No module named 'Image'
If you're using Pillow, the correct usage is
from PIL import Image
To install Pillow on Windows, start the Command Prompt application (or hit WinR and type cmd, then hit Enter). Type
pip install Pillow
hit Enter, and everything should install automatically.
I think you mean from PIL import Image
from PIL import Image
PIL (Python Image Library) has to be installed on your system, AKA "pil" and "Pillow" from time to time. effbot.org/imagingbook/pil-index.htm

Python +pyodbc Retrieving images from MSSQL data. HOW to?

Sorry I don't show any piece of code here but I simply don't know how to start?
Ok I simply selected image cell and got file of bytearray:
"[bytearray(b'\xff\xd8\xff\xe1\x1a1Exif\x00\x00II*\x00\x08\x00\x00\x00\x10\x00\x00\x01\x03\x00\x01\x00\x00\x00\x08\x01\x00\ (...) etcetc )]"
But how to transform it and save as image?
Here's the answer:
from PIL import Image
import os,io
.....
image = Image.open(io.BytesIO(my_bytearray))
image.save(path_to_image)
PIL must be installed separately by
pip -m install pillow (I'm not certain if -m is necessary ;) )

Strange Pillow exception while saving cropped image

We have the following code:
img = Image.open(FileSystemStorage().path(relpath))
coords = [
cd['crop_start_x'],
cd['crop_start_y'],
cd['crop_end_x'],
cd['crop_end_y']
]
cropped_img = img.crop(coords)
cropped_path = "%s-cropped%s" % os.path.splitext(relpath)
tasks.delete_temporary_file.delay(fss.path(relpath))
cropped_img.save(fss.path(cropped_path))
When trying to save the cropped image, we get a strange "Not a valid number of quantization tables. Should be between 1 and 4." exception, just in one of our environments.
The strangest part is that the code might work sometimes even though the crop or the image doesn't change
Has anyone a lead on this?
We are using Pillow 2.8.1, python 2.7.6 and Ubuntu server 12.04
Basically, the problem originated on a conflicting PIL installation in one of the app servers. It was hard to find since they were hiding behind a load balancer, so the error would pop out sometimes
When we issued pip freeze on the console, we found out that in one of the servers we had both PIL and Pillow installed.
Upon removing both of them and reinstalling Pillow, we solved the issue.
Just to make it clear:
pip uninstall PIL
pip uninstall Pillow
pip install Pillow
And then, just restart the web server.
As others have stated, another possible cause is the use of:
import Image
That statement works only for PIL and should be avoided.
We should always use:
from PIL import Image

Categories