It is currently Sat Jul 06, 2024 8:08 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Format of pixel region data
PostPosted: Sat Jan 19, 2019 8:24 pm  (#1) 
Offline
New Member

Joined: Jan 19, 2019
Posts: 4
Hi,

Sorry if this is a silly question, but in all my Googling and looking through the docs, I couldn't find anything related to this besides a vague mention in the GIMP-Python documentation.

I'm writing a plugin that scales an image to 2x size using Hyllian's Super-xBR algorithm.

I have an array of RGBA values that I want to draw into a pixel region that spans the image. Formatted like this:
# Indices: 0   1   2   3  |  4   5   6   7  |  8
# Values:  235 127 0   94 |  221 67  95  255|  ...


where indices 0-3 are the R, G, B, A values of the pixel at [0, 0], indices 4-7 are the values of [1, 0], etc.

The documentation says that the input data for setting a pixel region "is a string containing the binary data of the requested region", but I'm not sure how it should be formatted. The plugin I used as a basic reference, Akkana's arclayer.py (can find on their website at shallowsky), just uses array.tostring() to change the input into a string and then sets the pixel region equal to it:

# Copy the whole array back to the pixel region:
dstRgn[0:newWidth, 0:newHeight] = dest_pixels.tostring()


however, doing this simply gives me an empty(transperant, all RGBA values 0) layer when I insert the layer into the image.

Is there another, or better, way of doing something like this? Or do I just have the wrong idea completely?

This is my first time using Python or dealing with GIMP at all(I used paint NET pretty heavily before I switched off Windows), so apologies if I'm missing something obvious.

Here's some relevant code, if it helps:

    original_pixel_region = tdrawable.get_pixel_rgn(0, 0, original_width, original_height, False, False)
    original_pixel_data = array("B", original_pixel_region[0:original_width, 0:original_height])

    # True if the pixel data is RGBA, false if it's RGB and needs alpha to be fudged.
    rgba_flag = (tdrawable.type == RGBA_IMAGE)
   
    original_pixel_data = rgba_to_int(original_width, original_height, original_pixel_data, rgba_flag)

    dest_drawable = gimp.Layer(timg, "scaled", out_width, out_height, RGBA_IMAGE, 100, NORMAL_MODE)
    dest_region = dest_drawable.get_pixel_rgn(0, 0, out_width, out_height, True, True)
    pdb.gimp_image_resize(timg, out_width, out_height, 0, 0)
   
    output_data = array("L", [0L] * (out_width * out_height))
   
    # ----- (algorithm) -----

    output_data = int_to_rgba(out_width, out_height, output_data)

    pdb.gimp_image_insert_layer(timg, dest_drawable, None, 0)
    dest_region[0:out_width, 0:out_height] = output_data.tostring()
    dest_drawable.flush()
    dest_drawable.update(0, 0, out_width, out_height)

    timg.undo_group_end()
    gimp.context_pop()
   


Thanks.


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Format of pixel region data
PostPosted: Sun Jan 20, 2019 4:27 am  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4756
My experimental numpyops.py script reads pixel regions into numpy arrays. Source is here: https://www.gimp-forum.net/Thread-Gimp-python-and-numpy

_________________
Image


Top
 Post subject: Re: Format of pixel region data
PostPosted: Sun Jan 20, 2019 3:00 pm  (#3) 
Offline
New Member

Joined: Jan 19, 2019
Posts: 4
ofnuts wrote:
My experimental numpyops.py script reads pixel regions into numpy arrays. Source is here: (snip)


Thanks for the source/tip, I noticed a lot of people seemed to recommend numPy arrays instead of using python's stdlib array class.

I'd like to keep it as portable as possible, though, without relying on external dependencies. Although Super-xBR is quite slow in Python, so if numPy was the only option, I wouldn't have too big a problem with using it.


Top
 Post subject: Re: Format of pixel region data
PostPosted: Wed Jan 23, 2019 9:23 pm  (#4) 
Offline
New Member

Joined: Jan 19, 2019
Posts: 4
Apologies for the double post, but I've been continuing to look at this and it seems that the output data is actually copying into the pixel region array just fine, as it should. However, no matter what PDB or Image methods I call(I've tried update and flush), I've been unable to make the pixels write to the layer - it continues to simply insert a blank layer and never fill the layer with the pixels in the pixel region, despite the layer having a reference to the pixel region.

Any help would be appreciated.


Top
 Post subject: Re: Format of pixel region data
PostPosted: Thu Jan 24, 2019 2:56 am  (#5) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 199
The first script is a template I made for using region derived from Akkana's arclayer.py. From this template I made the second script, I don't know if it will help with your problem but you might get some ideas from it

The Region test script swaps the red and blue channels, done just to see it works

These scripts work on my Gimp 2.8.22

Attachment:
region-template.zip [1.46 KiB]
Downloaded 196 times


scripts are found at "File > On test >"

[EDIT] I should have mentioned that in your coding you use 4 spaces which is convention, being lazy I use tabs. The thing is you can't mix them, needed to state this just in case you copy/paste any lines of my script. If you do, change each leading tab to 4 spaces

steve


Last edited by Steve on Thu Jan 24, 2019 5:16 am, edited 1 time in total.

Top
 Post subject: Re: Format of pixel region data
PostPosted: Thu Jan 24, 2019 3:24 am  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4756
math_skill wrote:
Apologies for the double post, but I've been continuing to look at this and it seems that the output data is actually copying into the pixel region array just fine, as it should. However, no matter what PDB or Image methods I call(I've tried update and flush), I've been unable to make the pixels write to the layer - it continues to simply insert a blank layer and never fill the layer with the pixels in the pixel region, despite the layer having a reference to the pixel region.

Any help would be appreciated.


Could be a simple error in your code, but until you post it we can't tell.

From my own script:

def createResultLayer(image,name,result):
    rlBytes=np.uint8(result).tobytes();
    rl=gimp.Layer(image,name,image.width,image.height,image.active_layer.type,100,NORMAL_MODE)
    region=rl.get_pixel_rgn(0, 0, rl.width,rl.height,True)
    region[:,:]=rlBytes
    image.add_layer(rl,0)
    gimp.displays_flush()


Which more or less is summarized as "get a pixel region from the layer and copy the bytes to it".

_________________
Image


Top
 Post subject: Re: Format of pixel region data
PostPosted: Thu Jan 24, 2019 6:21 pm  (#7) 
Offline
New Member

Joined: Jan 19, 2019
Posts: 4
Steve wrote:
The first script is a template I made for using region derived from Akkana's arclayer.py. From this template I made the second script, I don't know if it will help with your problem but you might get some ideas from it

The Region test script swaps the red and blue channels, done just to see it works

These scripts work on my Gimp 2.8.22

Attachment:
region-template.zip


scripts are found at "File > On test >"

[EDIT] I should have mentioned that in your coding you use 4 spaces which is convention, being lazy I use tabs. The thing is you can't mix them, needed to state this just in case you copy/paste any lines of my script. If you do, change each leading tab to 4 spaces

steve


Your reference code was helpful, thanks. I was under the impression that merge_shadow() wasn't important, but that was because I wasn't aware that the 'shadow' refers to a temporary processing buffer, and that without merging it, the layer will not be updated. After adding a call to merge_shadow(), the layer is successfully filled.

Guess I should have looked it up in the procedure database. :bigthup


Top
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Line/pixel size doesnot correspond with pixel resolution

3

No new posts Attachment(s) save restore data on demand

12

No new posts Parse Device Data size Error - FIXED

5

No new posts Attachment(s) Help for Gimp 2.10 in pdf format.

5

No new posts Attachment(s) Error ...Unable to decode abr format version 5.

14



* Login  



Powered by phpBB3 © phpBB Group