Downloading .JPG from web

Post

Posted
Rating:
#1 (In Topic #795)
Regular
Diverod is in the usergroup ‘Regular’
I'm trying to download files / images from the web. I test them first in my browser to make sure they are accessible. I can get some to work with no problems, but there is one that's kick'n me down the road.

This text file works fine:

Code

File.save(ImageFilePath &/ "test.txt", HttpClient.Download("https://moz.com/robots.txt"))

This jpg works fine:

Code

File.save(ImageFilePath &/ "testPic.jpg", HttpClient.Download("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Gamba.JPG/90px-Gamba.JPG"))

This one is thumb'n it's nose at me:

Code

File.save(ImageFilePath &/ "test4.jpg", HttpClient.Download("https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/15/20213631716_GOES16-ABI-se-15-1200x1200.jpg"))
I can't get it to work with curl or wget from terminal either, all it returns is:

Code

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
I've also assigned a UserAgent but it didn't help. Is the returned text telling me something? Any guidance or direction would be appreciated. Thanks
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’

Diverod said

I can't get it to work with curl or wget
…if you want to try extern Curl library :)  with Gambas:

Code (gambas)

  1. Library "libcurl:4.8.0"
  2.  
  3. Private Const CURLOPT_WRITEDATA As Integer = 10001
  4. Private Const CURLOPT_URL As Integer = 10002
  5.    
  6. ' CURL *curl_easy_init(void)
  7. ' Start a libcurl easy session.
  8. Private Extern curl_easy_init() As Pointer
  9.  
  10. ' CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...)
  11. ' Set options for a curl easy handle
  12. Private Extern curl_easy_setopt(curl As Pointer, optionI As Integer, optionP As Pointer) As Integer
  13.  
  14. ' CURLcode curl_easy_perform(CURL *curl)
  15. ' Perform a blocking file transfer.
  16. Private Extern curl_easy_perform(curl As Pointer) As Pointer
  17.  
  18. ' const char *curl_easy_strerror(CURLcode)
  19. ' Turn a CURLcode value into the equivalent human readable error string.
  20. Private Extern curl_easy_strerror(CURLcode As Integer) As String
  21.  
  22. ' void curl_easy_cleanup(CURL *curl)
  23. ' End a libcurl easy handle.
  24. Private Extern curl_easy_cleanup(curl As Pointer)
  25.  
  26.  
  27. Library "libc:6"
  28.  
  29. ' FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)
  30. ' Open a file and create a new stream for it.
  31. Private Extern fopen(__filename As String, __modes As String) As Pointer
  32.  
  33. ' int fclose (FILE *__stream)
  34. ' Close STREAM.
  35. Private Extern fclose(__stream As Pointer) As Integer
  36.  
  37.  
  38. Public Sub Main()
  39.  
  40.  Dim url, cu, fl As Pointer
  41.  Dim final_file As String
  42.  Dim ris As Integer
  43.  
  44. ' Web address of JPG image-file, that you want to download:
  45.  url = Alloc("https://cdn.pixabay.com/photo/2017/09/01/00/15/png-2702691_640.png")
  46.  
  47. ' Sets the path (and file name) where JPG image-file will be re-created:
  48.  final_file = "/tmp/file.jpg"
  49.  
  50.  cu = curl_easy_init()
  51.  If cu == 0 Then Error.Raise("Error !")
  52.  
  53.  curl_easy_setopt(cu, CURLOPT_URL, url)
  54.  
  55.  fl = fopen(final_file, "wb")
  56.  If fl == 0 Then Error.Raise("Error !")
  57.  
  58.  curl_easy_setopt(cu, CURLOPT_WRITEDATA, fl)
  59.  
  60. ' Writes new JPG image file:
  61.  ris = curl_easy_perform(cu)
  62.  If ris <> 0 Then Error.Raise("Error writing image-file: " & curl_easy_strerror(ris))
  63.  
  64. ' Free memory:
  65.  fclose(fl)
  66.  curl_easy_cleanup(cu)
  67.  


…if you want to try "wget" with Gambas:

Code (gambas)

  1. Public Sub Main()
  2.  
  3. ' It will download the image file to the "/tmp" folder.
  4. ' The Process of the "wget" command, launched by the "Shell" instruction, will be opened for "Read" to obtain the messages sent by the aforementioned Process in the console/Terminal.
  5.   Shell "wget https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Gamba.JPG/90px-Gamba.JPG --directory-prefix /tmp"  For Input As "Processus"
  6.  
  7.  
  8. Public Sub Processus_Read()
  9.  
  10.  
  11.  Line Input #Last, s
  12.  
  13.  Print s
  14.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Banned
If you look at this page…
https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/15/

you'll see the file does not exist
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
Diverod is in the usergroup ‘Regular’

vuott said

…if you want to try "wget" with Gambas:

Code (gambas)

  1. Public Sub Main()
  2.  
  3. ' It will download the image file to the "/tmp" folder.
  4. ' The Process of the "wget" command, launched by the "Shell" instruction, will be opened for "Read" to obtain the messages sent by the aforementioned Process in the console/Terminal.
  5.   Shell "wget https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Gamba.JPG/90px-Gamba.JPG --directory-prefix /tmp"  For Input As "Processus"
  6.  
  7.  
  8. Public Sub Processus_Read()
  9.  
  10.  
  11.  Line Input #Last, s
  12.  
  13.  Print s
  14.  

Thank you very much vuott! The wget code worked perfect in Gambas for all cases tried.
I've been reading the "man" page on curl and find it a bit overwhelming at this point for me but your code using a curl library is very helpful in understanding some of it. Thanks, RodG.
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
Diverod is in the usergroup ‘Regular’

BruceSteers said

If you look at this page…
https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/05/

you'll see the file does not exist

Okay, Bruce, now I feel stupid  :o I swear I could get it to display in my browser when testing this. I know these do expire after a certain time so I should have cleared my browser cache. Lesson learned after leaving my computer on for 2 days! I got a new one

Code

https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/15/20220032041_GOES16-ABI-se-15-300x300.jpg
and now everything works that I had previously tried :!:
Thanks for letting me know because I might have been still chasing that one for a while. RodG
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
Mitin77 is in the usergroup ‘unknown’
Downloading .JPG files from the web is a straightforward process. Right-click on the image and select "Save image as" to choose a destination on your device. Ensure the file name is descriptive for easy identification later. Enjoy your new images!
Online now: No Back to the top

Post

Posted
Rating:
#7
Banned

Mitin77 said

Downloading .JPG files from the web is a straightforward process. Right-click on the image and select "Save image as" to choose a destination on your device. Ensure the file name is descriptive for easy identification later. Enjoy your new images!

Erm , this is a Gambas Basic forum and that has nothing to do with how you download images using gambas basic.

OP probably knows how to download images using their web browser.  :lol:
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
Bigfooter is in the usergroup ‘Trainee’
Yeah, I’ve had this kind of issue before when grabbing image links from dynamically generated pages or ones that expire after a while. It's annoying because everything looks fine when you test it, then boom—404 out of nowhere. Clearing the browser cache or getting a fresh image URL usually solves it for me too, especially with those NOAA or satellite images that update frequently.

I ran into something similar while editing weather time lapse sequences. I needed to find consistent file formats that wouldn’t glitch during import. I found this helpful list of the best AVI video editors https://www.movavi.com/learning-portal/best-avi-video-editors.html when sorting that out, you might find something useful there if you're planning to edit image sequences into videos later.
Online now: No Back to the top
1 guest and 0 members have just viewed this.