I am trying to use PIL.Image.open to open the image from an opened json file. Should I just put the name of the png file inside the Image.open() or is it the directory that leads to the json file?
Thanks
Here is a full example which is probably what you're looking for:
import json
from PIL import Image
with open('yourfilename.txt', 'r') as f:
data = json.load(f)
# Load the file path from the json
imgpath = data['yourkey']
# Place the image path into the open method
img = Image.open(imgpath)
The imgpath variable should return a string of the path to your image.
Pillows Image.open docs
Related
I have service in my Django project's app, that upload images, and I need to convert all images to webp to optimize further work with these files on the frontend side.
Draft of _convert_to_webp method:
# imports
from pathlib import Path
from django.core.files import temp as tempfile
from django.core.files.uploadedfile import InMemoryUploadedFile
from PIL import Image
# some service class
...
def _convert_to_webp(self, f_object: InMemoryUploadedFile):
new_file_name = str(Path(f_object._name).with_suffix('.webp'))
temp_file = tempfile.NamedTemporaryFile(suffix='.temp.webp')
# FIXME: on other OS may cause FileNotFoundError
with open(temp_file 'wb') as f:
for line in f_object.file.readlines():
... # will it works good?
new_file = ...
new_f_object = InMemoryUploadedFile(
new_file,
f_object.field_name,
new_file_name,
f_object.content_type,
f_object.size,
f_object.charset,
f_object.content_type_extra
)
return new_file_name, new_f_object
...
f_object is InMemoryUploadedFile instance from POST request body (Django automatically create it).
My idea is to create a temporary file, write data from f_object.file.readlines() to it, open this file with PIL.Image.open and save with format="webp". Is this idea a good one or there is another way to make file converting?
I found a pretty clean way to do this using the django-resized package.
After pip installing, I just needed to swap out the imageField for a ResizedImageField
img = ResizedImageField(force_format="WEBP", quality=75, upload_to="post_imgs/")
All image uploads are automatically converted to .webp!
The solution was pretty simple. PIL.Image can be opened using file instance, so I just opened it using f_object.file and then saved it in BytesIO instance with optimization and compression.
Correctly working code:
# imports
from pathlib import Path
from django.core.files.uploadedfile import InMemoryUploadedFile
from PIL import Image
# some service class
...
def _convert_to_webp(self, f_object: InMemoryUploadedFile):
suffix = Path(f_object._name).suffix
if suffix == ".webp":
return f_object._name, f_object
new_file_name = str(Path(f_object._name).with_suffix('.webp'))
image = Image.open(f_object.file)
thumb_io = io.BytesIO()
image.save(thumb_io, 'webp', optimize=True, quality=95)
new_f_object = InMemoryUploadedFile(
thumb_io,
f_object.field_name,
new_file_name,
f_object.content_type,
f_object.size,
f_object.charset,
f_object.content_type_extra
)
return new_file_name, new_f_object
95% was chosen as balanced parameter. There was very bad quality with quality=80 or quality=90.
I see that there are two ways to download images using python-reuqests.
Uisng PIL as stated in docs (https://requests.readthedocs.io/en/master/user/quickstart/#binary-response-content):
from PIL import Image
from io import BytesIO
i = Image.open(BytesIO(r.content))
using streamed response content:
r = requests.get(url, stream=True)
with open(image_name, 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)
Which is the recommended wya to download images however? both have its merits I suyppose, and I was wondering what is the optimal approach.
I love the minimalist way. There is nothing called right way. It depends on the task you want to perform and the constraints you have.
import requests
with open('file.png', 'wb') as f:
f.write(requests.get(url).content)
# if you change png to jpg, there will be no error
I did use the below lines of code in a function to save images.
# import the required libraries from Python
import pathlib,urllib.request,os,uuid
# URL of the image you want to download
image_url = "https://example.com/image.png"
# Using the uuid generate new and unique names for your images
filename = str(uuid.uuid4())
# Strip the image extension from it's original name
file_ext = pathlib.Path(image_url).suffix
# Join the new image name to the extension
picture_filename = filename + file_ext
# Using pathlib, specify where the image is to be saved
downloads_path = str(pathlib.Path.home() / "Downloads")
# Form a full image path by joining the path to the
# images' new name
picture_path = os.path.join(downloads_path, picture_filename)
# Using "urlretrieve()" from urllib.request save the image
urllib.request.urlretrieve(image_url, picture_path)
# urlretrieve() takes in 2 arguments
# 1. The URL of the image to be downloaded
# 2. The image new name after download. By default, the image is
# saved inside your current working directory
Recently, I took a project. Converting a scanned PDF to searchable PDF/word using Python tesseract.
After few attempts, I could able to convert scanned PDF to PNG image files and afterwards, I'm struck could anyone please help me to convert the PNG files to Word/PDF searchable.my piece of code attached
Please find the attached image for reference.
Import os
Import sys
from PIL import image
Import pytesseract
from pytesseract import image_to_string
Libpath =r'_______' #site-package
Pop_path=r'_______' #poppler dlls
Sys.path.insert(0,LibPath)
from pdf2image import convert_from_path
Pdfpath=r'_______' # PDF file directory
imgpath=r'_______' #image output path
images= convert_from_path(pdf_path = pdfpath,
dpi=500, poppler_path= pop_path)
for idx, of in enumerate (images):
pg.save(imgPath+'PDF_Page_'+'.png',"PNG")
print('{} page converted'.format(str(idx)))
try:
from PIL import image
except ImportError:
import image
import pytesseract
def ocr-core(images):
Text =
pytesseract.image_to_string(image.open(images))
return text
print(ocr_core("image path/imagename))
that's it, I've written.....then I got multiple ".PNG" images...now I can only able to convert one PNG images to text.
How to convert all the images and save it in CSV/word?
from PIL import image
from pdf2image import convert_from_path
import pytesseract
import OS
import sys
Pdf_file_path = '_______' #your file path
Images = convert_from_path(Pdf_file_path, dpi=500)
Counter=1
for page in Images:
idx= "image_"+str(Counter)+".jpg" ##or ".png"
page.save(idx, 'JPEG')
Counter = Counter+1
file=Counter-1
Output= '_____' #where you want to save and file name
f=open(output, "w")
for i in range(1,file+1):
idx= "image_"+str(Counter)+".jpg" ##or ".png"
text=str(pytesseract.image_to_string(Image.open(idx)))
f.write(text)
f.close()
I have an uploaded file in memory. I want to manipulate the file with cv2. Currently, I write the file to disk then read it with cv2. How can I skip writing the file and load it directly with cv2?
file = request.files['file']
# if file and allowed_file(file.filename):
# save file
filename = secure_filename(file.filename)
file_path = os.path.join(dressrank.config['SHOW_IMG_FOLDER'], filename);
file.save(file_path)
img_path = file_path
# COLOR FATURE EXTRACTION
img = read_img(img_path)
img =img_resize(img, 500)
Build a numpy array using the uploaded data. Decode this array using cv2.
img = cv2.imdecode(numpy.fromstring(request.files['file'].read(), numpy.uint8), cv2.IMREAD_UNCHANGED)
Prior to OpenCV 3.0, use cv2.CV_LOAD_IMAGE_UNCHANGED instead.
See also: Python OpenCV load image from byte string
If working with BaseHTTPRequestHandler, one should first create a FieldStorage form:
fm = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST'})
then:
if "file" in fm:
image = cv2.imdecode(np.frombuffer(fm['file'].file.read(), np.uint8), cv2.IMREAD_UNCHANGED)
Also, note that fromstring is deprecated, and that's why I'm updating davidism's answer with frombuffer.
I'm taking uploaded files which I can save to S3 using SimpleS3:
from simples3.bucket import S3Bucket
upload = request.POST['image']
s = S3Bucket("cdn", s3.access_key, s3.secret_key)
s.put(upload.filename, upload.file.read())
Somewhere between saving that as a file and uploading it I save the file which is an image as a thumbnail using PIL or Imagemagick depending on what kind of image file was uploaded. The process there is to turn the File into an Image. My question is how do I open that Image as a file? I'm trying to upload the thumbnail to Amazon's S3 exactly as I do above. My code below is the idea of what I'm attempting:
thumb = self._im.copy() #where _im is the Image
s = S3Bucket("cdn", s3.access_key, s3.secret_key)
s.put(self.filename+ext, thumb)
I've tried with no success:
f = open(thumb, "rb")
s.put(self.filename+ext, f.read()
What does work, but is incredibly inefficient, is writing the file to the drive using the Image.save function and then opening it as a file:
thumb.save(self.filename+ext)
f = open(self.filename+ext, 'r')
s.put(self.filename+ext, f.read())
Figured it out:
from StringIO import StringIO
f = StringIO()
thumb.save(f, ext)
s.put(self.filename+ext, f.getvalue())
For ImageMagick case, if you are using PythonMagick, you can use underlying Blob class,
>>> from PythonMagick import Image, Blob
>>> image = Image("images/test_image.miff")
>>> image.magick("PNG")
>>> blob = Blob()
>>> image.write(blob)
>>> len(blob.data)
28282
>>> blob.data[0:20]
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x94'