Python display image - AttributeError - python

Hi I just got a Raspberry Pi, and I'm working on some very simple code here. What I'm trying to do is to capture an image and display it.
import PIL
import Image
import picamera
camera = picamera.PiCamera()
camera.capture("image.jpg")
im = Image.open("image.jpg")
im.show()
There was no error, but the image would not show up.
I checked the file, the picture was taken, so no problem on that part of the code.
Would really like some help, thanks!

Python is case-sensitive. You are importing Image so use it.
Change image to Image in im = image("image.jpg")
It'll become:
from PIL import Image
import picamera
camera = picamera.PiCamera()
camera.capture("image.jpg")
im = Image.open("image.jpg")
im.show()

Related

Loading Images from pillow into PyGame with BytesIO

Hi I'm trying to load my image from Pillow into Pygame using BytesIO.
from PIL import Image
import io
pilImage = Image.open('AgV6E.png')
temp_io = io.BytesIO()
pilImage.save(temp_io, format='PNG')
pygame.image.load(temp_io)
I get the following error:
pygame.image.load(temp_io) pygame.error: Unsupported image format
Strangely enough though simply saving to png works.
from PIL import Image
import io
pilImage = Image.open('AgV6E.png')
pilImage.save("test.png", format='PNG')
pygame.image.load("test.png")
Anybody know how to fix this?
Turns out I needed to run BytesIO.seek(0) before running load to set the stream position back to zero. Here is the updated code.
from PIL import Image
import io
pilImage = Image.open('AgV6E.png')
temp_io = io.BytesIO()
pilImage.save(temp_io, format='PNG')
temp_io.seek(0)
pygame.image.load(temp_io)
seek(0) is probably what allowed it to be read correctly as it moved the stream position back to the beginning.
Thanks to Starbuck5 letting me know the answer.

why google lens can't recognise QR made by pyqrcode on an image made by using PIL library?

I am working on a template kind of thing where the input are some images and text while the result being an image.
Here one of the input is a QR code generated using pyqrcode. I am using PIL library to paste images and text. Problem here is google lens is not recognizing the qr code.
What to do to solve this issue.
The code I written to generate image
from PIL import Image, ImageFont, ImageDraw
import pyqrcode
#Read the two images
image1 = Image.open('./10.jpg')
#image1.show()
image2 = Image.open('./2.jpg')
#image2.show()
#resize, first image,second image
image1 = image1.resize((320,640))
image2=image2.resize((144,144))
image1_size = image1.size
image2_size = image2.size
#pasting image2 on image1
image1.paste(image2,(100,220))
#Adding text
font1=ImageFont.truetype('./1.ttf',30)
font2=ImageFont.truetype('./1.ttf',20)
text1="My Home Jewels"
image3=ImageDraw.Draw(image1)
image3.text((30,30),text1,(237,230,211),font=font1)
#Adding QR code
link1="www.google.com"
url=pyqrcode.QRCode(link1,error='L')
url.png('test.png',scale=2)
img=Image.open('test.png')
#img.save('./qr.jpg',quality=100)
#img=img.resize((72,72))
image1.paste(img,(30,500))
#Adding link as text
image3=ImageDraw.Draw(image1)
image3.text((120,520),link1,(237,230,211),font=font2)
image1.show()
image1.save('./3.jpg',quality=100)
I cannot reproduce. This image works for me.

Preprocessing images for QR detection in python

I used Zbar and OpenCV to read the QR code in the image below but both failed to detect it. For ZBar, I use pyzbar library as the python wrapper. There are images that QR is detected correctly and images really similar to the successful ones that fail. My phone camera can read the QR code in the uploaded image which means it is a valid one. Below is the code snippet:
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
import cv2
# zbar
results = decode(cv2.imread(image_path), symbols=[ZBarSymbol.QRCODE])
print(results)
# opencv
qr_decoder = cv2.QRCodeDetector()
data, bbox, rectified_image = qr_decoder.detectAndDecode(cv2.imread(image_path))
print(data, bbox)
What type of pre-processing will help to increase the rate of success for detecting QR codes?
zbar, which does some preprocessing, does not detect the QR code, which you can test running zbarimg image.jpg.
Good binarization is useful here. I got this to work using the kraken.binarization.nlbin() function of the Kraken library. The library is for OCR, but works very well for QR codes, too, by using non-linear processing. The Kraken binarization code is here.
Here is the code for the sample:
from kraken import binarization
from PIL import Image
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
image_path = "image.jpg"
# binarization using kraken
im = Image.open(image_path)
bw_im = binarization.nlbin(im)
# zbar
decode(bw_im, symbols=[ZBarSymbol.QRCODE])
[Decoded(data=b'DE-AAA002065', type='QRCODE', rect=Rect(left=1429, top=361, width=300, height=306), polygon=[Point(x=1429, y=361), Point(x=1429, y=667), Point(x=1729, y=667), Point(x=1723, y=365)])]
The following picture shows the clear image of the QR code after binarization:
I had a similar issue, and Seanpue's answer got me on the right track for this problem. Since I was already using the OpenCV library for image processing rather than PIL, I used it to perform Otsu's Binarization using the directions in an OpenCV tutorial on Image Thresholding. Here's my code:
import cv2
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
image_path = "qr.jpg"
# preprocessing using opencv
im = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
blur = cv2.GaussianBlur(im, (5, 5), 0)
ret, bw_im = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# zbar
decode(bw_im, symbols=[ZBarSymbol.QRCODE])
[Decoded(data=b'DE-AAA002065', type='QRCODE', rect=Rect(left=1429, top=362, width=300, height=305), polygon=[Point(x=1429, y=362), Point(x=1430, y=667), Point(x=1729, y=667), Point(x=1724, y=366)])]
Applying the gaussian blur is supposed to remove noise from the picture to make the binarization more effective, but for my application it didn't actually make much difference. What was vital was to convert the image to grayscale to make the threshold function work (done here by opening the file with the cv2.IMREAD_GRAYSCALE flag).
QReader use to work quite well for these cases.
from qreader import QReader
import cv2
if __name__ == '__main__':
# Initialize QReader
detector = QReader()
img = cv2.cvtColor(cv2.imread('92iKG.jpg'), cv2.COLOR_BGR2RGB)
# Detect and Decode the QR
print(detector.detect_and_decode(image=img))
This code output for this QR:
DE-AAA002065

Import image in python 3 with pillow

For a few hours now, I've been trying to figure out how to view an image I imported with pillow. So far, I got rid of all errors, but it just shows the exact filename of the photo, the image isn't showing up, please help me fix this.
This is what I have so far:
from PIL import Image
img = Image.open("image.jpg")
print(img)
From the docs the function to show an image is
Image.show(title=None, command=None)
So to display an image you can do
from PIL import Image
img = Image.open("image.jpg")
img.show()
Sir Please try this code
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
a=Image.open("F:\\FYP DATASET\\images\\train\\10_left.jpeg")
a
try by removing print statement and adding matplotlib library.
Purpose of %Matplotlib inline in Python:
By just writing variable without print statement will display the image in notebook and by img.show() It will display image in Photo Viewer (Windows photo viwer) etc depends on what you are using.
from PIL import Image
img = Image.open("man.png")
img.show()

How to convert a Pyglet image to a PIL image?

i want to convert a Pyglet.AbstractImage object to an PIL image for further manipulation
here are my codes
from pyglet import image
from PIL import Image
pic = image.load('pic.jpg')
data = pic.get_data('RGB', pic.pitch)
im = Image.fromstring('RGB', (pic.width, pic.height), data)
im.show()
but the image shown went wrong.
so how to convert an image from pyglet to PIL properly?
I think I find the solution
the pitch in Pyglet.AbstractImage instance is not compatible with PIL
I found in pyglet 1.1 there is a codec function to encode the Pyglet image to PIL
here is the link to the source
so the code above should be modified to this
from pyglet import image
from PIL import Image
pic = image.load('pic.jpg')
pitch = -(pic.width * len('RGB'))
data = pic.get_data('RGB', pitch) # using the new pitch
im = Image.fromstring('RGB', (pic.width, pic.height), data)
im.show()
I'm using a 461x288 image in this case and find that pic.pitch is -1384
but the new pitch is -1383
This is an open wishlist item:
AbstractImage to/from PIL image.

Categories