Save part of the scene as an image (not the whole scene) - python-3.x

Saving the (entire) scene as an image is not a problem (see my code) ... but I'd like to save a part of the scene with precise coordinates (and not the whole scene) ... and there I don't know how to go about it, is it possible?, if so, how to do it?, can you help me?
scene_rectangle = self.scene.sceneRect()
self.scene.clearSelection()
img = QImage(QSize(self.scene.sceneRect().width(), self.scene.sceneRect().height()), QImage.Format_ARGB32_Premultiplied)
painter = QPainter(img)
self.scene.render(painter, target=QRectF(self.scene.sceneRect().x(), self.scene.sceneRect().y(), self.scene.sceneRect().width(), self.scene.sceneRect().height()), source=scene_rectangle)
painter.end()
img.save("... path to save ... /image.png")

Related

How to position text ontop of an Image Label

I have been tinkering with Tkinter the last couple of days to create a little game.
I have searched for how to display text ontop of an image label but there is a little downside, it won't display exactly at the spot i am looking(in this case the top left side of the label).
I tried anchor and compound together in the same label but it doesn't seem to do anything new
the text still remains in the middle.
The code looks like this:
'''Adjusting the information frame'''
self.image_info = PhotoImage(file=locate_images("_info_panel", ""))
self.label_text_info = "• Welcome to Agony,\nto play or read the rules please go to File"
self.label_info = Label(self.info, text=self.label_text_info, font=10, image=self.image_info, compound=CENTER, anchor=NW)
self.label_info.grid(row=0, column=0)
Edit:
If I try to add anything else other than compound=CENTER the text is displayed outside the boundingbox of the image on the specified side.
Moved from an edit to the question by the OP to an answer.
Use a Canvas:
'''Adjusting the information frame'''
self.image_info = PhotoImage(file=locate_images("_info_panel", ""))
self.label_text_info = "• Welcome to Agony,\nto start the game or read the rules please go to File"
self.canvas_info = Canvas(self.info, width=300, height=600)
self.canvas_info.create_image((0, 0), image=self.image_info, anchor=NW)
self.canvas_info.create_text((140, 25), text=self.label_text_info)
self.canvas_info.grid(row=0, column=0)
I was approaching it wrong from the start, I am fearly new to tkinter
and I had to use a canvas to display the items inside of.

How do I carry over identical texture mapping when exporting to DAE?

I am able to open a 3DS file in MeshLab and when I export to Collada DAE format the textures are visible but they are not being projected onto the mesh in the same way as the preview in MeshLab. For example, the front/back faces of a cube would have the proper texture (suppose it's a polka dot) but the top and bottom have a striped look. How can I apply a single texture and have it appear as intended on all faces, like the imported model before I convert it?
This problem is a result of the end software being used to view the DAE file. It's not a problem with MeshLab.
For example, if loading the file into Away3D be sure to handle the texture materials using the TextureMaterial class instead of the simpler SinglePassMaterialBase such as what you might find in their example code. Here is what I use now, and it displays texture properly:
var material:TextureMaterial = cast(asset, TextureMaterial);
material.ambientColor = 0xffffff;
material.lightPicker = _lightPicker;
material.shadowMethod = new FilteredShadowMapMethod(_light);
material.lightPicker = _lightPicker;
material.gloss = 30;
material.specular = 1;
material.ambient = 1;
material.repeat = true;

Why isn't this mask working in Phaser?

I must be missing something...why isn't this working? Instead of clipping to the circle the entire 800x800 backdrop image is displayed...
var mask;
var img;
function preload() {
game.load.image('back', 'backdrop.jpg');
}
function create() {
img = game.add.image(game.world.centerX, game.world.centerY,'back').anchor.setTo(0.5);
mask = game.add.graphics(0, 0);
mask.beginFill(0xffffff);
mask.drawCircle(game.world.centerX, game.world.centerY, 600);
img.mask = mask;
}
jsfiddle here
Disclaimer: I have no formal experience in phaser.io
I was able to fix this in your fiddle by changing
img = game.add.image(game.world.centerX, game.world.centerY,'back').anchor.setTo(0.5);
to
img = game.add.image(0, 0, 'back');
JSFiddle Fork
I would assume that placing the image in the centerX,centerY position results in the mask being offset of the image. Hopefully someone with more experience than I could explain the specifics here, but I will research further and update my answer as I figure out the why to go along with the how.
Update
Okay so I've done some digging through the documentation. First, you want to use img = game.add.image(0, 0, 'back'); due to the fact that the x and y parameters in this case dictate the upper-left origin of the image, not the center. By using game.world.centerX and game.world.centerY you are trying to throw the background image to the center of the canvas even though the canvas is the same size as the image.
using .anchor.setTo(0.5) from what I can gather, is attempting to set the anchor point at which the image originates to the centerX position. However, when you remove this anchor, suddenly the mask works, even though it is not showing correctly (because the position of the background image is incorrect).
Theory - By anchoring the image, I believe that it is no longer possible to apply a mask to it. By all experimenting that I've done, having an anchor set on the background image prevents it from being masked, so the mask simply is added as a child to img and is placed as it's center, thus why you are seeing the white circle instead of the circle properly masking the image.
it appears I was a mistaken about the fluidity of the api in trying to chain that last function call... if I break it up:
img = game.add.image(game.world.centerX, game.world.centerY, 'back');
img.anchor.setTo(0.5);
it now works!
Fiddle Here

Unwanted black borders when trying to expand image to full size

When taking a screenshot of my scene in JavaFx, I save the BufferedImage to a file as a PNG/JPG. When I try to maximize the image size to its full length, I get Black borders on the picture from the left of the image to the bottom without the image increasing its size at all.
The size of the image only increases until I set my dimensions to 1300x700 as shown below.
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
However once I increase the dimensions greater than 1300x700, the black borders appear.
The following picture is set to
BufferedImage image = new BufferedImage(1500, 900, BufferedImage.TYPE_INT_RGB);
As you can see, part of the image is still cut off and there is now a big black border next to the image rather than the actual full sized image.
The following picture is set to
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
As you can see, the image is still cut off at the same spot as before but there is no black border along side with it.
How can I fit the whole snapshot of my current scene into one file without these borders and without any of the content getting cut off?
Here is my code:
File fa = new File("test.jpg");
snapshot = quotes.getScene().snapshot(null);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(snapshot, null);
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
image.setData(renderedImage.getData());
ImageIO.write(image, "jpg", fa);
The black border comes from uninitialized pixel buffer, inside your BufferedImage object.
So, I guess the renderedImage itself does not contains the right part of your scene.
The scene may not yet be properly resized when you are taking the snapshot. Try to give an appropriate WritableImage to the snapshot method:
snapshot = quotes.getScene().snapshot(new WritableImage(1500, 900));

Manipulate pixels in QGraphcisPixmapItem

I am using PyQt to build a small application for viewing images. When I click on the image I would like to alter the color of the pixels I have clicked:
Schematically my current code looks like this:
scene = QtGui.QGraphicsScene()
view = QtGui.QGraphicsView( scene )
image = QtGui.QImage( "image.png" )
pixmap = QtGui.QGraphicsPixmapItem( QtGui.QPixMap.fromImage( image ))
scene.addItem( pixmap )
...
...
def mousePressEvent(self , event):
print "Click on pixmap recorded - setting Pixel to red"
image.setPixel( event.pos() , RED.rgb())
The code 'works' in the sense that the mousePressEvent() method is called, and the image.setPixel() method does not give any errors, but nothing happens on the screen. Any tips on how to get the updated pixels to be displayed?
Joakim
To make changes appear, you need to reload image
self.image.setPixel(event.pos(), RED.rgb())
self.pixmap.setPixmap(QtGui.QPixmap.fromImage(self.image))
But I'm not sure that it's a good way. If you don't need to save modified image, I'd add some circles (e.g. addEllipse) instead modifying pixels.
Also, don't forget to map window coordinates to image.

Resources