How to assign a php generated image?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

How to assign a php generated image?

Post by DorinDXN »

In FreeCAD I can load an image to plane or, say, display it with coin
the filename is something like
"c:\image.bmp"
I have a server which returns an image as response like
http://phpqrcode.sourceforge.net/exampl ... output.php
how to use that in FreeCAD, i.e. instead of
"c:\image.bmp"
to have
"http://phpqrcode.sourceforge.net/exampl ... output.php"

any extra third party software needed?

Appreciate any hint!

cheers,
Dorin
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to assign a php generated image?

Post by onekk »

php is "server side" so it return an html page containing something, in you case an image.

Usually the page will contain as return some code that could be parsed, but this need some "php parser" that could take the page an "decode the output".

FC has some browsing capabilities made with Qt like the "Start Page" but I don't think that is able to replicate a "full browser" with php interpreter and so on.


Probably you are asking for something a little bit "out target" for a Software that is not made to be used as a "service" nor as a "python library" that are some "corner cases" that could be done, but that is not the "main target" for a CAD program.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: How to assign a php generated image?

Post by DorinDXN »

If I launch MS Paint in Windows and at Open dialog, at file name I enter
http://phpqrcode.sourceforge.net/exampl ... output.php
instead of
C:\image.bmp

Paint opens the image which is a little QR code in this case.
sure, all the work in producing the image is done by the server, Paint only shows the image
Something like that, but for images in FreeCAD :roll:

cheers,
Dorin
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: How to assign a php generated image?

Post by DorinDXN »

Perhaps I should ask:

How to set a coin.SoImage
for an url instead of using
filename.setValue("C:\image.bmp") ?

thanks
Dorin
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to assign a php generated image?

Post by heda »

for pulling the image data from the web...
https://docs.python.org/3/library/urlli ... ib.request

typically there is a "datafield" somewhere that can be populated for things like this.
happens to be so in this case as well, it is all in the coin-docs (at least that is how i went from not a clue to a working example),
then one has to go through some hoops to fill that field without getting errors back...

Code: Select all

# courtesy wmayer for the basic adding of image to scenegraph

from io import BytesIO
from PIL import Image
from pivy import coin

# this is only to get a picture to use...
with open('Path_to_any_image_that_PIL_opens', 'rb') as handle:
    pic = handle.read()

# you will need to know what format you are getting the response as
# could be base64 encoded, plenty of posts/blogs on www for how to deal with that
picbytes = BytesIO(pic)

# end goal for preprocessing is to get the raw pixels as a sequence of bytes
# you could of course roll your own pixel extractor, but why do that when there is pillow...

im = Image.open(picbytes)
w, h = im.size
pixbytes = im.tobytes()
bpp = int(len(pixbytes)/w/h)

# would have been easier to do if the SoImage, or the regular Image wb for that matter,
# would accept a bytesio object instead of just a filepath string
# or at least for image wb, that one programmatically could set a data field.

# the coin stuff
textSep = coin.SoSeparator()
cam = coin.SoOrthographicCamera()
cam.aspectRatio = 1
cam.viewportMapping = coin.SoCamera.LEAVE_ALONE

trans = coin.SoTranslation()
trans.translation = (-0.95, -0.5, 0)

myImage = coin.SoImage()
#myImage.filename.setValue("Path_to_bitmap_file")
myImage.image.setValue(coin.SbVec2s(w, h), bpp, pixbytes)

textSep.addChild(cam)
textSep.addChild(trans)
textSep.addChild(myImage)

activeDoc = FreeCADGui.ActiveDocument
view = activeDoc.ActiveView
sg = view.getSceneGraph()
viewer = view.getViewer()
render = viewer.getSoRenderManager()
sup = render.addSuperimposition(textSep)
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: How to assign a php generated image?

Post by DorinDXN »

Thank you for reply

Got this far

Code: Select all

import requests
def download_php(url):
	headers = {
        "User-Agent": "",
    }
	response = requests.get(url, headers=headers)
	if response.status_code == 200:
		with open("s:\image.png", "wb") as f:
			f.write(response.content)
	else:
		print(response.status_code)
		
download_php("http://phpqrcode.sourceforge.net/examples/example_001_simple_png_output.php")
myImage = coin.SoImage()
myImage.filename.setValue("s:\image.png")
it does the job
FreeCAD 0.18 Python 2.7.14 Win 7
but uses an intermediate temporary image "s:\image.png"
I think must be a better solution :roll:

cheers,
Dorin
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to assign a php generated image?

Post by heda »

not sure what you mean with "better"...

anyhow,

Code: Select all

# python3
from urllib import request

url = "http://phpqrcode.sourceforge.net/examples/example_001_simple_png_output.php"

# this replaces the filereading done in the earlier post
# pic here holds exactly the same as pic in the earlier post
# at least with the link given as example...
with request.urlopen(url) as response:
    pic = response.read()
suppose code is similar in py2 except for the import...
https://docs.python.org/release/2.7.14/ ... llib2.html
Post Reply