record a web page using python [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to attempt creating a program in python but I don't know how to start. I want the program to do the following :
goes to a specific website URL
Take a 30 seconds recording or video of the entire webpage displayed.
Saves the recording in a video file.
Is it possible to achieve this, and what libraries would I use in python to create a program that does this?
I had an idea that opencv-python could be one of the libraries I could use, is it possible?
Thanks.

For opening a specific URL, you could use the module "webbrowser":
import webbrowser
webbrowser.open('http://example.com') # Go to example.com
For recording the page you could install the modules "opencv-python", "numpy" and
"pyautogui":
pip3 install opencv-python numpy pyautogui
And then use them all to get the final code, which could look something like this:
import cv2
import numpy as np
import os
import pyautogui
import webbrowser
webbrowser.open('http://example.com') # Go to example.com
output = "video.avi"
img = pyautogui.screenshot()
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
#get info from img
height, width, channels = img.shape
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output, fourcc, 20.0, (width, height))
for i in range(95):
try:
img = pyautogui.screenshot()
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
out.write(image)
StopIteration(0.5)
except KeyboardInterrupt:
break
print("finished")
out.release()
cv2.destroyAllWindows()
The file should save as "video" and should be runnable. Your screen resolution should be detected automatically during a screenshot. If there are any issues within this code, feel free to tell it to me.

Related

Convert JPEG blob into an image [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I get values from the Oracle blob, below.
It's a JPEG file that somebody stored as a signed char, rather than unsigned.
How can I convert it into a image?
[-1,-40,-1,-32,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,-1,-37,0,67,0,1,...]
View raw data on Pastebin
It depends whether you mean you want to save the data to disk as a JPEG or further process it in memory as an image.
As it is, the image has been (incorrectly) stored as a signed char instead of as an unsigned char - I can tell that because there are negative values in there. I can tell it is a JPEG because JPEGs have the string JFIF near the start and J=74, F=70 and I=73 in ASCII - you will see those values near the start.
So, first I did a list comprehension to correct the values like this:
# Data from pastebin
im = [-1,-40,-1,-32,0,16,74,70,73,70,0...]
# Correct negative values to be "256+current"
corrected = [256+x if x<0 else x for x in im]
Then, assuming you want to write the image to disk, it is already JPEG-encoded so we just need to convert the list to bytes and write those to disk
#!/usr/bin/env python3
# Data from pastebin
im = [-1,-40,-1,-32,0,16,74,70,73,70,0 ...]
# Correct negative values to be "256+current"
corrected = [256+x if x<0 else x for x in im]
# Convert corrected data to bytes and save to disk as "result.jpg"
with open('result.jpg', 'wb') as f:
f.write(bytes(corrected))
If, on the other hand, you want to make a PIL Image and process it, you can wrap the bytes in a BytesIO and ask PIL to open that:
from PIL import Image
from io import BytesIO
# Data from pastebin
im = [-1,-40,-1,-32,0,16,74,70,73,70,0 ...]
# Correct negative values to be "256+current"
corrected = [256+x if x<0 else x for x in im]
# Convert the corrected list to "bytes", wrap in BytesIO and ask PIL to open as PIL Image
pImage = Image.open(BytesIO(bytes(corrected)))
# Now we can display it
pImage.show()
# Or save it
pImage.save('result.jpg')
Note that if you prefer to use OpenCV rather than PIL, you would use cv2.imdecode() in place of PIL Image.open().
Keywords: Python, image processing, convert list of numbers to image, convert blob to image, convert Oracle database blob to image.

Image not Displaying even after following effbot [duplicate]

This question already has an answer here:
PhotoImage not showing up the image associated with it
(1 answer)
Closed 3 years ago.
I'm trying to add in an image to a screen and its not displaying.
Followed other Stack overflow solutions and the Effbot solution and neither worked. Ive moved the image around and it still isnt displaying so its not where its displaying. Not getting any Error messages either.
image = PhotoImage("newspaper-extra-computer-icons-breaking-newsnewspaper.jpg")
image_label = Label(news_aggregator,image = image)
image_label.image = image
image_label.place(x=400,y = 200)
Just expecting the image to be displayed.
The main problem is you are missing file= in PhotoImage().
Try this:
PhotoImage(file='path_to_image.gif')
That said tkinter only supports a hand full of formats.
The PhotoImage class can read GIF and PGM/PPM images from files
For working with other formats you will need PIL.
If you need to work with other file formats, the Python Imaging Library (PIL) contains classes that lets you load images in over 30 formats, and convert them to Tkinter-compatible image objects:
You can see all there here on the documentation.
For your file try this:
import tkinter as tk
from PIL import ImageTk
root = tk.Tk()
image = ImageTk.PhotoImage(file="newspaper-extra-computer-icons-breaking-newsnewspaper.jpg")
image_label = tk.Label(root, image=image)
image_label.image = image
image_label.place(x=400, y=200)
root.mainloop()
Keep in mind due to you using place() the above code will not be showing the image as it is off frame. You will need to expand the window.

How do i close a figure shown using matplotlib in python ? And What is difference among image, figure and picture?

I am using ubuntu. I want to close a figure shown using matplotlib after few seconds without using keyboard or mouse. I am able to close an image shown using PIL after few seconds by getting its process id and then kill it.
And i am also little bit confused among terms figure, image and picture in matplotlib.
Thank you so much in advance.
Regarding part 1.
i have used plt.close(), plt.close("all") as well as 'psutil' library to fetch process ID and kill. But none of them worked. I got only solution of closing an image opened via 'PIL'.
link :-
How can I close an image shown to the user with the Python Imaging Library?
Regarding part 2.
Actually, at some pages, i found the terms 'figure','picture' and 'image' were used interchangeably; and at some pages they were not. I saw 'plt.imshow()' is used for image and picture and 'plt.show()' is used for figure. But, what is difference between figure, image and picture. And when to use these functions?
link :-
Why plt.imshow() doesn't display the image?
# for graphing
import matplotlib.pyplot as plt
import time
# for process
import psutil
# importing for image processing
from PIL import Image
#### closing an image which was opened via PIL
#### working perfectly
filename = "check.jpg"
img = Image.open(filename)
img.show()
time.sleep(5)
# for killing process such that image viewer
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
#### closing an image/figure which was opened via matplotlib
#### unable to close without keyboard or mouse
x = [[1,2,3,4],[11,22,33,44],[9,8,7,6]]
print (x)
plt.imshow(x)
plt.colorbar()
plt.title("a")
plt.xlabel('b')
plt.ylabel('c')
a = plt.show()
time.sleep(2)
## not working
plt.close()
## not working
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
## not working
plt.close("all")
i expect that my shown figure closes automatically after a few seconds,
instead of any manual intervention.

How to overlay images on each other in python and opencv?

I am trying to write images over each other. Ideally, what I want to do is to write every image in one folder over every image in another folder and output every unique image to another folder. So far, I am just working on having one image write over one image, but I can't seem to get that to work.
import numpy as np
import cv2
import matplotlib
def opencv_createsamples():
mask = ('resized_pos/2')
img = cv2.imread('neg/1')
new_img = img * (mask.astype(img.dtype))
cv2.imwrite('samp', new_img)
opencv_createsamples()
It would be helpful to have more information about your errors.
Something that stands out immediately is the lack of file type extensions. Your images are probably not being read correctly, to begin with. Also, image sizes would be a good thing to consider so you could resize as required.
If the goal is to blend images, considering the alpha channel is important. Here is a relevant question on StackOverflow:How to overlay images in python
Some other OpenCV docs that have helped me in the past: https://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html
https://docs.opencv.org/3.1.0/d5/dc4/tutorial_adding_images.html
Hope this helps!

(PYTHON) Manipulating certain portions of URL at user's request [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The download link I want to manipulate is below:
http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/USWC/6km/hourly/RTV/HFRADAR,_US_West_Coast,_6km_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&north=47.20&west=-126.3600&east=-123.8055&south=37.2500&horizStride=1&time_start=2015-11-01T00%3A00%3A00Z&time_end=2015-11-03T14%3A00%3A00Z&timeStride=1&addLatLon=true&accept=netcdf
I want to make anything that's in bold a variable, so I can ask the user what coordinates and data set they want. This way I can download different data sets by using this script. I would also like to use the same variables to name the new file that was downloaded ex:USWC6km20151101-20151103.
I did some research and learned that I can use the urllib.parse and urllib2, but when I try experimenting with them, it says "no module named urllib.parse."
I can use the webbrowser.open() to download the file, but manipulating the url is giving me problems
THANK YOU!!
Instead of urllib you can use requests module that makes downloading content much easier. The part that makes actual work is just 4 lines long.
# first install this module
import requests
# parameters to change
location = {
'part': 'USWC',
'part2': '_US_West_Coast',
'km': '6km',
'north': '45.0000',
'west': '-120.0000',
'east': '-119.5000',
'south': '44.5000',
'start': '2016-10-01',
'end': '2016-10-02'
}
# this is template for .format() method to generate links (very naive method)
link_template = "http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/{part}/{km}/hourly/RTV/\
HFRADAR,{part2},_{km}_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&\
north={north}&west={west}&east={east}&south={south}&horizStride=1&\
time_start={start}T00:00:00Z&time_end={end}T16:00:00Z&timeStride=1&addLatLon=true&accept=netcdf"
# some debug info
link = link_template.format(**location)
file_name = location['part'] + location['km'] + location['start'].replace('-', '') + '-' + location['end'].replace('-', '')
print("Link: ", link)
print("Filename: ", file_name)
# try to open webpage
response = requests.get(link)
if response.ok:
# open file for writing in binary mode
with open(file_name, mode='wb') as file_out:
# write response to file
file_out.write(response.content)
Probably the next step would be running this in loop on list that contains location dicts. Or maybe reading locations from csv file.

Resources