Manipulate pixels in QGraphcisPixmapItem - pyqt

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.

Related

Save part of the scene as an image (not the whole scene)

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")

Tkinter Button not working when inserting an image

I am trying to add buttons with custom bg or image so like 3d color. But it gives a white unclickable button as result. I tried many other ways but failed, the code i gave below its a code which lets us to select the weight and height we want for the button that's why i choosed it otherwise if other things like the .zoom or making a function to make the image fit the button were working i would be using them, they are all not working and i have no idea, i know i must be doing something wrong but wellas you see i am new in python, i learned everything myself so really i am 0 x). I Resized the image aswell, it has the same weight height with the button:
root.geometry(geo)
image = ImageTk.PhotoImage(file='background.png')
label = Label(root, image=image)
label.pack(fill='both', expand=True)
label.image = image
myWidth = 150
myHeight = 82
img4=Image.open('button.png')
image4=img4.resize((myWidth,myHeight))
useImg4=ImageTk.PhotoImage(image4)
quitBtn = Button(label, image=useImg4,command=lambda: root.destroy())
quitBtn.pack()
do you know why its not working?
The result:

python tkinter, Scrollbar missing knob(thumb)

【Summary】
By tkinter, python. I set scrollbar on Canvas ... Then that has been succeeded.
But missing knob in scrollbar.
【Background】
This is my application that developing now.
App's purpose is simple. Get some icons from target URL, Then put as tile in Window.
Application Window graphic
As you see, Can't put all icons in initial window size.
So I want to use scrollbar, Then scrolldown to show below icons.
Now succeeded put scrollbar at right side. But in that bar missing knob(thumb).
So this isn't working as scrollbar (TωT)
【Question】
How to make code to this vertical scrollbar working?
This is scrollbar build section in my src file.
Already exists scrollbar, It's almost fine... But maybe missing something.
# Make vertical scrollbar to see all stickers -----------------------
outCV = tk.Canvas(self.iconsFrame, width=GUIController.__windowWidth, height=GUIController.__windowHeight)
scrollbar = tk.Scrollbar(self.iconsFrame, orient=tk.VERTICAL)
scrollbar.config(command=outCV.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
outCV.configure(yscrollcommand=scrollbar.set)
outCV.pack()
# --------------------------------------------------------------------
gridRow = 0
gridCol = 0
for i, tkimg in enumerate(self.tkimgs) :
# Put icons as tile.
Please give me your knowledge.
(FYI) https://github.com/we-yu/L_SL/blob/develop/Canvas_in_Canvas/src/GUICtrl.py Line:196
I found already how to solve it by myself.
There are 2 points in my case.
Should be set scrollregion
Scrollbar works for only Canvas. So, on my case. Need overlapped frames and canvases.
Solving way
self.outerCV = tk.Canvas(self.iconsFrame, width=GUIController.__windowWidth, height=GUIController.__windowHeight)
scrollbar = tk.Scrollbar(self.iconsFrame, orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
scrollbar.config(command=self.outerCV.yview)
self.outerCV.config(scrollregion=(0, 0, 2000, 2000), yscrollcommand=scrollbar.set)
self.outerCV.pack(fill=tk.BOTH)
galleryFrame = tk.Frame(self.outerCV)
self.galleryTag = self.outerCV.create_window((0, 0), window=galleryFrame, anchor=tk.NW, width=self.outerCV.cget('width'))
Put Basement-frame at bottom of window. Then put Canvas on that. Scrollbar attaches on this Canvas.
But if put Canvas on this Canvas, Scrollbar works for scroll, But window won't be working.
So, put frame on scrollbar-canvas. Then Main graphic canvases on that frame.
[Canvas-1][Canvas-2][Canvas-3]...
[Frame for gallery]
[Canvas for Scrollbar]
[Basement frame]

Display number in rectangle

Note that I am using Python3 and Phoenix.
I would like to display a number (double, but that does not matter now) formatted in some way (again, no matter what that way is) within a rectangle: almost a wx.StaticText but not editable by the user. This is to display some data coming from some hardware, such as a temperature.
Is there such a widget?
I tried with using the default wx.StaticText with a style but I must have done something wrong:
hbox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(parent, label=label)
title.SetLabelMarkup("<b>{}</b>".format(label))
hbox.Add(title, border=5)
value = wx.StaticText(parent, label="3.141592", style=wx.BORDER_RAISED)
value.SetWindowStyle(wx.BORDER_SIMPLE)
hbox.Add(value, border=5)
title = wx.StaticText(parent, label="\u2103")
hbox.Add(title, border=5)
Shows this on Linux (Fedora 24, GTK):
Wouldn't using a wx.TextCtrl set to read only do the job?
Temp = wx.TextCtrl(panel1, value="3.141592", style=wx.TE_READONLY)
Temp.SetBackgroundColour('green')
The simplest solution is to just use wxStaticText with a border style (e.g. wxBORDER_SIMPLE, ...). If you don't like the appearance this results in, it's pretty simple to make your own widget drawing whatever border you desire: just create a window, define its wxEVT_PAINT handler and draw the (presumably centered) text in it and a border outside of it.

fix a splash screen image to display in j2me

In my j2me App I have tried canvas which works great on Nokia phone but doesn't run on samsung. For that I have to switch to some FORM which in both cases works but only issue is of size, if I create smaller image to fit for both phone screens, one (samsung) shows that ok but other (nokia) leaves a lot more space and vice versa.
I need to have code that could stretch my image and just fix if to the screen size which I basically get by form.getHeight() and form.getWidth() property. I wonder if there is property of Image.createImage(width, height) then why doesn't it stretch it to the value I provide?
my code for that is below
try {
System.out.println("Height: " + displayForm.getHeight());
System.out.println("Width: " + displayForm.getWidth());
Image img1 = Image.createImage("/bur/splashScreen1.PNG");
img1.createImage(displayForm.getHeight(), displayForm.getWidth());
displayForm.append(new ImageItem(null, img1, Item.LAYOUT_CENTER, null));
} catch (Exception ex) {
}
Image
A single image will not fit all screens. But more will do.
The smaller logo image should be less than 96x54, as this is the smallest screen resolution. This image can be used up to the resolution of 128x128 without problems. With bigger resolutions it will look tiny, though.
The bigger logo image should be a bit bigger than 128x128 and can be used up to 240x320.
The code bellow gives as example of how to implement this.
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
class Splash extends javax.microedition.lcdui.Canvas {
Image logo;
Splash () {
if (getWidth() <= 128) {
// sl stands for Small Logo and does not need to have a file extension
// this will use less space on the jar file
logo = Image.createImage("/sl");
} else {
// bl stands for Big Logo
logo = Image.createImage("/bl");
}
}
protected void paint (Graphics g) {
// With these anchors your logo image will be drawn on the center of the screen.
g.drawImage(logo, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER);
}
}
As seen in http://smallandadaptive.blogspot.com.br/2008/10/showing-splash.html
wonder if there is property of Image.createImage(width, height) then why doesn't it stretch it to the value I provide?
Parameters in this method have nothing to do with stretching, see API javadocs:
public static Image createImage(int width, int height)
Creates a new, mutable image for off-screen drawing.
Every pixel within the newly created image is white.
The width and height of the image must both be greater than zero.
Parameters:
width - the width of the new image, in pixels
height - the height of the new image, in pixels
Image class (API javadocs) has two more createImage methods that use parameters called "width" and "height" - one with six, another with four arguments but none of these has anything to do with stretching.
In createImage with six arguments, width and height specify size of the region to be copied (without stretching) from source image.
In method with four arguments, width and height specify how to interpret source ARGB array, without these it would be impossible to find out if, say, array of 12 values represents 3x4 image or 4x3. Again, this has nothing to do with stretching.

Resources