first of all, I'm apology for the title. I dont know how to explain this so i write the title like this.
I want to check the Content-Type and if it is image/ run these code
from PIL import Image
import io, uuid, requests
class LinkCreateView(CreateView):
model = Link
form_class = LinkForm
def form_valid(self, form):
headers = {'content-type': 'image/'}
r = requests.get(form.instance.url, headers=headers)
i = Image.open(io.BytesIO(r.content)).convert('RGB')
image_io = io.BytesIO()
try:
crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65, 65), bleed=0.5, centering=(0.5, 0.5))
hash = str(uuid.uuid1())
name = "{}.jpeg".format(hash)
crop_image.save(image_io, format="jpeg")
form.instance.img.save(name, ContentFile(image_io.getvalue()))
finally:
image_io.close()
f = form.save(commit=False)
f.rank_score = 0.0
f.submitter = self.request.user
f.save()
return super(CreateView, self).form_valid(form)
If it is text/html then run these code;
from PIL import Image
import io, uuid, requests, lxml.html
url = ("http://website.com")
page = requests.get(url)
hash = str(uuid.uuid1())
tree = lxml.html.fromstring(page.content)
og = tree.xpath('/html/head/meta[@property="og:image"][1]/@content')
r = requests.get(og[0])
i = Image.open(io.BytesIO(r.content)).convert('RGB')
crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65,65), bleed=0.5, centering=(0.5, 0.5))
crop_image.save("{}.jpeg".format(hash))
I tried if, else things but it didn't work. I dont know how to implement this second piece of code to first one. Both code work perfectly by itself.
I tried like these code:
if page.headers['Content-Type'] == 'text/html:
.....
elif page.headers['Content-Type'] == 'image/':
.....
Thanks to LearnerEarner my problem is solved. Working code is below
class LinkCreateView(CreateView):
model = Link
form_class = LinkForm
def form_valid(self, form):
r = requests.get(form.instance.url)
image_io = io.BytesIO()
hash = str(uuid.uuid1())
try:
if r.headers['Content-Type'].startswith("image/"):
i = Image.open(io.BytesIO(r.content)).convert('RGB')
crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65, 65), bleed=0.5, centering=(0.5, 0.5))
name = "{}.jpeg".format(hash)
crop_image.save(image_io, format="jpeg")
form.instance.img.save(name, ContentFile(image_io.getvalue()))
elif r.headers['Content-Type'].startswith("text/html"):
tree = lxml.html.fromstring(r.content)
og = tree.xpath('/html/head/meta[@property="og:image"][1]/@content')
r = requests.get(og[0])
i = Image.open(io.BytesIO(r.content)).convert('RGB')
crop_image = ImageOps.fit(i, method=Image.ANTIALIAS, size=(65, 65), bleed=0.5, centering=(0.5, 0.5))
name = "{}.jpeg".format(hash)
crop_image.save(image_io, format="jpeg")
form.instance.img.save(name, ContentFile(image_io.getvalue()))
finally:
image_io.close()
f = form.save(commit=False)
f.rank_score = 0.0
f.submitter = self.request.user
f.save()
return super(CreateView, self).form_valid(form)
I tried like these code:
if page.headers['Content-Type'] == 'text/html: ..... elif page.headers['Content-Type'] == 'image/': .....
As far as my knowledge goes there is no generic Content-Type like 'image/'(unless you are explicitly sending such Content-Type in response headers). The MIME Type has to be specific.
Print and observe the response headers from your requests.get
to see the exact Content-Type before you use it in the else condition.
If all that you expect are HTML and images, you need not have a specific condition for images. You can try something like this:
if page.headers['Content-Type'] == 'text/html':
....
else:
try:
#treat it as image and proceed
except:
#may be not an image
OR
You can do this:
if page.headers['Content-Type'] == 'text/html:
.....
elif page.headers['Content-Type'].startswith("image/"):
.....
Please refer to this list for MIME Types .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.