Image aspect ratio changes even with match_aspect set to true in bokeh figure with image_url - bokeh

I tried to keep the aspect ratio of an online image as is. However, the aspect ratio will change when the browser window is resized. This doesn't happen with image, but happens with image_url. Is this a feature or a bug? Thanks.
I use the figure(match_aspect=True) and need to stretch the layer to occupy the full window layer = layout(p, sizing_mode='stretch_both')
Sample code
from bokeh.plotting import figure, show
from bokeh.layouts import layout
p = figure(match_aspect=True)
p.image_url(['https://static.bokeh.org/logos/logo.png'],0,0,1,1)
layer = layout(p, sizing_mode='stretch_both')
show(layer)

Related

NSPopover positions incorrectly when NSWindow frame is programmatically changed

I have a button in an NSWindow that brings up an NSPopover with:
popover = NSPopover()
popover?.behavior = .applicationDefined
popover?.contentViewController = self
let myDelegate = MyDelegate()
myDelegate.controller = self
popover?.delegate = myDelegate
popover?.show(relativeTo: hostingButton.bounds, of: hostingButton.superview!, preferredEdge: .minY)
This works great and I can drag the window around and resize it while the popover is open with zero problems.
The window, however, displays dynamic content and I have an option to keep it "compact" so to use the minimum height necessary for all of the current content to be shown.
When my code sets the NSWindow's frame with window.setFrame(newFrame, display: true), the popover moves to the other side of the screen. Its vertical alignment is perfect and matches the new window height, but its horizontal position is completely off.
The horizontal positioning on the Screen appears to be matching the x coordinate of the hostingButton within its superview, i.e. it sure looks like popover is using the x coordinate from the button's frame as a screen coordinate!?
Am I doing something wrong?
Is there a way of asking the popover, its hosting view, its superview, or its window to have another go at the positioning?
If not is there a way that I can manually move the popover window to the right place?
Any help would be much appreciated!

Qt flow layout with scroll area

I am attempting to do a HTML style grid layout in Qt using this example here: https://gist.github.com/Cysu/7461066. It nearly does what I want, however when I put it into a scroll area it seems to collapse down to a minimum size and not want to expand to the full size of the scroll area. I did see that in the documentation you need to set the sizeConstraint in the layout to SetMinAndMaxSize, however this did not seem to fix it.
How can I get the layout to occupy the maximum size inside of the scroll area?
Here's a basic example, importing the class from the link above:
class App(QtWidgets.QDialog):
def __init__():
super().__init__()
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
grid = QtWidgets.QWidget()
grid_layout = FlowLayout()
grid_layout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize)
grid.setLayout(grid_layout)
scroll_area = QtWidgets.QScrollArea()
scroll_area.setWidget(grid)
layout.addWidget(scroll_area)
# layout.addWidget(grid) # adding the widget the the parent layout works just fine...
for i in range(25):
grid_layout.addWidget(QtWidgets.QLabel(f"Label {i}"))

How to resize and reposition PaperJs drawing with canvas size change?

I'm creating an application using Paperjs to drawing some sketch on an image, persisting the sketch without the background and display back the sketch along with background image.
In my Paperjs canvas, I have a raster image and on top of that I'm making some sketch pointing a part of the background image. After the drawing is done, I'm doing JSON export, excluding background raster image, to persist the drawing. Now when I export it back, the background raster image is generated dynamically and the Paper canvas size is set to the size of the image. But if the browser window size is different then the size of the image will be different than where the original drawing is created.
The issue that I'm facing if the image size isn't the used while creating the sketch, its pointing to the different part of the image. Is there any way we can proportionally change the paperjs drawing so that the sketch points to the same point of the background raster image?
Any help would be appreciated.
Below is an example :
With actual size -
After resize
I'm able to achieve what I wanted to with the below changes. Thanks #arthur.sw for your help.
Here originalViewSize is the original size on which the actual sketch was created.
Steps:
Update view size
Calculate x-axis and y-axis scale factor
Update bounds for background image
Update active layer positions
Scale active layer
// Changing view size and calculating scale factor for x & y-axis
paperScope.view.viewSize = new Size(originalViewSize.width + 200, originalViewSize.height + 100);
const xScale = paperScope.view.viewSize.width /originalViewSize.width;
const yScale = paperScope.view.viewSize.height /originalViewSize.height;
// Update b/g image bounds
backgroundRaster.bounds = paperScope.view.bounds;
paperScope.project.activeLayer.position.x = paperScope.project.activeLayer.position.x * xScale;
paperScope.project.activeLayer.position.y = paperScope.project.activeLayer.position.y * yScale;
paperScope.project.activeLayer.scale(xScale, yScale);

Resize images inside qt-label

I have a qt label which by default has a place holder image in it:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("place_holder.jpg")))
There is a function to update the image contained in the label which is:
def selectFile(self):
image = QtGui.QFileDialog.getOpenFileName(None, 'Select Reference Image', '', '*.jpg')
self.label.setPixmap(QtGui.QPixmap(_fromUtf8(image)))
This works fine (the image is updated), but it is also deformed if the image used to update the label has different size from the place holder image.
Is there any way to fix this? I mean to adapt the image and to keep fix the size of the label?
You can try to set the scaledContents property:
self.label.setScaledContents(True)
self.label.setPixmap(QPixmap("your_image.jpeg"))
It works fine for me.
Beware that this scale the image to fill all available space. It means that the aspect ratio of the image wont be preserved unless the label size and the image have the same aspect ratio. You will have the same problem using the QPixmap.scale method as you can see in the images here.
Scale the image each time you import it. If you already have a size in mind for your label, you can even scale the place holder.
try (c++)
//initial setup
QPixmap pixPlaceHolder = QPixmap(_fromUtf8("place_holder.jpg");
QSize desiredsize = pixPlaceHolder.size(); // or any of the size you want
Each time you select a file:
label.setPixmap(QPixmap(_fromUtf8(image).scaled(desiredsize));
try this, this helped me for scaling the image to fit the label while maintaining the aspect ratio. Will also help to resize the image based on the label size.
pixmap = QPixmap(x)
pixmap = pixmap.scaled(self.label.size().width(),self.ui.label_4.size().height(), Qt.KeepAspectRatio)
self.label.setPixmap(QPixmap(pixmap))

How to scale and clip a BitmapImage in Flex 4.5

Here's my problem, I need to scale and clip images into square sized tiles to put into a tile list. Here's how I want it to work:
I want all my tiles to be, say, 300px x 300px.
For each image, I want to scale the shorter side (either width or height) to fit in the tile using the "letterbox" scaleMode (so that it maintains aspect ratio).
Then I want to position the image in the center and clip away anything left over from either both sides or the top and bottom.
Here's an example to help clarify:
I have an image with width=600px and height=1200px. First I want to scale the image to width=300px and height=600px (notice that aspect ratio is maintained), then center the image vertically and clip the image to 300 x 300.
Is this possible? This is actually a pretty standard way of displaying square thumbnails in many photo-based web sites, but I can't find a way to make it work in flex.
Any help would be appreciated, thanks!
UPDATE JUNE 2012:
Just in case anyone finds this thread now, this issue has been resolved in the latest version of the Flex SDK. On the spark image object there is a new scaleMode of "zoom" which does exactly what I've asked for here.
Take your big image and draw it on BitmapData with scale and reposition:
const zoom:Number = Math.max(THUMB_WIDTH/image.width, THUMB_HEIGHT/image.height);
const x:int = (THUMB_WIDTH - image.width*zoom)/2;
const y:int = (THUMB_HEIGHT - image.height*zoom)/2;
var matrix:Matrix = new Matrix;
matrix.scale(zoom, zoom);
matrix.translate(x, y);
var _thumbBitmap:BitmapData = new BitmapData(THUMB_WIDTH, THUMB_HEIGHT, false, 0xFFFFFF);
_thumbBitmap.draw(image, matrix, null, null, null, true);
Then assign resulting BitmapData to the source of the BitmapImage.
More: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#draw%28%29
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/primitives/BitmapImage.html#source

Resources