I wanted read a image using PIL.Image.open().But I've image in different path.
The following is the path I've the python script
"D:\YY_Aadhi\holy-edge-master\hed\test.py"
The following is the path I've the image file.
"D:\YY_Aadhi\HED-BSDS\test\2018.jpg"
from PIL import Image
'''some code here'''
image = Image.open(????)
How should I fill the question mark to access the image file.
you can simply do
from PIL import Image
image = Image.open("D:\\YY_Aadhi\\HED-BSDS\\test\\2018.jpg")
or
from PIL import Image
directory = "D:\\YY_Aadhi\\HED-BSDS\\test\\2018.jpg"
image = Image.open(directory)
like this.
you have to write escape sequence twice in windows, when you want to define as directory. and It will be great if you try some stupid code. It helps you a lot.
Does this image = Image.open("D:\YY_Aadhi\HED-BSDS\test\2018.jpg") not do the trick?
You can use this to read an online image
from urllib.request import urlopen
url = 'https://somewebsite/images/logo.png'
msg_image = urlopen(url).read()
Related
How to read the image through opencv and send it to zxing for detection instead of using the image path?
reader = zxing.BarCodeReader()
qrcode = reader.decode(img_path)
I want to use the code as below, but I won't do it in detail.
reader = zxing.BarCodeReader()
qrcode = reader.decode(cv2.imread(img_path))
zxing supports PIL images so you can try:
qrcode = reader.decode(Image.open(img_path))`
If you need to use opencv images, first convert them to PIL:
qrcode = reader.decode(Image.fromarray(cv2.imread(img_path)))
Or you can write them to a temporary file, what zxing is actually doing if you pass a PIL image.
Links:
pillow
zxing code
I wrote the following code to resize images in a folder to 100*100 and save the images in the same folder using for loop.I am wondering why it isn't working.
The following is the code I have written:
import cv2
import glob
images=glob.glob("*.jpg")
for image in images:
img=cv2.imread(image,0)
re=cv2.resize(img,(100,100))
cv2.imshow("Hey",re)
cv2.waitKey(500)
cv2.destroyAllWindows()
cv2.imwrite("resized_"+image,re)
I executed this:
nsu#NSU:~/Desktop/cryptology$ python3 img2.py
I got no error:
nsu#NSU:~/Desktop/cryptology$ python3 img2.py
nsu#NSU:~/Desktop/cryptology$
But the folder where i have saved the images and code is as it is...
what should i do?
**A viewer posted an answer which resulted the same.
**The problem MAY not be with the code.
**Please consider this
If you like you can use very robust module of python called Pillow for image manipulation, so first do pip3 install pillow
then run this code
from PIL import Image
import glob
images=glob.glob("*.jpg")
for im in images:
# print(im)
img = Image.open(im)
img = img.resize((100, 100), Image.ANTIALIAS)
img.save(im+'_resized.jpg')
hope it helps ... you need to improve the code if you want to keep the aspect ratio of the image
I'm trying to use the python image library to import an image. I keep getting an error that says the file or directory doesn't exist when I run this code:
from PIL import Image
img = Image.open("Users/tylercordeiro/hello/sunrisesunsettime.jpeg")
Am I doing anything wrong?
Is there a specific way that I need to put in the name of my file/directory?
Try with an absolute path. I have added a / or slash at the beginning of the file name.
from PIL import Image
im = Image.open("/Users/tylercordeiro/hello/sunrisesunsettime.jpeg")
im
Hello i need help with a very simple Python 3 script. In the script i try open a png image file from a folder with this:
png = Image.open('img/image.png', + 'r')
It's work good, but now i need get random .png images from same folder, but after some unsuccessful try, I'll like know how do it, actually i use in my script
from random import randint
import uuid
from PIL import Image
Any help will be appreciated, thank you all
import glob
import random
from PIL import Image
img = random.choice(glob.glob('img/*.png'))
png = Image.open(img, + 'r')
import os,random
from PIL import Image
random_image = random.choice(os.listdir("img"))
Image.open(random_image)
I've been playing around with Pythonista on iOS to create some automation scripts.
I have a problem where I'm trying to grab an animated gif from a remote url. I've come up with the following script.
import Image
from urllib import urlopen
from io import BytesIO
url = "http://someurl.com/funny.gif"
img = Image.open(BytesIO(urlopen(url).read()))
I get the image but it only appears to be the first frame of the gif? I'm guessing it has something to do with the BytesIO not reading in the whole file but I'm not sure?
Hope I'm along the right lines.
You're almost there. You use img.seek to advance frames. So..
import Image
from urllib import urlopen
from io import BytesIO
url = 'http://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif'
img = Image.open(BytesIO(urlopen(url).read()))
# Start with first frame
img.seek(0)
#img.show()
# Advance by one
img.seek(img.tell() + 1)
#img.show()
Here's a SO post showing how to save a gif using the Image class.
According to Pillow Manual:
To save all frames, the save_all parameter must be present and set to True.
So, opened image could be save by:
image.save('filename.gif', save_all=True)