I am using Gtk.jl and want to save image from GtkCanvas
Here is my code:
canvas = #GtkCanvas()
draw_area = GtkWindow(canvas, "Canvas", 300, 300)
How can i do this?
You could use Cairo to create a surface and then save it as an image. eg in your draw function for the canvas in Gtk:
#guarded draw(can) do widget
...
if _want_file_save_flag
surface_buf = Gtk.cairo_surface_for(can)
# Cairo.jl functions to write a surface to your image file go here
end
end
Related
Is there a way to show a picture/image in a QToolTip?
I want to show small images of keyboard-buttons to explain the user which buttons/shortcuts he can use on that specific widget.
You can easily show images with the following html code:
QToolTip::showText(QCursor::pos(), "<img src=':/icon.png'>Message", this, QRect(), 5000);
This example will show a tooltip with the image from Qt resources and the text for 5 seconds.
For more details, you can see this video: https://youtu.be/X9JD8gKGZ00
Edit1:
If you want to show an image from memory, you can save the QImage/QPixmap to memory (preferably using some lossless compression like PNG) and convert it to base 64 and load it with the html code, like this:
QImage icon = QImage(10, 10, QImage::Format_ARGB32);
icon.fill(QColor(255, 0, 0, 100));
QByteArray data;
QBuffer buffer(&data);
icon.save(&buffer, "PNG", 100);
QString html = QString("<img src='data:image/png;base64, %0'>Message").arg(QString(data.toBase64()));
QToolTip::showText(QCursor::pos(), html, this, QRect(), 5000);
Edit2: Corrected the html string as #Damon Lynch suggests.
With respect to the HTML string, the Python 3 / PyQt equivalent of user2014561's excellent solution to displaying in memory images is like this, assuming a QPixmap (a QImage will work the same):
buffer = QBuffer()
buffer.open(QIODevice.WriteOnly)
pixmap.save(buffer, "PNG", quality=100)
image = bytes(buffer.data().toBase64()).decode()
html = '<img src="data:image/png;base64,{}">'.format(image)
Working on allowing the upload of images which can range in a variety of size, then allowing to crop a predefined area of the image for a thumbnail.
The thumbnail size is predefined to 150x150. Using the Jcrop.js tool to select a section of the image.
Problem:
When displaying the uploaded image in a smaller size than the original image by implementing set height/width on the image rendered, then there is a scale factor that comes into play when selecting an area to crop.
You either have to scale down the cropping area proportionately or you have to scale the image in relation to the actual image's size in comparison to its displayed size.
Question:
How do I figure out the scale of the browser displayed image vs. original image? I am currently using the following code to save the image, but how would I take into consideration the scaling?
public static Image CropImage(Image originalImage, int x, int y, int width, int height)
{
var bmp = new Bitmap(width, height);
bmp.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
using (var graphic = Graphics.FromImage(bmp))
{
graphic.SmoothingMode = SmoothingMode.AntiAlias;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.PixelOffsetMode = PixelOffsetMode.HighSpeed;
graphic.DrawImage(originalImage, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
return bmp;
}
}
Bonus Question:
Another problem I discovered, is that there seems to be no efficient way to transfer the original file's ImageFormat when creating a new Bitmap which creates a ImageFormatMemoryBMP and when you attempt to call Bitmap.Save(memorystream, original rawformat) it will blow up. And bitmap RawFormat has no setter.
So how can you set the format on a new bitmap?
I think perhaps that this problem is solved purely on the front end, no need to use any server side for this.
Jcrop has a built in scale factor handler.
http://deepliquid.com/content/Jcrop_Sizing_Issues.html
Now you can use this in two ways, as I understand it. Either to 'resize' the image for you on the front end using 'box sizing', or you can tell it the 'truesize' of the image and it will work out the scale factor and handle the coordinates for you on it's own.
Box sizing
$('#cropbox').Jcrop({ boxWidth: 450, boxHeight: 400 });
True Size
$.Jcrop('#cropbox',{ trueSize: [500,370] });
Using the true size method you will need to invoke jcrop using the api method:
http://deepliquid.com/content/Jcrop_API.html#API_Invocation_Method
var jcrop_api,
options = { trueSize: [500,370] };
$('#target').Jcrop(options,function(){
jcrop_api = this;
});
Good luck!
I want to display an image in my app. I use QtDesigner to design UI, then use pyqt to coding. The problem is the image that will be shown is lager than the widget size on the UI. I refer to offical demo:
QT - Widget Image Viewer Demo
add imagelabel and scrollArea, code as follows:
---- UI init ----
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.label.setSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
self.label.setObjectName(_fromUtf8("label"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.scrollArea.setWidget(self.label)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
---- function ----
filename = "./Penguins.jpg"
image = QtGui.QImage(filename)
pp = QtGui.QPixmap.fromImage(image)
lbl = QtGui.QLabel(self.label)
lbl.setPixmap(pp)
self.scrollArea.setWidgetResizable(True)
lbl.show()
but it doesn't stretch the image, even no scroll bar appear!
You need to call self.label.setScaledContents(true);. So that QLabel will resize itself to the size of pixmap/image and scroll-bar will get visible. See this documentation.
The default implementation of QLabel::setScaledContents wasn't working for me, since it didn't allow me to keep the aspect ratio when the images where larger then the label's
maximum sizes.
This little helper will scale the image down to fit into a label's maximum size if needed (but not up), always keeping the aspect ratio:
/**
* Fill a QLabel widget with an image file, respecting the widget's maximum sizes,
* while scaling the image down if needed (but not up), and keeping the aspect ratio
* Returns false if image loading failed
****************************************************************************/
static bool SetLabelImage(QLabel *label, QString imageFileName)
{
QPixmap pixmap(imageFileName);
if (pixmap.isNull()) return false;
int w = std::min(pixmap.width(), label->maximumWidth());
int h = std::min(pixmap.height(), label->maximumHeight());
pixmap = pixmap.scaled(QSize(w, h), Qt::KeepAspectRatio, Qt::SmoothTransformation);
label->setPixmap(pixmap);
return true;
}
I do not use PyQt but the QtPixmap control has scaled() functions. You can resize the image before put in the label:
scaled()
scaledToHeight()
scaledToWidth()
This is the sample code I use in C++ to resize an image to the QLabel size:
imatge.load("sprite.png");
QPixmap imatge2 = imatge.scaled(ui->label->width(),ui->label->height());
Well, the problem may be a simple one but I can't figure it out. I have an image loaded into BitmapData. now I want to take text from a textinput and put it on the BitmapData. Basically it's drawing a text on the BitmapData and get the result as another BitmapData that will consist of the original BitmapData with the text drawn over it on a specified position. What's the best way to achieve this in flex?
To put the text inside a bitmap you can do:
var channelName:TextField = new TextField();
channelName.textColor=0x000000;
channelName.antiAliasType = AntiAliasType.NORMAL;
channelName.alpha=1.0;
var txtFormat:TextFormat = new TextFormat("SansSerif",14,0x000000,true);
channelName.setTextFormat(txtFormat);
var bitmapdata:BitmapData = new BitmapData(
channelName.width, channelName.height, true, 0x000000);
bitmapdata.draw(channelName);
You can't draw over bitmapdata per say, but you could compose it from the data. Since you have BitmapData, it's easy enough to change it to a bitmap (var bitmap:Bitmap = new Bitmap(bitmapData);) and then add it as source for an image.
Now that you have an actual image on the stage, you can now add text above that using what you like (text, label, textarea, etc) and then you can do a Bitmap.draw over the dimensions of the image to get the pixel information back into a BitmapData (under Bitmap.bitmapData).
I am trying to create a similar effect to what is used in CityVille for dropping coins and experience icons, they do a custom move animation and it first moves a little bit up and then down. It looks like it is following a spline or a sine function.
The Move effect in flex 3 only moves linearly.
Any help?
Use a tweening library such as Actuate http://code.google.com/p/actuate/
It lets you animate an object along a bezier curve or custom motion path like this:
var xPath:MotionPath = MotionPath.bezier (200, 20).line (400);
var yPath:MotionPath = MotionPath.bezier (0, 300).line (0);
Actuate.motionPath (MySprite, 1, { x: xPath, y: yPath } );
A very similar library is eaze
http://code.google.com/p/eaze-tween/
and it's mxml friendly wrapper:
http://code.google.com/p/eazefx/
You should be looking at Tweens instead of Moves. An example could be:
import mx.transitions.easing.*;
import mx.transitions.Tween;
new Tween(myMC, ‘_x’, Regular.easeOut, myMC._x, myMC._x + 300, 30);
new Tween(myMC, ‘_y’, Regular.easeIn, myMC._y, myMC._y + 300, 30);
Code is random Google result, so I post no guarantees. Also for myself I would prefer a tween engine like TweenLite: http://www.greensock.com/tweenlite/