Jump to content
Phishing Emails & YouTube Messages - Fake Giveaway Read more... ×
Search In
  • More options...
Find results that contain...
Find results in...
EvilCat70

Save image as file from URL (PlL, Python)

Recommended Posts

Posted · Original PosterOP

Hello. I am wanting to get an image from a URL using PIL and then convert the image object that I have to a file that I can then use to set as the desktop background on Windows. I have figured out how to set the desktop background using a fixed absolute path (E.g C:\Users\<My User>\Desktop\<Image file>) but I need to figure out how to either set it with an Image object from PIL or how to save the Image object I get from PIL to a file on disk.

 

Here is my current code for getting an image:

image.png.c931d160be96d4b35df102649bf49977.png

Ignore the whole time sleeping stuff and opening the browser, that is irrelevant.

 

I currently use the Requests module to do a HTTP request to the website. I then open an Image using the content of that URL and store it in img. But how do I put the contents of that Image object into something I can use to set the desktop background?

 

Help is appreciated, thanks.


CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites
Image.save(fp, format=None, **params)

https://pillow.readthedocs.io/en/4.2.x/reference/Image.html


¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 19 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
Posted · Original PosterOP
4 minutes ago, BobVonBob said:

Image.save(fp, format=None, **params)

https://pillow.readthedocs.io/en/4.2.x/reference/Image.html

Image.save doesn't seem to pop up when I type that in. I'm using Visual Studio and maybe it's being buggy?


CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites
7 minutes ago, EvilCat70 said:

Image.save doesn't seem to pop up when I type that in. I'm using Visual Studio and maybe it's being buggy?

Image is the image object, so img.save in your case.

 


¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 19 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
Posted · Original PosterOP
20 minutes ago, BobVonBob said:

Image is the image object, so img.save in your case.

 

image.thumb.png.8187ef5a12285bdb00ac5643129f4b97.png

this happened. I don't know why.


CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites
1 minute ago, EvilCat70 said:

-snip-

Look at the error message. "ValueError: unknown file extension" and above that "format = EXTENSION[ext]". In other words, specify the correct format, None is a default.


¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 19 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
Posted · Original PosterOP
1 minute ago, BobVonBob said:

Look at the error message. "ValueError: unknown file extension" and above that "format = EXTENSION[ext]". In other words, specify the correct format, None is a default.

Yes I fixed that by putting the file name as "image.jpg". However, now when I try to set the desktop background using that file, it is setting it to solid black. I've checked the image file and the contents are there as they should be, it's not just a black image. I've made sure the path is correct by doing this:

image.thumb.png.930d8de963a1dcbc426efa6c7b10bea2.png

As you can see, the path that gets printed is exactly where the image is being saved.

image.thumb.png.692943e47cac093f8c1a5acbb7e9af3e.png

 

What's going wrong?


CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites

Not even the slightest clue. Maybe a race condition? The save hasn't actually written the file when you try to change it? Try a short sleep between the save and loading the wallpaper. If that's not it I'm stumped. Here's 3 stackexchange topics about a seemingly similar issue.

 

https://stackoverflow.com/questions/21715895/creating-a-background-changer-in-python-with-ctypes-not-working

https://stackoverflow.com/questions/27726236/why-wont-this-ctypes-code-work-with-python-3-3-but-will-work-with-python-2-7/27726289#27726289

https://stackoverflow.com/questions/21715895/creating-a-background-changer-in-python-with-ctypes-not-working/30018577#30018577


¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 19 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

Link to post
Share on other sites
Posted · Original PosterOP
45 minutes ago, BobVonBob said:

Not even the slightest clue. Maybe a race condition? The save hasn't actually written the file when you try to change it? Try a short sleep between the save and loading the wallpaper. If that's not it I'm stumped. Here's 3 stackexchange topics about a seemingly similar issue.

 

https://stackoverflow.com/questions/21715895/creating-a-background-changer-in-python-with-ctypes-not-working

https://stackoverflow.com/questions/27726236/why-wont-this-ctypes-code-work-with-python-3-3-but-will-work-with-python-2-7/27726289#27726289

https://stackoverflow.com/questions/21715895/creating-a-background-changer-in-python-with-ctypes-not-working/30018577#30018577

I was able to get everything working! The issue was that the paths weren't exactly the same, but now the path is always correct!


CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×