Hack Forums
[Python] Urllib2 to save a image - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Urllib2 to save a image (/showthread.php?tid=5774015)



Urllib2 to save a image - CleverUSB - Dec 24, 2017

I'm having some issues with my Twitter Bot. The bot works perfectly but I'm having the issues with the image part. It currently posts the same image every time the tweet is sent when it should be posting the new one that urllib2 got and saved into the image. I don't get any errors but I believe its because I didn't add image.close(). I tried adding image.close() but I keep getting "ValueError: I/O operation on closed file" where ever I put it in the code. I tried attaching the code on in the thread but Hack Forums firewall blocked it. Instead, I have attached Leet Files and Paste Bin links. Thanks ahead of time.

Paste Bin: here
Leet Files: here


RE: Need help with Python - Not Your Average Orange - Dec 24, 2017

Why not just paste it here?


RE: Need help with Python - CleverUSB - Dec 25, 2017

(Dec 24, 2017 07:06 PM)Not Your Average Orange Wrote:  Why not just paste it here?

Sorry I was in a hurry and didn't have time to copy and paste. I attached the code now.




RE: Urllib2 to save a image - CleverUSB - Dec 27, 2017

I’m still having this issue and it would be great to receive help. Thanks!


RE: Urllib2 to save a image - Nomad.Null - Dec 28, 2017

(Dec 27, 2017 11:11 AM)CleverUSB Wrote:  I’m still having this issue and it would be great to receive help. Thanks!

So, as I understand it, you are using the image variable as a writable bytes object to "print" your image to. That's perfectly okay as it is, just make sure you close it out at the end of the program before exiting. I think your problem lies with updating the snapshot. Notice that your loop never updates the image variable with the new one grabbed, so essentially it is repeatedly writing the same currentPrintImage variable into the image variable. I would do it differently like so...

https://pastebin.com/THqrRt1T

If I am understanding your logic correctly, if you move your currentPrintImage inside of your while loop and re-assign it by calling the urllib.urlopen("http://blah.blah") BEFORE it is read() into the image file object you created, then this should update the image with a fresh snapshot each time. 🤝


RE: Urllib2 to save a image - CleverUSB - Dec 29, 2017

(Dec 28, 2017 09:43 AM)True Demon Wrote:  So, as I understand it, you are using the image variable as a writable bytes object to "print" your image to. That's perfectly okay as it is, just make sure you close it out at the end of the program before exiting. I think your problem lies with updating the snapshot. Notice that your loop never updates the image variable with the new one grabbed, so essentially it is repeatedly writing the same currentPrintImage variable into the image variable. I would do it differently like so...

https://pastebin.com/THqrRt1T

If I am understanding your logic correctly, if you move your currentPrintImage inside of your while loop and re-assign it by calling the urllib.urlopen("http://blah.blah") BEFORE it is read() into the image file object you created, then this should update the image with a fresh snapshot each time. 🤝

Thanks for reviewing my code. It helped alot.