Jump to content

Save image as file from URL (PlL, Python)

RileyTheFox

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 comment
Share on other sites

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-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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

×